[xburst] Add support for the n516
[openwrt/svn-archive/archive.git] / target / linux / xburst / files-2.6.32 / drivers / i2c / chips / n516-lpc.c
1 /*
2 * board-n516-display.c -- Platform device for N516 display
3 *
4 * Copyright (C) 2009, Yauhen Kharuzhy <jekhor@gmail.com>
5 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
6 *
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
9 * more details.
10 */
11
12
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/init.h>
16 #include <linux/fs.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/sched.h>
20 #include <linux/pm.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>
28
29 #include <linux/i2c.h>
30
31 #include <asm/mach-jz4740/irq.h>
32 #include <asm/mach-jz4740/gpio.h>
33 #include <asm/mach-jz4740/board-n516.h>
34
35
36 static int batt_level=0;
37 module_param(batt_level, int, 0);
38
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;
44 };
45
46 static struct n516_lpc_chip *the_lpc;
47
48 struct i2c_device_id n516_lpc_i2c_ids[] = {
49 {"LPC524", 0},
50 {},
51 };
52
53 MODULE_DEVICE_TABLE(i2c, n516_lpc_i2c_ids);
54
55 static const unsigned short normal_i2c[] = {0x54, I2C_CLIENT_END};
56
57 static const unsigned int n516_lpc_keymap[] = {
58 [0x01] = KEY_4,
59 [0x02] = KEY_3,
60 [0x03] = KEY_2,
61 [0x04] = KEY_1,
62 [0x05] = KEY_0,
63 [0x07] = KEY_9,
64 [0x08] = KEY_8,
65 [0x09] = KEY_7,
66 [0x0a] = KEY_6,
67 [0x0b] = KEY_5,
68 [0x0d] = KEY_PLAYPAUSE,
69 [0x0e] = KEY_MENU,
70 [0x0f] = KEY_SEARCH,
71 [0x10] = KEY_DIRECTION,
72 [0x11] = KEY_SPACE,
73 [0x13] = KEY_ENTER,
74 [0x14] = KEY_UP,
75 [0x15] = KEY_DOWN,
76 [0x16] = KEY_RIGHT,
77 [0x17] = KEY_LEFT,
78 [0x19] = KEY_PAGEDOWN,
79 [0x1a] = KEY_PAGEUP,
80 [0x1c] = KEY_POWER,
81 [0x1d] = KEY_ESC,
82 [0x1e] = KEY_SLEEP,
83 /* [0x1f] = KEY_WAKEUP,*/
84 };
85
86 static const unsigned int batt_charge[] = {0, 7, 20, 45, 65, 80, 100};
87 #define MAX_BAT_LEVEL (ARRAY_SIZE(batt_charge) - 1)
88
89 /* Insmod parameters */
90 I2C_CLIENT_INSMOD_1(n516_lpc);
91
92 static inline int n516_bat_usb_connected(void)
93 {
94 return !gpio_get_value(GPIO_USB_DETECT);
95 }
96
97 static inline int n516_bat_charging(void)
98 {
99 return !gpio_get_value(GPIO_CHARG_STAT_N);
100 }
101
102 static int n516_bat_get_status(struct power_supply *b)
103 {
104 if (n516_bat_usb_connected()) {
105 if (n516_bat_charging())
106 return POWER_SUPPLY_STATUS_CHARGING;
107 else
108 return POWER_SUPPLY_STATUS_FULL;
109 } else {
110 return POWER_SUPPLY_STATUS_DISCHARGING;
111 }
112 }
113
114 static int n516_bat_get_charge(struct power_supply *b)
115 {
116 return batt_charge[the_lpc->battery_level];
117 }
118
119 static int n516_bat_get_property(struct power_supply *b,
120 enum power_supply_property psp,
121 union power_supply_propval *val)
122 {
123 switch (psp) {
124 case POWER_SUPPLY_PROP_STATUS:
125 val->intval = n516_bat_get_status(b);
126 break;
127 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
128 val->intval = 100;
129 break;
130 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
131 val->intval = 0;
132 break;
133 case POWER_SUPPLY_PROP_CHARGE_NOW:
134 val->intval = n516_bat_get_charge(b);
135 break;
136 default:
137 return -EINVAL;
138 }
139 return 0;
140 }
141
142 static void n516_bat_power_changed(struct power_supply *p)
143 {
144 power_supply_changed(p);
145 }
146
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,
152 };
153
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,
160 };
161
162 static irqreturn_t n516_bat_charge_irq(int irq, void *dev)
163 {
164 struct power_supply *psy = dev;
165
166 dev_dbg(psy->dev, "Battery charging IRQ\n");
167
168 if (n516_bat_usb_connected() && !n516_bat_charging())
169 the_lpc->battery_level = MAX_BAT_LEVEL;
170
171 power_supply_changed(psy);
172
173 return IRQ_HANDLED;
174 }
175
176 static int n516_lpc_set_normal_mode(struct n516_lpc_chip *chip)
177 {
178 struct i2c_client *client = chip->i2c_client;
179 unsigned char val = 0x02;
180 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
181 int ret = 0;
182
183 ret = i2c_transfer(client->adapter, &msg, 1);
184 return ret > 0 ? 0 : ret;
185 }
186
187 static void n516_key_event(struct n516_lpc_chip *chip, unsigned char keycode)
188 {
189 struct i2c_client *client = chip->i2c_client;
190 bool long_press = false;
191
192 if (keycode & 0x40) {
193 keycode &= ~0x40;
194 long_press = true;
195 }
196
197 dev_dbg(&client->dev, "keycode: 0x%02x, long_press: 0x%02x\n", keycode, (unsigned int)long_press);
198
199 if (keycode >= ARRAY_SIZE(n516_lpc_keymap) || n516_lpc_keymap[keycode] == 0)
200 return;
201
202 if (long_press)
203 input_report_key(chip->input, KEY_LEFTALT, 1);
204
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);
208
209 if (long_press)
210 input_report_key(chip->input, KEY_LEFTALT, 0);
211 input_sync(chip->input);
212 }
213
214
215 static void n516_battery_event(struct n516_lpc_chip *chip, unsigned char battery_level)
216 {
217 if (battery_level != chip->battery_level) {
218 chip->battery_level = battery_level;
219 power_supply_changed(&n516_battery);
220 }
221 }
222
223 static irqreturn_t n516_lpc_irq(int irq, void *devid)
224 {
225 struct n516_lpc_chip *chip = (struct n516_lpc_chip*)devid;
226 int ret;
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};
230
231 ret = i2c_transfer(client->adapter, &msg, 1);
232 if (ret != 1) {
233 dev_dbg(&client->dev, "I2C error: %d\n", ret);
234 return IRQ_HANDLED;
235 }
236
237 dev_dbg(&client->dev, "msg: 0x%02x\n", raw_msg);
238
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);
243 } else {
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);
247 if (ret != 1) {
248 dev_dbg(&client->dev, "I2C error: %d\n", ret);
249 } else {
250 dev_warn(&client->dev, "Unkown message part 2: %x\n", raw_msg);
251 }
252
253 }
254
255 if (chip->suspending)
256 chip->can_sleep = 0;
257
258 printk("foobar\n");
259
260 return IRQ_HANDLED;
261 }
262
263 static void n516_lpc_power_off(void)
264 {
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};
268
269 printk("Issue LPC POWEROFF command...\n");
270 while (1)
271 i2c_transfer(client->adapter, &msg, 1);
272 }
273
274 static int n516_lpc_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
275 {
276 return 0;
277 }
278
279 static int n516_lpc_suspend_notifier(struct notifier_block *nb,
280 unsigned long event,
281 void *dummy)
282 {
283 switch(event) {
284 case PM_SUSPEND_PREPARE:
285 the_lpc->suspending = 1;
286 the_lpc->can_sleep = 1;
287 break;
288 case PM_POST_SUSPEND:
289 the_lpc->suspending = 0;
290 the_lpc->can_sleep = 1;
291 break;
292 default:
293 return NOTIFY_DONE;
294 }
295 return NOTIFY_OK;
296 }
297
298 static struct notifier_block n516_lpc_notif_block = {
299 .notifier_call = n516_lpc_suspend_notifier,
300 };
301
302 static int n516_lpc_probe(struct i2c_client *client, const struct i2c_device_id *id)
303 {
304 struct n516_lpc_chip *chip;
305 struct input_dev *input;
306 int ret = 0;
307 int i;
308
309 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
310 if (!chip)
311 return -ENOMEM;
312
313 the_lpc = chip;
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);
318
319 ret = gpio_request(GPIO_LPC_INT, "LPC interrupt request");
320 if (ret) {
321 dev_err(&client->dev, "Unable to reguest LPC INT GPIO\n");
322 goto err_gpio_req_lpcint;
323 }
324
325 ret = gpio_request(GPIO_CHARG_STAT_N, "LPC interrupt request");
326 if (ret) {
327 dev_err(&client->dev, "Unable to reguest CHARG STAT GPIO\n");
328 goto err_gpio_req_chargstat;
329 }
330
331 n516_lpc_set_normal_mode(chip);
332
333 input = input_allocate_device();
334 if (!input) {
335 dev_err(&client->dev, "Unable to allocate input device\n");
336 ret = -ENOMEM;
337 goto err_input_alloc;
338 }
339
340 chip->input = input;
341
342 __set_bit(EV_KEY, input->evbit);
343
344 for (i = 0; i < ARRAY_SIZE(n516_lpc_keymap); i++)
345 __set_bit(n516_lpc_keymap[i], input->keybit);
346
347 __set_bit(KEY_LEFTALT, input->keybit);
348
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;
356
357 ret = input_register_device(input);
358 if (ret < 0) {
359 dev_err(&client->dev, "Unable to register input device\n");
360 goto err_input_register;
361 }
362
363 ret = power_supply_register(NULL, &n516_battery);
364 if (ret) {
365 dev_err(&client->dev, "Unable to register N516 battery\n");
366 goto err_bat_reg;
367 }
368
369 if (n516_bat_usb_connected() && !n516_bat_charging())
370 the_lpc->battery_level = MAX_BAT_LEVEL;
371
372 ret = request_threaded_irq(gpio_to_irq(GPIO_LPC_INT), NULL, n516_lpc_irq,
373 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lpc", chip);
374 if (ret) {
375 dev_err(&client->dev, "request_irq failed: %d\n", ret);
376 goto err_request_lpc_irq;
377 }
378
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);
381 if (ret) {
382 dev_err(&client->dev, "Unable to claim battery charging IRQ\n");
383 goto err_request_chrg_irq;
384 }
385
386 pm_power_off = n516_lpc_power_off;
387 ret = register_pm_notifier(&n516_lpc_notif_block);
388 if (ret) {
389 dev_err(&client->dev, "Unable to register PM notify block\n");
390 goto err_reg_pm_notifier;
391 }
392
393 device_init_wakeup(&client->dev, 1);
394
395 return 0;
396
397 unregister_pm_notifier(&n516_lpc_notif_block);
398 err_reg_pm_notifier:
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);
402 err_request_lpc_irq:
403 power_supply_unregister(&n516_battery);
404 err_bat_reg:
405 input_unregister_device(input);
406 err_input_register:
407 input_free_device(input);
408 err_input_alloc:
409 gpio_free(GPIO_CHARG_STAT_N);
410 err_gpio_req_chargstat:
411 gpio_free(GPIO_LPC_INT);
412 err_gpio_req_lpcint:
413 i2c_set_clientdata(client, NULL);
414 kfree(chip);
415
416 return ret;
417 }
418
419 static int n516_lpc_remove(struct i2c_client *client)
420 {
421 struct n516_lpc_chip *chip = i2c_get_clientdata(client);
422
423 unregister_pm_notifier(&n516_lpc_notif_block);
424 pm_power_off = NULL;
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);
432 kfree(chip);
433
434 return 0;
435 }
436
437 #if CONFIG_PM
438 static int n516_lpc_suspend(struct i2c_client *client, pm_message_t msg)
439 {
440 if (!the_lpc->can_sleep)
441 return -EBUSY;
442
443 if (device_may_wakeup(&client->dev))
444 enable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
445
446 return 0;
447 }
448
449 static int n516_lpc_resume(struct i2c_client *client)
450 {
451 if (device_may_wakeup(&client->dev))
452 disable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
453
454 return 0;
455 }
456 #else
457 #define n516_lpc_suspend NULL
458 #define n516_lpc_resume NULL
459 #endif
460
461 static struct i2c_driver n516_lpc_driver = {
462 .class = I2C_CLASS_HWMON,
463 .driver = {
464 .name = "n516-keys",
465 .owner = THIS_MODULE,
466 },
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,
474 };
475
476 static int n516_lpc_init(void)
477 {
478 return i2c_add_driver(&n516_lpc_driver);
479 }
480 module_init(n516_lpc_init);
481
482 static void n516_lpc_exit(void)
483 {
484 i2c_del_driver(&n516_lpc_driver);
485 }
486 module_exit(n516_lpc_exit);
487
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");