1 #include <linux/module.h>
2 #include <linux/version.h>
3 #include <linux/init.h>
5 #include <linux/interrupt.h>
7 #include <linux/sched.h>
9 #include <linux/sysctl.h>
10 #include <linux/proc_fs.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/input.h>
14 #include <linux/power_supply.h>
15 #include <linux/suspend.h>
17 #include <linux/i2c.h>
19 #include <asm/mach-jz4740/irq.h>
20 #include <asm/mach-jz4740/gpio.h>
21 #include <asm/mach-jz4740/board-n516.h>
23 static int batt_level=0;
24 module_param(batt_level, int, 0);
26 struct n516_lpc_chip {
27 struct i2c_client *i2c_client;
28 struct input_dev *input;
29 unsigned int battery_level;
30 unsigned int suspending:1, can_sleep:1;
33 static struct n516_lpc_chip *the_lpc;
35 struct i2c_device_id n516_lpc_i2c_ids[] = {
40 MODULE_DEVICE_TABLE(i2c, n516_lpc_i2c_ids);
42 static const unsigned short normal_i2c[] = {0x54, I2C_CLIENT_END};
44 static const unsigned int n516_lpc_keymap[] = {
55 [0x0d] = KEY_PLAYPAUSE,
58 [0x10] = KEY_DIRECTION,
65 [0x19] = KEY_PAGEDOWN,
73 static const unsigned int batt_charge[] = {0, 7, 20, 45, 65, 80, 100};
74 #define MAX_BAT_LEVEL 6
76 /* Insmod parameters */
77 I2C_CLIENT_INSMOD_1(n516_lpc);
79 static inline int n516_bat_charging(void)
81 return !gpio_get_value(GPIO_CHARG_STAT_N);
84 static int n516_bat_get_status(struct power_supply *b)
86 if (power_supply_am_i_supplied(b)) {
87 if (n516_bat_charging())
88 return POWER_SUPPLY_STATUS_CHARGING;
90 return POWER_SUPPLY_STATUS_FULL;
92 return POWER_SUPPLY_STATUS_DISCHARGING;
96 static int n516_bat_get_charge(struct power_supply *b)
98 return batt_charge[the_lpc->battery_level];
101 static int n516_bat_get_property(struct power_supply *b,
102 enum power_supply_property psp,
103 union power_supply_propval *val)
106 case POWER_SUPPLY_PROP_STATUS:
107 val->intval = n516_bat_get_status(b);
109 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
112 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
115 case POWER_SUPPLY_PROP_CHARGE_NOW:
116 val->intval = n516_bat_get_charge(b);
124 static void n516_bat_power_changed(struct power_supply *p)
126 if (power_supply_am_i_supplied(p) && !n516_bat_charging())
127 the_lpc->battery_level = MAX_BAT_LEVEL;
129 power_supply_changed(p);
132 static enum power_supply_property n516_bat_properties[] = {
133 POWER_SUPPLY_PROP_STATUS,
134 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
135 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
136 POWER_SUPPLY_PROP_CHARGE_NOW,
139 static struct power_supply n516_battery = {
140 .name = "n516-battery",
141 .get_property = n516_bat_get_property,
142 .properties = n516_bat_properties,
143 .num_properties = ARRAY_SIZE(n516_bat_properties),
144 .external_power_changed = n516_bat_power_changed,
147 static irqreturn_t n516_bat_charge_irq(int irq, void *dev)
149 struct power_supply *psy = dev;
151 dev_dbg(psy->dev, "Battery charging IRQ\n");
153 if (power_supply_am_i_supplied(psy) && !n516_bat_charging())
154 the_lpc->battery_level = MAX_BAT_LEVEL;
156 power_supply_changed(psy);
161 static int n516_lpc_send_message(struct n516_lpc_chip *chip, unsigned char val)
163 struct i2c_client *client = chip->i2c_client;
164 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
167 ret = i2c_transfer(client->adapter, &msg, 1);
168 return ret > 0 ? 0 : ret;
171 static void n516_key_event(struct n516_lpc_chip *chip, unsigned char keycode)
173 struct i2c_client *client = chip->i2c_client;
174 bool long_press = false;
176 if (keycode & 0x40) {
181 dev_dbg(&client->dev, "keycode: 0x%02x, long_press: 0x%02x\n", keycode, (unsigned int)long_press);
183 if (keycode >= ARRAY_SIZE(n516_lpc_keymap) || n516_lpc_keymap[keycode] == 0)
187 input_report_key(chip->input, KEY_LEFTALT, 1);
189 input_report_key(chip->input, n516_lpc_keymap[keycode], 1);
190 input_sync(chip->input);
191 input_report_key(chip->input, n516_lpc_keymap[keycode], 0);
194 input_report_key(chip->input, KEY_LEFTALT, 0);
195 input_sync(chip->input);
198 static void n516_battery_event(struct n516_lpc_chip *chip, unsigned char battery_level)
200 if (battery_level != chip->battery_level) {
201 chip->battery_level = battery_level;
202 power_supply_changed(&n516_battery);
206 static irqreturn_t n516_lpc_irq_thread(int irq, void *devid)
208 struct n516_lpc_chip *chip = (struct n516_lpc_chip*)devid;
210 unsigned char raw_msg;
211 struct i2c_client *client = chip->i2c_client;
212 struct i2c_msg msg = {client->addr, client->flags | I2C_M_RD, 1, &raw_msg};
214 if (client->dev.power.status >= DPM_OFF)
217 ret = i2c_transfer(client->adapter, &msg, 1);
219 dev_dbg(&client->dev, "I2C error: %d\n", ret);
223 dev_dbg(&client->dev, "msg: 0x%02x\n", raw_msg);
225 /* Ack wakeup event */
226 if ((raw_msg & ~0x40) < ARRAY_SIZE(n516_lpc_keymap))
227 n516_key_event(chip, raw_msg);
228 else if ((raw_msg >= 0x81) && (raw_msg <= 0x87))
229 n516_battery_event(chip, raw_msg - 0x81);
230 else if (raw_msg == 0x7e)
231 n516_lpc_send_message(chip, 0x00);
233 dev_warn(&client->dev, "Unknown message: %x\n", raw_msg);
235 if (chip->suspending)
241 static void n516_lpc_power_off(void)
243 struct i2c_client *client = the_lpc->i2c_client;
244 unsigned char val = 0x01;
245 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
247 printk("Issue LPC POWEROFF command...\n");
249 i2c_transfer(client->adapter, &msg, 1);
252 static int n516_lpc_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
257 static int n516_lpc_suspend_notifier(struct notifier_block *nb,
262 case PM_SUSPEND_PREPARE:
263 the_lpc->suspending = 1;
264 the_lpc->can_sleep = 1;
266 case PM_POST_SUSPEND:
267 the_lpc->suspending = 0;
268 the_lpc->can_sleep = 1;
276 static struct notifier_block n516_lpc_notif_block = {
277 .notifier_call = n516_lpc_suspend_notifier,
280 static int __devinit n516_lpc_probe(struct i2c_client *client, const struct i2c_device_id *id)
282 struct n516_lpc_chip *chip;
283 struct input_dev *input;
287 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
292 chip->i2c_client = client;
293 if ((batt_level > 0) && (batt_level < ARRAY_SIZE(batt_charge)))
294 chip->battery_level = batt_level;
296 chip->battery_level = 1;
298 i2c_set_clientdata(client, chip);
300 ret = gpio_request(GPIO_LPC_INT, "LPC interrupt request");
302 dev_err(&client->dev, "Unable to reguest LPC INT GPIO\n");
303 goto err_gpio_req_lpcint;
306 ret = gpio_request(GPIO_CHARG_STAT_N, "LPC charging status");
308 dev_err(&client->dev, "Unable to reguest CHARG STAT GPIO\n");
309 goto err_gpio_req_chargstat;
312 /* Enter normal mode */
313 n516_lpc_send_message(chip, 0x2);
315 input = input_allocate_device();
317 dev_err(&client->dev, "Unable to allocate input device\n");
319 goto err_input_alloc;
324 __set_bit(EV_KEY, input->evbit);
326 for (i = 0; i < ARRAY_SIZE(n516_lpc_keymap); i++)
327 __set_bit(n516_lpc_keymap[i], input->keybit);
329 __set_bit(KEY_LEFTALT, input->keybit);
331 input->name = "n516-keys";
332 input->phys = "n516-keys/input0";
333 input->dev.parent = &client->dev;
334 input->id.bustype = BUS_I2C;
335 input->id.vendor = 0x0001;
336 input->id.product = 0x0001;
337 input->id.version = 0x0100;
339 ret = input_register_device(input);
341 dev_err(&client->dev, "Unable to register input device\n");
342 goto err_input_register;
345 ret = power_supply_register(NULL, &n516_battery);
347 dev_err(&client->dev, "Unable to register N516 battery\n");
351 ret = request_threaded_irq(gpio_to_irq(GPIO_LPC_INT), NULL,
353 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
356 dev_err(&client->dev, "request_irq failed: %d\n", ret);
357 goto err_request_lpc_irq;
360 ret = request_irq(gpio_to_irq(GPIO_CHARG_STAT_N), n516_bat_charge_irq,
361 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
362 "battery charging", &n516_battery);
364 dev_err(&client->dev, "Unable to claim battery charging IRQ\n");
365 goto err_request_chrg_irq;
368 pm_power_off = n516_lpc_power_off;
369 ret = register_pm_notifier(&n516_lpc_notif_block);
371 dev_err(&client->dev, "Unable to register PM notify block\n");
372 goto err_reg_pm_notifier;
375 device_init_wakeup(&client->dev, 1);
379 unregister_pm_notifier(&n516_lpc_notif_block);
381 free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
382 err_request_chrg_irq:
383 free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
385 power_supply_unregister(&n516_battery);
387 input_unregister_device(input);
389 input_free_device(input);
391 gpio_free(GPIO_CHARG_STAT_N);
392 err_gpio_req_chargstat:
393 gpio_free(GPIO_LPC_INT);
395 i2c_set_clientdata(client, NULL);
401 static int __devexit n516_lpc_remove(struct i2c_client *client)
403 struct n516_lpc_chip *chip = i2c_get_clientdata(client);
405 unregister_pm_notifier(&n516_lpc_notif_block);
407 free_irq(gpio_to_irq(GPIO_CHARG_STAT_N), &n516_battery);
408 free_irq(gpio_to_irq(GPIO_LPC_INT), chip);
409 power_supply_unregister(&n516_battery);
410 input_unregister_device(chip->input);
411 gpio_free(GPIO_CHARG_STAT_N);
412 gpio_free(GPIO_LPC_INT);
413 i2c_set_clientdata(client, NULL);
420 static int n516_lpc_suspend(struct i2c_client *client, pm_message_t msg)
422 if (!the_lpc->can_sleep)
425 if (device_may_wakeup(&client->dev))
426 enable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
431 static int n516_lpc_resume(struct i2c_client *client)
433 if (device_may_wakeup(&client->dev))
434 disable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
439 #define n516_lpc_suspend NULL
440 #define n516_lpc_resume NULL
444 static struct i2c_driver n516_lpc_driver = {
445 .class = I2C_CLASS_HWMON,
448 .owner = THIS_MODULE,
450 .probe = n516_lpc_probe,
451 .remove = __devexit_p(n516_lpc_remove),
452 .detect = n516_lpc_detect,
453 .id_table = n516_lpc_i2c_ids,
454 .address_data = &addr_data,
455 .suspend = n516_lpc_suspend,
456 .resume = n516_lpc_resume,
459 static int __init n516_lpc_init(void)
461 return i2c_add_driver(&n516_lpc_driver);
464 static void __exit n516_lpc_exit(void)
466 i2c_del_driver(&n516_lpc_driver);
470 module_init(n516_lpc_init);
471 module_exit(n516_lpc_exit);
473 MODULE_AUTHOR("Yauhen Kharuzhy");
474 MODULE_LICENSE("GPL");
475 MODULE_DESCRIPTION("Keys and power controller driver for N516");
476 MODULE_ALIAS("platform:n516-keys");