[brcm63xx] add required exports for wl.ko
[openwrt.git] / target / linux / xburst / files-2.6.32 / drivers / i2c / chips / n516-lpc.c
1 #include <linux/module.h>
2 #include <linux/version.h>
3 #include <linux/init.h>
4 #include <linux/fs.h>
5 #include <linux/interrupt.h>
6 #include <linux/irq.h>
7 #include <linux/sched.h>
8 #include <linux/pm.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>
16
17 #include <linux/i2c.h>
18
19 #include <asm/mach-jz4740/irq.h>
20 #include <asm/mach-jz4740/gpio.h>
21 #include <asm/mach-jz4740/board-n516.h>
22
23 static int batt_level=0;
24 module_param(batt_level, int, 0);
25
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;
31 };
32
33 static struct n516_lpc_chip *the_lpc;
34
35 struct i2c_device_id n516_lpc_i2c_ids[] = {
36 {"LPC524", 0},
37 {},
38 };
39
40 MODULE_DEVICE_TABLE(i2c, n516_lpc_i2c_ids);
41
42 static const unsigned short normal_i2c[] = {0x54, I2C_CLIENT_END};
43
44 static const unsigned int n516_lpc_keymap[] = {
45 [0x01] = KEY_4,
46 [0x02] = KEY_3,
47 [0x03] = KEY_2,
48 [0x04] = KEY_1,
49 [0x05] = KEY_0,
50 [0x07] = KEY_9,
51 [0x08] = KEY_8,
52 [0x09] = KEY_7,
53 [0x0a] = KEY_6,
54 [0x0b] = KEY_5,
55 [0x0d] = KEY_PLAYPAUSE,
56 [0x0e] = KEY_MENU,
57 [0x0f] = KEY_SEARCH,
58 [0x10] = KEY_DIRECTION,
59 [0x11] = KEY_SPACE,
60 [0x13] = KEY_ENTER,
61 [0x14] = KEY_UP,
62 [0x15] = KEY_DOWN,
63 [0x16] = KEY_RIGHT,
64 [0x17] = KEY_LEFT,
65 [0x19] = KEY_PAGEDOWN,
66 [0x1a] = KEY_PAGEUP,
67 [0x1c] = KEY_POWER,
68 [0x1d] = KEY_ESC,
69 [0x1e] = KEY_SLEEP,
70 [0x1f] = KEY_WAKEUP,
71 };
72
73 static const unsigned int batt_charge[] = {0, 7, 20, 45, 65, 80, 100};
74 #define MAX_BAT_LEVEL 6
75
76 /* Insmod parameters */
77 I2C_CLIENT_INSMOD_1(n516_lpc);
78
79 static inline int n516_bat_charging(void)
80 {
81 return !gpio_get_value(GPIO_CHARG_STAT_N);
82 }
83
84 static int n516_bat_get_status(struct power_supply *b)
85 {
86 if (power_supply_am_i_supplied(b)) {
87 if (n516_bat_charging())
88 return POWER_SUPPLY_STATUS_CHARGING;
89 else
90 return POWER_SUPPLY_STATUS_FULL;
91 } else {
92 return POWER_SUPPLY_STATUS_DISCHARGING;
93 }
94 }
95
96 static int n516_bat_get_charge(struct power_supply *b)
97 {
98 return batt_charge[the_lpc->battery_level];
99 }
100
101 static int n516_bat_get_property(struct power_supply *b,
102 enum power_supply_property psp,
103 union power_supply_propval *val)
104 {
105 switch (psp) {
106 case POWER_SUPPLY_PROP_STATUS:
107 val->intval = n516_bat_get_status(b);
108 break;
109 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
110 val->intval = 100;
111 break;
112 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
113 val->intval = 0;
114 break;
115 case POWER_SUPPLY_PROP_CHARGE_NOW:
116 val->intval = n516_bat_get_charge(b);
117 break;
118 default:
119 return -EINVAL;
120 }
121 return 0;
122 }
123
124 static void n516_bat_power_changed(struct power_supply *p)
125 {
126 if (power_supply_am_i_supplied(p) && !n516_bat_charging())
127 the_lpc->battery_level = MAX_BAT_LEVEL;
128
129 power_supply_changed(p);
130 }
131
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,
137 };
138
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,
145 };
146
147 static irqreturn_t n516_bat_charge_irq(int irq, void *dev)
148 {
149 struct power_supply *psy = dev;
150
151 dev_dbg(psy->dev, "Battery charging IRQ\n");
152
153 if (power_supply_am_i_supplied(psy) && !n516_bat_charging())
154 the_lpc->battery_level = MAX_BAT_LEVEL;
155
156 power_supply_changed(psy);
157
158 return IRQ_HANDLED;
159 }
160
161 static int n516_lpc_send_message(struct n516_lpc_chip *chip, unsigned char val)
162 {
163 struct i2c_client *client = chip->i2c_client;
164 struct i2c_msg msg = {client->addr, client->flags, 1, &val};
165 int ret = 0;
166
167 ret = i2c_transfer(client->adapter, &msg, 1);
168 return ret > 0 ? 0 : ret;
169 }
170
171 static void n516_key_event(struct n516_lpc_chip *chip, unsigned char keycode)
172 {
173 struct i2c_client *client = chip->i2c_client;
174 bool long_press = false;
175
176 if (keycode & 0x40) {
177 keycode &= ~0x40;
178 long_press = true;
179 }
180
181 dev_dbg(&client->dev, "keycode: 0x%02x, long_press: 0x%02x\n", keycode, (unsigned int)long_press);
182
183 if (keycode >= ARRAY_SIZE(n516_lpc_keymap) || n516_lpc_keymap[keycode] == 0)
184 return;
185
186 if (long_press)
187 input_report_key(chip->input, KEY_LEFTALT, 1);
188
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);
192
193 if (long_press)
194 input_report_key(chip->input, KEY_LEFTALT, 0);
195 input_sync(chip->input);
196 }
197
198 static void n516_battery_event(struct n516_lpc_chip *chip, unsigned char battery_level)
199 {
200 if (battery_level != chip->battery_level) {
201 chip->battery_level = battery_level;
202 power_supply_changed(&n516_battery);
203 }
204 }
205
206 static irqreturn_t n516_lpc_irq_thread(int irq, void *devid)
207 {
208 struct n516_lpc_chip *chip = (struct n516_lpc_chip*)devid;
209 int ret;
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};
213
214 if (client->dev.power.status >= DPM_OFF)
215 return IRQ_HANDLED;
216
217 ret = i2c_transfer(client->adapter, &msg, 1);
218 if (ret != 1) {
219 dev_dbg(&client->dev, "I2C error: %d\n", ret);
220 return IRQ_HANDLED;
221 }
222
223 dev_dbg(&client->dev, "msg: 0x%02x\n", raw_msg);
224
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);
232 else
233 dev_warn(&client->dev, "Unknown message: %x\n", raw_msg);
234
235 if (chip->suspending)
236 chip->can_sleep = 0;
237
238 return IRQ_HANDLED;
239 }
240
241 static void n516_lpc_power_off(void)
242 {
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};
246
247 printk("Issue LPC POWEROFF command...\n");
248 while (1)
249 i2c_transfer(client->adapter, &msg, 1);
250 }
251
252 static int n516_lpc_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
253 {
254 return 0;
255 }
256
257 static int n516_lpc_suspend_notifier(struct notifier_block *nb,
258 unsigned long event,
259 void *dummy)
260 {
261 switch(event) {
262 case PM_SUSPEND_PREPARE:
263 the_lpc->suspending = 1;
264 the_lpc->can_sleep = 1;
265 break;
266 case PM_POST_SUSPEND:
267 the_lpc->suspending = 0;
268 the_lpc->can_sleep = 1;
269 break;
270 default:
271 return NOTIFY_DONE;
272 }
273 return NOTIFY_OK;
274 }
275
276 static struct notifier_block n516_lpc_notif_block = {
277 .notifier_call = n516_lpc_suspend_notifier,
278 };
279
280 static int __devinit n516_lpc_probe(struct i2c_client *client, const struct i2c_device_id *id)
281 {
282 struct n516_lpc_chip *chip;
283 struct input_dev *input;
284 int ret = 0;
285 int i;
286
287 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
288 if (!chip)
289 return -ENOMEM;
290
291 the_lpc = chip;
292 chip->i2c_client = client;
293 if ((batt_level > 0) && (batt_level < ARRAY_SIZE(batt_charge)))
294 chip->battery_level = batt_level;
295 else
296 chip->battery_level = 1;
297
298 i2c_set_clientdata(client, chip);
299
300 ret = gpio_request(GPIO_LPC_INT, "LPC interrupt request");
301 if (ret) {
302 dev_err(&client->dev, "Unable to reguest LPC INT GPIO\n");
303 goto err_gpio_req_lpcint;
304 }
305
306 ret = gpio_request(GPIO_CHARG_STAT_N, "LPC charging status");
307 if (ret) {
308 dev_err(&client->dev, "Unable to reguest CHARG STAT GPIO\n");
309 goto err_gpio_req_chargstat;
310 }
311
312 /* Enter normal mode */
313 n516_lpc_send_message(chip, 0x2);
314
315 input = input_allocate_device();
316 if (!input) {
317 dev_err(&client->dev, "Unable to allocate input device\n");
318 ret = -ENOMEM;
319 goto err_input_alloc;
320 }
321
322 chip->input = input;
323
324 __set_bit(EV_KEY, input->evbit);
325
326 for (i = 0; i < ARRAY_SIZE(n516_lpc_keymap); i++)
327 __set_bit(n516_lpc_keymap[i], input->keybit);
328
329 __set_bit(KEY_LEFTALT, input->keybit);
330
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;
338
339 ret = input_register_device(input);
340 if (ret < 0) {
341 dev_err(&client->dev, "Unable to register input device\n");
342 goto err_input_register;
343 }
344
345 ret = power_supply_register(NULL, &n516_battery);
346 if (ret) {
347 dev_err(&client->dev, "Unable to register N516 battery\n");
348 goto err_bat_reg;
349 }
350
351 ret = request_threaded_irq(gpio_to_irq(GPIO_LPC_INT), NULL,
352 n516_lpc_irq_thread,
353 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
354 "lpc", chip);
355 if (ret) {
356 dev_err(&client->dev, "request_irq failed: %d\n", ret);
357 goto err_request_lpc_irq;
358 }
359
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);
363 if (ret) {
364 dev_err(&client->dev, "Unable to claim battery charging IRQ\n");
365 goto err_request_chrg_irq;
366 }
367
368 pm_power_off = n516_lpc_power_off;
369 ret = register_pm_notifier(&n516_lpc_notif_block);
370 if (ret) {
371 dev_err(&client->dev, "Unable to register PM notify block\n");
372 goto err_reg_pm_notifier;
373 }
374
375 device_init_wakeup(&client->dev, 1);
376
377 return 0;
378
379 unregister_pm_notifier(&n516_lpc_notif_block);
380 err_reg_pm_notifier:
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);
384 err_request_lpc_irq:
385 power_supply_unregister(&n516_battery);
386 err_bat_reg:
387 input_unregister_device(input);
388 err_input_register:
389 input_free_device(input);
390 err_input_alloc:
391 gpio_free(GPIO_CHARG_STAT_N);
392 err_gpio_req_chargstat:
393 gpio_free(GPIO_LPC_INT);
394 err_gpio_req_lpcint:
395 i2c_set_clientdata(client, NULL);
396 kfree(chip);
397
398 return ret;
399 }
400
401 static int __devexit n516_lpc_remove(struct i2c_client *client)
402 {
403 struct n516_lpc_chip *chip = i2c_get_clientdata(client);
404
405 unregister_pm_notifier(&n516_lpc_notif_block);
406 pm_power_off = NULL;
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);
414 kfree(chip);
415
416 return 0;
417 }
418
419 #if CONFIG_PM
420 static int n516_lpc_suspend(struct i2c_client *client, pm_message_t msg)
421 {
422 if (!the_lpc->can_sleep)
423 return -EBUSY;
424
425 if (device_may_wakeup(&client->dev))
426 enable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
427
428 return 0;
429 }
430
431 static int n516_lpc_resume(struct i2c_client *client)
432 {
433 if (device_may_wakeup(&client->dev))
434 disable_irq_wake(gpio_to_irq(GPIO_LPC_INT));
435
436 return 0;
437 }
438 #else
439 #define n516_lpc_suspend NULL
440 #define n516_lpc_resume NULL
441 #endif
442
443
444 static struct i2c_driver n516_lpc_driver = {
445 .class = I2C_CLASS_HWMON,
446 .driver = {
447 .name = "n516-keys",
448 .owner = THIS_MODULE,
449 },
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,
457 };
458
459 static int __init n516_lpc_init(void)
460 {
461 return i2c_add_driver(&n516_lpc_driver);
462 }
463
464 static void __exit n516_lpc_exit(void)
465 {
466 i2c_del_driver(&n516_lpc_driver);
467 }
468
469
470 module_init(n516_lpc_init);
471 module_exit(n516_lpc_exit);
472
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");
477
This page took 0.068786 seconds and 5 git commands to generate.