2 * board-n516-display.c -- Platform device for N516 display
4 * Copyright (C) 2009, Yauhen Kharuzhy <jekhor@gmail.com>
5 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/sched.h>
21 #include <linux/sysctl.h>
22 #include <linux/proc_fs.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/input.h>
26 #include <linux/power_supply.h>
27 #include <linux/suspend.h>
29 #include <linux/i2c.h>
31 #include <asm/mach-jz4740/irq.h>
32 #include <asm/mach-jz4740/gpio.h>
33 #include <asm/mach-jz4740/board-n516.h>
36 static int batt_level=0;
37 module_param(batt_level, int, 0);
39 struct n516_lpc_chip {
40 struct i2c_client *i2c_client;
41 struct input_dev *input;
42 unsigned int battery_level;
43 unsigned int suspending:1, can_sleep:1;
46 static struct n516_lpc_chip *the_lpc;
48 struct i2c_device_id n516_lpc_i2c_ids[] = {
53 MODULE_DEVICE_TABLE(i2c, n516_lpc_i2c_ids);
55 static const unsigned short normal_i2c[] = {0x54, I2C_CLIENT_END};
57 static const unsigned int n516_lpc_keymap[] = {
68 [0x0d] = KEY_PLAYPAUSE,
71 [0x10] = KEY_DIRECTION,
78 [0x19] = KEY_PAGEDOWN,
83 /* [0x1f] = KEY_WAKEUP,*/
86 static const unsigned int batt_charge[] = {0, 7, 20, 45, 65, 80, 100};
87 #define MAX_BAT_LEVEL (ARRAY_SIZE(batt_charge) - 1)
89 /* Insmod parameters */
90 I2C_CLIENT_INSMOD_1(n516_lpc);
92 static inline int n516_bat_usb_connected(void)
94 return !gpio_get_value(GPIO_USB_DETECT);
97 static inline int n516_bat_charging(void)
99 return !gpio_get_value(GPIO_CHARG_STAT_N);
102 static int n516_bat_get_status(struct power_supply *b)
104 if (n516_bat_usb_connected()) {
105 if (n516_bat_charging())
106 return POWER_SUPPLY_STATUS_CHARGING;
108 return POWER_SUPPLY_STATUS_FULL;
110 return POWER_SUPPLY_STATUS_DISCHARGING;
114 static int n516_bat_get_charge(struct power_supply *b)
116 return batt_charge[the_lpc->battery_level];
119 static int n516_bat_get_property(struct power_supply *b,
120 enum power_supply_property psp,
121 union power_supply_propval *val)
124 case POWER_SUPPLY_PROP_STATUS:
125 val->intval = n516_bat_get_status(b);
127 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
130 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
133 case POWER_SUPPLY_PROP_CHARGE_NOW:
134 val->intval = n516_bat_get_charge(b);
142 static void n516_bat_power_changed(struct power_supply *p)
144 power_supply_changed(p);
147 static enum power_supply_property n516_bat_properties[] = {
148 POWER_SUPPLY_PROP_STATUS,
149 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
150 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
151 POWER_SUPPLY_PROP_CHARGE_NOW,
154 static struct power_supply n516_battery = {
155 .name = "n516-battery",
156 .get_property = n516_bat_get_property,
157 .properties = n516_bat_properties,
158 .num_properties = ARRAY_SIZE(n516_bat_properties),
159 .external_power_changed = n516_bat_power_changed,
162 static irqreturn_t n516_bat_charge_irq(int irq, void *dev)
164 struct power_supply *psy = dev;
166 dev_dbg(psy->dev, "Battery charging IRQ\n");
168 if (n516_bat_usb_connected() && !n516_bat_charging())
169 the_lpc->battery_level = MAX_BAT_LEVEL;
171 power_supply_changed(psy);
176 static int n516_lpc_set_normal_mode(struct n516_lpc_chip *chip)
178 struct i2c_client *client = chip->i2c_client;
179 unsigned char val = 0x02;
180 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
183 ret = i2c_transfer(client->adapter, &msg, 1);
184 return ret > 0 ? 0 : ret;
187 static void n516_key_event(struct n516_lpc_chip *chip, unsigned char keycode)
189 struct i2c_client *client = chip->i2c_client;
190 bool long_press = false;
192 if (keycode & 0x40) {
197 dev_dbg(&client->dev, "keycode: 0x%02x, long_press: 0x%02x\n", keycode, (unsigned int)long_press);
199 if (keycode >= ARRAY_SIZE(n516_lpc_keymap) || n516_lpc_keymap[keycode] == 0)
203 input_report_key(chip->input, KEY_LEFTALT, 1);
205 input_report_key(chip->input, n516_lpc_keymap[keycode], 1);
206 input_sync(chip->input);
207 input_report_key(chip->input, n516_lpc_keymap[keycode], 0);
210 input_report_key(chip->input, KEY_LEFTALT, 0);
211 input_sync(chip->input);
215 static void n516_battery_event(struct n516_lpc_chip *chip, unsigned char battery_level)
217 if (battery_level != chip->battery_level) {
218 chip->battery_level = battery_level;
219 power_supply_changed(&n516_battery);
223 static irqreturn_t n516_lpc_irq(int irq, void *devid)
225 struct n516_lpc_chip *chip = (struct n516_lpc_chip*)devid;
227 unsigned char raw_msg;
228 struct i2c_client *client = chip->i2c_client;
229 struct i2c_msg msg = {client->addr, client->flags | I2C_M_RD, 1, &raw_msg};
231 ret = i2c_transfer(client->adapter, &msg, 1);
233 dev_dbg(&client->dev, "I2C error: %d\n", ret);
237 dev_dbg(&client->dev, "msg: 0x%02x\n", raw_msg);
239 if ((raw_msg & 0x40) < ARRAY_SIZE(n516_lpc_keymap)) {
240 n516_key_event(chip, raw_msg);
241 } else if ((raw_msg >= 0x81) && (raw_msg <= 0x87)) {
242 n516_battery_event(chip, raw_msg - 0x81);
244 n516_lpc_set_normal_mode(chip);
245 dev_warn(&client->dev, "Unkown message: %x\n", raw_msg);
246 ret = i2c_transfer(client->adapter, &msg, 1);
248 dev_dbg(&client->dev, "I2C error: %d\n", ret);
250 dev_warn(&client->dev, "Unkown message part 2: %x\n", raw_msg);
255 if (chip->suspending)
263 static void n516_lpc_power_off(void)
265 struct i2c_client *client = the_lpc->i2c_client;
266 unsigned char val = 0x01;
267 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
269 printk("Issue LPC POWEROFF command...\n");
271 i2c_transfer(client->adapter, &msg, 1);
274 static int n516_lpc_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
279 static int n516_lpc_suspend_notifier(struct notifier_block *nb,
284 case PM_SUSPEND_PREPARE:
285 the_lpc->suspending = 1;
286 the_lpc->can_sleep = 1;
288 case PM_POST_SUSPEND:
289 the_lpc->suspending = 0;
290 the_lpc->can_sleep = 1;
298 static struct notifier_block n516_lpc_notif_block = {
299 .notifier_call = n516_lpc_suspend_notifier,
302 static int n516_lpc_probe(struct i2c_client *client, const struct i2c_device_id *id)
304 struct n516_lpc_chip *chip;
305 struct input_dev *input;
309 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
314 chip->i2c_client = client;
315 if ((batt_level > 0) && (batt_level < ARRAY_SIZE(batt_charge)))
316 chip->battery_level = batt_level;
317 i2c_set_clientdata(client, chip);
319 ret = gpio_request(GPIO_LPC_INT, "LPC interrupt request");
321 dev_err(&client->dev, "Unable to reguest LPC INT GPIO\n");
322 goto err_gpio_req_lpcint;
325 ret = gpio_request(GPIO_CHARG_STAT_N, "LPC interrupt request");
327 dev_err(&client->dev, "Unable to reguest CHARG STAT GPIO\n");
328 goto err_gpio_req_chargstat;
331 n516_lpc_set_normal_mode(chip);
333 input = input_allocate_device();
335 dev_err(&client->dev, "Unable to allocate input device\n");
337 goto err_input_alloc;
342 __set_bit(EV_KEY, input->evbit);
344 for (i = 0; i < ARRAY_SIZE(n516_lpc_keymap); i++)
345 __set_bit(n516_lpc_keymap[i], input->keybit);
347 __set_bit(KEY_LEFTALT, input->keybit);
349 input->name = "n516-keys";
350 input->phys = "n516-keys/input0";
351 input->dev.parent = &client->dev;
352 input->id.bustype = BUS_I2C;
353 input->id.vendor = 0x0001;
354 input->id.product = 0x0001;
355 input->id.version = 0x0100;
357 ret = input_register_device(input);
359 dev_err(&client->dev, "Unable to register input device\n");
360 goto err_input_register;
363 ret = power_supply_register(NULL, &n516_battery);
365 dev_err(&client->dev, "Unable to register N516 battery\n");
369 if (n516_bat_usb_connected() && !n516_bat_charging())
370 the_lpc->battery_level = MAX_BAT_LEVEL;
372 ret = request_threaded_irq(gpio_to_irq(GPIO_LPC_INT), NULL, n516_lpc_irq,
373 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lpc", chip);
375 dev_err(&client->dev, "request_irq failed: %d\n", ret);
376 goto err_request_lpc_irq;
379 ret = request_irq(gpio_to_irq(GPIO_CHARG_STAT_N), n516_bat_charge_irq,
380 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "battery charging", &n516_battery);
382 dev_err(&client->dev, "Unable to claim battery charging IRQ\n");
383 goto err_request_chrg_irq;
386 pm_power_off = n516_lpc_power_off;
387 ret = register_pm_notifier(&n516_lpc_notif_block);
389 dev_err(&client->dev, "Unable to register PM notify block\n");
390 goto err_reg_pm_notifier;
393 device_init_wakeup(&client->dev, 1);
397 unregister_pm_notifier(&n516_lpc_notif_block);
399 free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
400 err_request_chrg_irq:
401 free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
403 power_supply_unregister(&n516_battery);
405 input_unregister_device(input);
407 input_free_device(input);
409 gpio_free(GPIO_CHARG_STAT_N);
410 err_gpio_req_chargstat:
411 gpio_free(GPIO_LPC_INT);
413 i2c_set_clientdata(client, NULL);
419 static int n516_lpc_remove(struct i2c_client *client)
421 struct n516_lpc_chip *chip = i2c_get_clientdata(client);
423 unregister_pm_notifier(&n516_lpc_notif_block);
425 free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
426 free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
427 power_supply_unregister(&n516_battery);
428 input_unregister_device(chip->input);
429 gpio_free(GPIO_CHARG_STAT_N);
430 gpio_free(GPIO_LPC_INT);
431 i2c_set_clientdata(client, NULL);
438 static int n516_lpc_suspend(struct i2c_client *client, pm_message_t msg)
440 if (!the_lpc->can_sleep)
443 if (device_may_wakeup(&client->dev))
444 enable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
449 static int n516_lpc_resume(struct i2c_client *client)
451 if (device_may_wakeup(&client->dev))
452 disable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
457 #define n516_lpc_suspend NULL
458 #define n516_lpc_resume NULL
461 static struct i2c_driver n516_lpc_driver = {
462 .class = I2C_CLASS_HWMON,
465 .owner = THIS_MODULE,
467 .probe = n516_lpc_probe,
468 .remove = __devexit_p(n516_lpc_remove),
469 .detect = n516_lpc_detect,
470 .id_table = n516_lpc_i2c_ids,
471 .address_data = &addr_data,
472 .suspend = n516_lpc_suspend,
473 .resume = n516_lpc_resume,
476 static int n516_lpc_init(void)
478 return i2c_add_driver(&n516_lpc_driver);
480 module_init(n516_lpc_init);
482 static void n516_lpc_exit(void)
484 i2c_del_driver(&n516_lpc_driver);
486 module_exit(n516_lpc_exit);
488 MODULE_AUTHOR("Yauhen Kharuzhy");
489 MODULE_LICENSE("GPL");
490 MODULE_DESCRIPTION("Keys and power controller driver for N516");
491 MODULE_ALIAS("platform:n516-keys");