ARM: S3C2410: H1940: Add keys device
[pandora-kernel.git] / arch / arm / mach-s3c2410 / mach-h1940.c
1 /* linux/arch/arm/mach-s3c2410/mach-h1940.c
2  *
3  * Copyright (c) 2003-2005 Simtec Electronics
4  *   Ben Dooks <ben@simtec.co.uk>
5  *
6  * http://www.handhelds.org/projects/h1940.html
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/memblock.h>
19 #include <linux/timer.h>
20 #include <linux/init.h>
21 #include <linux/sysdev.h>
22 #include <linux/serial_core.h>
23 #include <linux/platform_device.h>
24 #include <linux/io.h>
25 #include <linux/gpio.h>
26 #include <linux/input.h>
27 #include <linux/gpio_keys.h>
28 #include <linux/pwm_backlight.h>
29 #include <linux/i2c.h>
30 #include <video/platform_lcd.h>
31
32 #include <linux/mmc/host.h>
33
34 #include <asm/mach/arch.h>
35 #include <asm/mach/map.h>
36 #include <asm/mach/irq.h>
37
38 #include <mach/hardware.h>
39 #include <asm/irq.h>
40 #include <asm/mach-types.h>
41
42 #include <plat/regs-serial.h>
43 #include <mach/regs-lcd.h>
44 #include <mach/regs-clock.h>
45
46 #include <mach/regs-gpio.h>
47 #include <mach/gpio-fns.h>
48 #include <mach/gpio-nrs.h>
49
50 #include <mach/h1940.h>
51 #include <mach/h1940-latch.h>
52 #include <mach/fb.h>
53 #include <plat/udc.h>
54 #include <plat/iic.h>
55
56 #include <plat/gpio-cfg.h>
57 #include <plat/clock.h>
58 #include <plat/devs.h>
59 #include <plat/cpu.h>
60 #include <plat/pll.h>
61 #include <plat/pm.h>
62 #include <plat/mci.h>
63 #include <plat/ts.h>
64
65 #include <sound/uda1380.h>
66
67 #define H1940_LATCH             ((void __force __iomem *)0xF8000000)
68
69 #define H1940_PA_LATCH          S3C2410_CS2
70
71 #define H1940_LATCH_BIT(x)      (1 << ((x) + 16 - S3C_GPIO_END))
72
73 static struct map_desc h1940_iodesc[] __initdata = {
74         [0] = {
75                 .virtual        = (unsigned long)H1940_LATCH,
76                 .pfn            = __phys_to_pfn(H1940_PA_LATCH),
77                 .length         = SZ_16K,
78                 .type           = MT_DEVICE
79         },
80 };
81
82 #define UCON S3C2410_UCON_DEFAULT | S3C2410_UCON_UCLK
83 #define ULCON S3C2410_LCON_CS8 | S3C2410_LCON_PNONE | S3C2410_LCON_STOPB
84 #define UFCON S3C2410_UFCON_RXTRIG8 | S3C2410_UFCON_FIFOMODE
85
86 static struct s3c2410_uartcfg h1940_uartcfgs[] __initdata = {
87         [0] = {
88                 .hwport      = 0,
89                 .flags       = 0,
90                 .ucon        = 0x3c5,
91                 .ulcon       = 0x03,
92                 .ufcon       = 0x51,
93         },
94         [1] = {
95                 .hwport      = 1,
96                 .flags       = 0,
97                 .ucon        = 0x245,
98                 .ulcon       = 0x03,
99                 .ufcon       = 0x00,
100         },
101         /* IR port */
102         [2] = {
103                 .hwport      = 2,
104                 .flags       = 0,
105                 .uart_flags  = UPF_CONS_FLOW,
106                 .ucon        = 0x3c5,
107                 .ulcon       = 0x43,
108                 .ufcon       = 0x51,
109         }
110 };
111
112 /* Board control latch control */
113
114 static unsigned int latch_state;
115
116 static void h1940_latch_control(unsigned int clear, unsigned int set)
117 {
118         unsigned long flags;
119
120         local_irq_save(flags);
121
122         latch_state &= ~clear;
123         latch_state |= set;
124
125         __raw_writel(latch_state, H1940_LATCH);
126
127         local_irq_restore(flags);
128 }
129
130 static inline int h1940_gpiolib_to_latch(int offset)
131 {
132         return 1 << (offset + 16);
133 }
134
135 static void h1940_gpiolib_latch_set(struct gpio_chip *chip,
136                                         unsigned offset, int value)
137 {
138         int latch_bit = h1940_gpiolib_to_latch(offset);
139
140         h1940_latch_control(value ? 0 : latch_bit,
141                 value ? latch_bit : 0);
142 }
143
144 static int h1940_gpiolib_latch_output(struct gpio_chip *chip,
145                                         unsigned offset, int value)
146 {
147         h1940_gpiolib_latch_set(chip, offset, value);
148         return 0;
149 }
150
151 static int h1940_gpiolib_latch_get(struct gpio_chip *chip,
152                                         unsigned offset)
153 {
154         return (latch_state >> (offset + 16)) & 1;
155 }
156
157 struct gpio_chip h1940_latch_gpiochip = {
158         .base                   = H1940_LATCH_GPIO(0),
159         .owner                  = THIS_MODULE,
160         .label                  = "H1940_LATCH",
161         .ngpio                  = 16,
162         .direction_output       = h1940_gpiolib_latch_output,
163         .set                    = h1940_gpiolib_latch_set,
164         .get                    = h1940_gpiolib_latch_get,
165 };
166
167 static struct s3c2410_udc_mach_info h1940_udc_cfg __initdata = {
168         .vbus_pin               = S3C2410_GPG(5),
169         .vbus_pin_inverted      = 1,
170         .pullup_pin             = H1940_LATCH_USB_DP,
171 };
172
173 static struct s3c2410_ts_mach_info h1940_ts_cfg __initdata = {
174                 .delay = 10000,
175                 .presc = 49,
176                 .oversampling_shift = 2,
177                 .cfg_gpio = s3c24xx_ts_cfg_gpio,
178 };
179
180 /**
181  * Set lcd on or off
182  **/
183 static struct s3c2410fb_display h1940_lcd __initdata = {
184         .lcdcon5=       S3C2410_LCDCON5_FRM565 | \
185                         S3C2410_LCDCON5_INVVLINE | \
186                         S3C2410_LCDCON5_HWSWP,
187
188         .type =         S3C2410_LCDCON1_TFT,
189         .width =        240,
190         .height =       320,
191         .pixclock =     260000,
192         .xres =         240,
193         .yres =         320,
194         .bpp =          16,
195         .left_margin =  8,
196         .right_margin = 20,
197         .hsync_len =    4,
198         .upper_margin = 8,
199         .lower_margin = 7,
200         .vsync_len =    1,
201 };
202
203 static struct s3c2410fb_mach_info h1940_fb_info __initdata = {
204         .displays = &h1940_lcd,
205         .num_displays = 1,
206         .default_display = 0,
207
208         .lpcsel=        0x02,
209         .gpccon=        0xaa940659,
210         .gpccon_mask=   0xffffffff,
211         .gpcup=         0x0000ffff,
212         .gpcup_mask=    0xffffffff,
213         .gpdcon=        0xaa84aaa0,
214         .gpdcon_mask=   0xffffffff,
215         .gpdup=         0x0000faff,
216         .gpdup_mask=    0xffffffff,
217 };
218
219 static struct platform_device h1940_device_leds = {
220         .name             = "h1940-leds",
221         .id               = -1,
222 };
223
224 static struct platform_device h1940_device_bluetooth = {
225         .name             = "h1940-bt",
226         .id               = -1,
227 };
228
229 static void h1940_set_mmc_power(unsigned char power_mode, unsigned short vdd)
230 {
231         switch (power_mode) {
232         case MMC_POWER_OFF:
233                 gpio_set_value(H1940_LATCH_SD_POWER, 0);
234                 break;
235         case MMC_POWER_UP:
236         case MMC_POWER_ON:
237                 gpio_set_value(H1940_LATCH_SD_POWER, 1);
238                 break;
239         default:
240                 break;
241         };
242 }
243
244 static struct s3c24xx_mci_pdata h1940_mmc_cfg __initdata = {
245         .gpio_detect   = S3C2410_GPF(5),
246         .gpio_wprotect = S3C2410_GPH(8),
247         .set_power     = h1940_set_mmc_power,
248         .ocr_avail     = MMC_VDD_32_33,
249 };
250
251 static int h1940_backlight_init(struct device *dev)
252 {
253         gpio_request(S3C2410_GPB(0), "Backlight");
254
255         gpio_direction_output(S3C2410_GPB(0), 0);
256         s3c_gpio_setpull(S3C2410_GPB(0), S3C_GPIO_PULL_NONE);
257         s3c_gpio_cfgpin(S3C2410_GPB(0), S3C2410_GPB0_TOUT0);
258         gpio_set_value(H1940_LATCH_MAX1698_nSHUTDOWN, 1);
259
260         return 0;
261 }
262
263 static int h1940_backlight_notify(struct device *dev, int brightness)
264 {
265         if (!brightness) {
266                 gpio_direction_output(S3C2410_GPB(0), 1);
267                 gpio_set_value(H1940_LATCH_MAX1698_nSHUTDOWN, 0);
268         } else {
269                 gpio_direction_output(S3C2410_GPB(0), 0);
270                 s3c_gpio_setpull(S3C2410_GPB(0), S3C_GPIO_PULL_NONE);
271                 s3c_gpio_cfgpin(S3C2410_GPB(0), S3C2410_GPB0_TOUT0);
272                 gpio_set_value(H1940_LATCH_MAX1698_nSHUTDOWN, 1);
273         }
274         return brightness;
275 }
276
277 static void h1940_backlight_exit(struct device *dev)
278 {
279         gpio_direction_output(S3C2410_GPB(0), 1);
280         gpio_set_value(H1940_LATCH_MAX1698_nSHUTDOWN, 0);
281 }
282
283
284 static struct platform_pwm_backlight_data backlight_data = {
285         .pwm_id         = 0,
286         .max_brightness = 100,
287         .dft_brightness = 50,
288         /* tcnt = 0x31 */
289         .pwm_period_ns  = 36296,
290         .init           = h1940_backlight_init,
291         .notify         = h1940_backlight_notify,
292         .exit           = h1940_backlight_exit,
293 };
294
295 static struct platform_device h1940_backlight = {
296         .name = "pwm-backlight",
297         .dev  = {
298                 .parent = &s3c_device_timer[0].dev,
299                 .platform_data = &backlight_data,
300         },
301         .id   = -1,
302 };
303
304 static void h1940_lcd_power_set(struct plat_lcd_data *pd,
305                                         unsigned int power)
306 {
307         int value;
308
309         if (!power) {
310                 gpio_set_value(S3C2410_GPC(0), 0);
311                 /* wait for 3ac */
312                 do {
313                         value = gpio_get_value(S3C2410_GPC(6));
314                 } while (value);
315
316                 gpio_set_value(H1940_LATCH_LCD_P2, 0);
317                 gpio_set_value(H1940_LATCH_LCD_P3, 0);
318                 gpio_set_value(H1940_LATCH_LCD_P4, 0);
319
320                 gpio_direction_output(S3C2410_GPC(1), 0);
321                 gpio_direction_output(S3C2410_GPC(4), 0);
322
323                 gpio_set_value(H1940_LATCH_LCD_P1, 0);
324                 gpio_set_value(H1940_LATCH_LCD_P0, 0);
325
326                 gpio_set_value(S3C2410_GPC(5), 0);
327
328         } else {
329                 gpio_set_value(H1940_LATCH_LCD_P0, 1);
330                 gpio_set_value(H1940_LATCH_LCD_P1, 1);
331
332                 s3c_gpio_cfgpin(S3C2410_GPC(1), S3C_GPIO_SFN(2));
333                 s3c_gpio_cfgpin(S3C2410_GPC(4), S3C_GPIO_SFN(2));
334
335                 gpio_set_value(S3C2410_GPC(5), 1);
336                 gpio_set_value(S3C2410_GPC(0), 1);
337
338                 gpio_set_value(H1940_LATCH_LCD_P3, 1);
339                 gpio_set_value(H1940_LATCH_LCD_P2, 1);
340                 gpio_set_value(H1940_LATCH_LCD_P4, 1);
341         }
342 }
343
344 static struct plat_lcd_data h1940_lcd_power_data = {
345         .set_power      = h1940_lcd_power_set,
346 };
347
348 static struct platform_device h1940_lcd_powerdev = {
349         .name                   = "platform-lcd",
350         .dev.parent             = &s3c_device_lcd.dev,
351         .dev.platform_data      = &h1940_lcd_power_data,
352 };
353
354 static struct uda1380_platform_data uda1380_info = {
355         .gpio_power     = H1940_LATCH_UDA_POWER,
356         .gpio_reset     = S3C2410_GPA(12),
357         .dac_clk        = UDA1380_DAC_CLK_SYSCLK,
358 };
359
360 static struct i2c_board_info h1940_i2c_devices[] = {
361         {
362                 I2C_BOARD_INFO("uda1380", 0x1a),
363                 .platform_data = &uda1380_info,
364         },
365 };
366
367 #define DECLARE_BUTTON(p, k, n, w)      \
368         {                               \
369                 .gpio           = p,    \
370                 .code           = k,    \
371                 .desc           = n,    \
372                 .wakeup         = w,    \
373                 .active_low     = 1,    \
374         }
375
376 static struct gpio_keys_button h1940_buttons[] = {
377         DECLARE_BUTTON(S3C2410_GPF(0),       KEY_POWER,          "Power", 1),
378         DECLARE_BUTTON(S3C2410_GPF(6),       KEY_ENTER,         "Select", 1),
379         DECLARE_BUTTON(S3C2410_GPF(7),      KEY_RECORD,         "Record", 0),
380         DECLARE_BUTTON(S3C2410_GPG(0),         KEY_F11,       "Calendar", 0),
381         DECLARE_BUTTON(S3C2410_GPG(2),         KEY_F12,       "Contacts", 0),
382         DECLARE_BUTTON(S3C2410_GPG(3),        KEY_MAIL,           "Mail", 0),
383         DECLARE_BUTTON(S3C2410_GPG(6),        KEY_LEFT,     "Left_arrow", 0),
384         DECLARE_BUTTON(S3C2410_GPG(7),    KEY_HOMEPAGE,           "Home", 0),
385         DECLARE_BUTTON(S3C2410_GPG(8),       KEY_RIGHT,    "Right_arrow", 0),
386         DECLARE_BUTTON(S3C2410_GPG(9),          KEY_UP,       "Up_arrow", 0),
387         DECLARE_BUTTON(S3C2410_GPG(10),       KEY_DOWN,     "Down_arrow", 0),
388 };
389
390 static struct gpio_keys_platform_data h1940_buttons_data = {
391         .buttons        = h1940_buttons,
392         .nbuttons       = ARRAY_SIZE(h1940_buttons),
393 };
394
395 static struct platform_device h1940_dev_buttons = {
396         .name           = "gpio-keys",
397         .id             = -1,
398         .dev            = {
399                 .platform_data  = &h1940_buttons_data,
400         }
401 };
402
403 static struct platform_device *h1940_devices[] __initdata = {
404         &h1940_dev_buttons,
405         &s3c_device_ohci,
406         &s3c_device_lcd,
407         &s3c_device_wdt,
408         &s3c_device_i2c0,
409         &s3c_device_iis,
410         &samsung_asoc_dma,
411         &s3c_device_usbgadget,
412         &h1940_device_leds,
413         &h1940_device_bluetooth,
414         &s3c_device_sdi,
415         &s3c_device_rtc,
416         &s3c_device_timer[0],
417         &h1940_backlight,
418         &h1940_lcd_powerdev,
419         &s3c_device_adc,
420         &s3c_device_ts,
421 };
422
423 static void __init h1940_map_io(void)
424 {
425         s3c24xx_init_io(h1940_iodesc, ARRAY_SIZE(h1940_iodesc));
426         s3c24xx_init_clocks(0);
427         s3c24xx_init_uarts(h1940_uartcfgs, ARRAY_SIZE(h1940_uartcfgs));
428
429         /* setup PM */
430
431 #ifdef CONFIG_PM_H1940
432         memcpy(phys_to_virt(H1940_SUSPEND_RESUMEAT), h1940_pm_return, 1024);
433 #endif
434         s3c_pm_init();
435
436         /* Add latch gpio chip, set latch initial value */
437         h1940_latch_control(0, 0);
438         WARN_ON(gpiochip_add(&h1940_latch_gpiochip));
439 }
440
441 /* H1940 and RX3715 need to reserve this for suspend */
442 static void __init h1940_reserve(void)
443 {
444         memblock_reserve(0x30003000, 0x1000);
445         memblock_reserve(0x30081000, 0x1000);
446 }
447
448 static void __init h1940_init_irq(void)
449 {
450         s3c24xx_init_irq();
451 }
452
453 static void __init h1940_init(void)
454 {
455         u32 tmp;
456
457         s3c24xx_fb_set_platdata(&h1940_fb_info);
458         s3c24xx_mci_set_platdata(&h1940_mmc_cfg);
459         s3c24xx_udc_set_platdata(&h1940_udc_cfg);
460         s3c24xx_ts_set_platdata(&h1940_ts_cfg);
461         s3c_i2c0_set_platdata(NULL);
462
463         /* Turn off suspend on both USB ports, and switch the
464          * selectable USB port to USB device mode. */
465
466         s3c2410_modify_misccr(S3C2410_MISCCR_USBHOST |
467                               S3C2410_MISCCR_USBSUSPND0 |
468                               S3C2410_MISCCR_USBSUSPND1, 0x0);
469
470         tmp =   (0x78 << S3C24XX_PLLCON_MDIVSHIFT)
471               | (0x02 << S3C24XX_PLLCON_PDIVSHIFT)
472               | (0x03 << S3C24XX_PLLCON_SDIVSHIFT);
473         writel(tmp, S3C2410_UPLLCON);
474
475         gpio_request(S3C2410_GPC(0), "LCD power");
476         gpio_request(S3C2410_GPC(1), "LCD power");
477         gpio_request(S3C2410_GPC(4), "LCD power");
478         gpio_request(S3C2410_GPC(5), "LCD power");
479         gpio_request(S3C2410_GPC(6), "LCD power");
480         gpio_request(H1940_LATCH_LCD_P0, "LCD power");
481         gpio_request(H1940_LATCH_LCD_P1, "LCD power");
482         gpio_request(H1940_LATCH_LCD_P2, "LCD power");
483         gpio_request(H1940_LATCH_LCD_P3, "LCD power");
484         gpio_request(H1940_LATCH_LCD_P4, "LCD power");
485         gpio_request(H1940_LATCH_MAX1698_nSHUTDOWN, "LCD power");
486         gpio_direction_output(S3C2410_GPC(0), 0);
487         gpio_direction_output(S3C2410_GPC(1), 0);
488         gpio_direction_output(S3C2410_GPC(4), 0);
489         gpio_direction_output(S3C2410_GPC(5), 0);
490         gpio_direction_input(S3C2410_GPC(6));
491         gpio_direction_output(H1940_LATCH_LCD_P0, 0);
492         gpio_direction_output(H1940_LATCH_LCD_P1, 0);
493         gpio_direction_output(H1940_LATCH_LCD_P2, 0);
494         gpio_direction_output(H1940_LATCH_LCD_P3, 0);
495         gpio_direction_output(H1940_LATCH_LCD_P4, 0);
496         gpio_direction_output(H1940_LATCH_MAX1698_nSHUTDOWN, 0);
497
498         gpio_request(H1940_LATCH_SD_POWER, "SD power");
499         gpio_direction_output(H1940_LATCH_SD_POWER, 0);
500
501         platform_add_devices(h1940_devices, ARRAY_SIZE(h1940_devices));
502
503         i2c_register_board_info(0, h1940_i2c_devices,
504                 ARRAY_SIZE(h1940_i2c_devices));
505 }
506
507 MACHINE_START(H1940, "IPAQ-H1940")
508         /* Maintainer: Ben Dooks <ben-linux@fluff.org> */
509         .boot_params    = S3C2410_SDRAM_PA + 0x100,
510         .map_io         = h1940_map_io,
511         .reserve        = h1940_reserve,
512         .init_irq       = h1940_init_irq,
513         .init_machine   = h1940_init,
514         .timer          = &s3c24xx_timer,
515 MACHINE_END