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