b2c329167528a0139c5b3cc276e523fff82d471d
[kernel.git] / arch / arm / mach-s3c2440 / mach-gta02.c
1 /*
2  * linux/arch/arm/mach-s3c2442/mach-gta02.c
3  *
4  * S3C2442 Machine Support for Openmoko GTA02 / FreeRunner.
5  *
6  * Copyright (C) 2006-2009 by Openmoko, Inc.
7  * Authors: Harald Welte <laforge@openmoko.org>
8  *          Andy Green <andy@openmoko.org>
9  *          Werner Almesberger <werner@openmoko.org>
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  *
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/interrupt.h>
32 #include <linux/list.h>
33 #include <linux/delay.h>
34 #include <linux/timer.h>
35 #include <linux/init.h>
36 #include <linux/gpio.h>
37 #include <linux/workqueue.h>
38 #include <linux/platform_device.h>
39 #include <linux/serial_core.h>
40 #include <linux/spi/spi.h>
41 #include <linux/spi/spi_gpio.h>
42
43 #include <linux/mmc/host.h>
44
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/nand.h>
47 #include <linux/mtd/nand_ecc.h>
48 #include <linux/mtd/partitions.h>
49 #include <linux/mtd/physmap.h>
50 #include <linux/io.h>
51
52 #include <linux/i2c.h>
53 #include <linux/backlight.h>
54 #include <linux/regulator/machine.h>
55 #include <linux/regulator/fixed.h>
56
57 #include <linux/mfd/pcf50633/core.h>
58 #include <linux/mfd/pcf50633/mbc.h>
59 #include <linux/mfd/pcf50633/adc.h>
60 #include <linux/mfd/pcf50633/gpio.h>
61 #include <linux/mfd/pcf50633/pmic.h>
62
63 #include <linux/input.h>
64 #include <linux/gpio_keys.h>
65
66 #include <linux/leds.h>
67 #include <linux/leds_pwm.h>
68
69 #include <asm/mach/arch.h>
70 #include <asm/mach/map.h>
71 #include <asm/mach/irq.h>
72
73 #include <asm/irq.h>
74 #include <asm/mach-types.h>
75
76 #include <mach/regs-irq.h>
77 #include <mach/regs-gpio.h>
78 #include <mach/regs-gpioj.h>
79 #include <mach/fb.h>
80
81 #include <mach/spi.h>
82 #include <mach/spi-gpio.h>
83 #include <plat/usb-control.h>
84 #include <mach/regs-mem.h>
85 #include <mach/hardware.h>
86
87 #include <mach/gta02.h>
88
89 #include <plat/regs-serial.h>
90 #include <plat/nand.h>
91 #include <plat/devs.h>
92 #include <plat/cpu.h>
93 #include <plat/pm.h>
94 #include <plat/udc.h>
95 #include <plat/gpio-cfg.h>
96 #include <plat/gpio-core.h>
97 #include <plat/iic.h>
98
99 #include <mach/gta02-pm-gps.h>
100 #include <mach/gta02-pm-wlan.h>
101
102 #include <linux/jbt6k74.h>
103 #include <linux/glamofb.h>
104 #include <linux/mfd/glamo.h>
105
106 static struct pcf50633 *gta02_pcf;
107
108 /*
109  * This gets called every 1ms when we paniced.
110  */
111
112 static long gta02_panic_blink(long count)
113 {
114         long delay = 0;
115         static long last_blink;
116         static char led;
117
118         /* Fast blink: 200ms period. */
119         if (count - last_blink < 100)
120                 return 0;
121
122         led ^= 1;
123         gpio_direction_output(GTA02_GPIO_AUX_LED, led);
124
125         last_blink = count;
126
127         return delay;
128 }
129
130
131 static struct map_desc gta02_iodesc[] __initdata = {
132         {
133                 .virtual        = 0xe0000000,
134                 .pfn            = __phys_to_pfn(S3C2410_CS3 + 0x01000000),
135                 .length         = SZ_1M,
136                 .type           = MT_DEVICE
137         },
138 };
139
140 #define UCON (S3C2410_UCON_DEFAULT | S3C2443_UCON_RXERR_IRQEN)
141 #define ULCON (S3C2410_LCON_CS8 | S3C2410_LCON_PNONE | S3C2410_LCON_STOPB)
142 #define UFCON (S3C2410_UFCON_RXTRIG8 | S3C2410_UFCON_FIFOMODE)
143
144 static struct s3c2410_uartcfg gta02_uartcfgs[] = {
145         [0] = {
146                 .hwport         = 0,
147                 .flags          = 0,
148                 .ucon           = UCON,
149                 .ulcon          = ULCON,
150                 .ufcon          = UFCON,
151         },
152         [1] = {
153                 .hwport         = 1,
154                 .flags          = 0,
155                 .ucon           = UCON,
156                 .ulcon          = ULCON,
157                 .ufcon          = UFCON,
158         },
159         [2] = {
160                 .hwport         = 2,
161                 .flags          = 0,
162                 .ucon           = UCON,
163                 .ulcon          = ULCON,
164                 .ufcon          = UFCON,
165         },
166 };
167
168 static struct platform_device gta02_pm_bt_dev = {
169         .name = "gta02-pm-bt",
170 };
171
172 static struct platform_device gta02_pm_gps_dev = {
173         .name = "gta02-pm-gps",
174 };
175
176 static struct platform_device gta02_pm_gsm_dev = {
177         .name = "gta02-pm-gsm",
178 };
179
180 static struct platform_device gta02_pm_wlan_dev = {
181         .name = "gta02-pm-wlan",
182 };
183
184 static struct regulator_consumer_supply gsm_supply_consumer = {
185         .dev = &gta02_pm_gsm_dev.dev,
186         .supply = "GSM",
187 };
188
189 static struct regulator_init_data gsm_supply_init_data = {
190         .constraints = {
191                 .min_uV = 3700000,
192                 .max_uV = 3700000,
193                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
194                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
195         },
196         .num_consumer_supplies = 1,
197         .consumer_supplies = &gsm_supply_consumer,
198 };
199
200 static struct fixed_voltage_config gsm_supply_config = {
201         .supply_name = "GSM",
202         .microvolts = 3700000,
203         .gpio = GTA02_GPIO_PCF(PCF50633_GPIO2),
204         .enable_high = 1,
205         .init_data = &gsm_supply_init_data,
206 };
207
208 static struct platform_device gta02_gsm_supply_device = {
209         .name = "reg-fixed-voltage",
210         .id = 1,
211         .dev = {
212                 .platform_data = &gsm_supply_config,
213         },
214 };
215
216 /*
217  * we crank down SD Card clock dynamically when GPS is powered
218  */
219
220 static int gta02_glamo_mci_use_slow(void)
221 {
222         return gta02_pm_gps_is_on();
223 }
224
225 static void gta02_glamo_external_reset(int level)
226 {
227         s3c2410_gpio_setpin(GTA02_GPIO_3D_RESET, level);
228         s3c2410_gpio_cfgpin(GTA02_GPIO_3D_RESET, S3C2410_GPIO_OUTPUT);
229 }
230
231 struct spi_gpio_platform_data spigpio_platform_data = {
232         .sck = GTA02_GPIO_GLAMO(10),
233         .mosi = GTA02_GPIO_GLAMO(11),
234         .miso = GTA02_GPIO_GLAMO(5),
235         .num_chipselect = 1,
236 };
237
238 static struct platform_device spigpio_device = {
239         .name = "spi_gpio",
240         .id   = 2,
241         .dev = {
242                 .platform_data = &spigpio_platform_data,
243         },
244 };
245
246 static void gta02_glamo_registered(struct device *dev)
247 {
248         spigpio_device.dev.parent = dev;
249         platform_device_register(&spigpio_device);
250 }
251
252 static struct fb_videomode gta02_glamo_modes[] = {
253         {
254                 .name = "480x640",
255                 .xres = 480,
256                 .yres = 640,
257                 .pixclock       = 40816,
258                 .left_margin    = 8,
259                 .right_margin   = 16,
260                 .upper_margin   = 2,
261                 .lower_margin   = 16,
262                 .hsync_len      = 8,
263                 .vsync_len      = 2,
264                 .vmode = FB_VMODE_NONINTERLACED,
265         }, {
266                 .name = "240x320",
267                 .xres = 240,
268                 .yres = 320,
269                 .pixclock       = 40816,
270                 .left_margin    = 8,
271                 .right_margin   = 16,
272                 .upper_margin   = 2,
273                 .lower_margin   = 16,
274                 .hsync_len      = 8,
275                 .vsync_len      = 2,
276                 .vmode = FB_VMODE_NONINTERLACED,
277         }
278 };
279
280 static struct glamo_fb_platform_data gta02_glamo_fb_pdata = {
281         .width  = 43,
282         .height = 58,
283
284         .num_modes = ARRAY_SIZE(gta02_glamo_modes),
285         .modes = gta02_glamo_modes,
286 };
287
288 static struct glamo_mmc_platform_data gta02_glamo_mmc_pdata = {
289         .glamo_mmc_use_slow = gta02_glamo_mci_use_slow,
290 };
291
292 static struct glamo_gpio_platform_data gta02_glamo_gpio_pdata = {
293         .base = GTA02_GPIO_GLAMO_BASE,
294         .registered = gta02_glamo_registered,
295 };
296
297 static struct glamo_platform_data gta02_glamo_pdata = {
298         .fb_data    = &gta02_glamo_fb_pdata,
299         .mmc_data   = &gta02_glamo_mmc_pdata,
300         .gpio_data  = &gta02_glamo_gpio_pdata,
301
302         .osci_clock_rate = 32768,
303
304         .glamo_external_reset = gta02_glamo_external_reset,
305 };
306
307 /* JBT6k74 display controller */
308 static void gta02_jbt6k74_probe_completed(struct device *dev)
309  {
310         pcf50633_bl_set_brightness_limit(gta02_pcf, 0x3f);
311 }
312  
313 const static struct jbt6k74_platform_data jbt6k74_pdata = {
314         .gpio_reset = GTA02_GPIO_GLAMO(4),
315 };
316
317 static struct spi_board_info gta02_spi_board_info[] = {
318         {
319                 .modalias       = "jbt6k74",
320                 .platform_data  = &jbt6k74_pdata,
321                 .controller_data = (void*)GTA02_GPIO_GLAMO(12),
322                 /* irq */
323                 .max_speed_hz   = 100 * 1000,
324                 .bus_num        = 2,
325                 .chip_select = 0
326         },
327 };
328  
329 static struct resource gta02_glamo_resources[] = {
330         [0] = {
331                 .start  = S3C2410_CS1,
332                 .end    = S3C2410_CS1 + 0x1000000 - 1,
333                 .flags  = IORESOURCE_MEM,
334         },
335         [1] = {
336                 .start  = GTA02_IRQ_3D,
337                 .end    = GTA02_IRQ_3D,
338                 .flags  = IORESOURCE_IRQ,
339         },
340         [2] = {
341                 .start  = IRQ_BOARD_START,
342                 .flags  = IORESOURCE_IRQ,
343         },
344 };
345
346 static struct platform_device gta02_glamo_dev = {
347         .name           = "glamo3362",
348         .num_resources  = ARRAY_SIZE(gta02_glamo_resources),
349         .resource       = gta02_glamo_resources,
350         .dev            = {
351                 .platform_data  = &gta02_glamo_pdata,
352         },
353 };
354
355
356 #ifdef CONFIG_CHARGER_PCF50633
357 /*
358  * On GTA02 the 1A charger features a 48K resistor to 0V on the ID pin.
359  * We use this to recognize that we can pull 1A from the USB socket.
360  *
361  * These constants are the measured pcf50633 ADC levels with the 1A
362  * charger / 48K resistor, and with no pulldown resistor.
363  */
364
365 #define ADC_NOM_CHG_DETECT_1A 6
366 #define ADC_NOM_CHG_DETECT_USB 43
367
368 static void
369 gta02_configure_pmu_for_charger(struct pcf50633 *pcf, void *unused, int res)
370 {
371         int  ma;
372
373         /* Interpret charger type */
374         if (res < ((ADC_NOM_CHG_DETECT_USB + ADC_NOM_CHG_DETECT_1A) / 2)) {
375
376                 /*
377                  * Sanity - stop GPO driving out now that we have a 1A charger
378                  * GPO controls USB Host power generation on GTA02
379                  */
380                 pcf50633_gpio_set(pcf, PCF50633_GPO, 0);
381
382                 ma = 1000;
383         } else
384                 ma = 100;
385
386         pcf50633_mbc_usb_curlim_set(pcf, ma);
387 }
388
389 static struct delayed_work gta02_charger_work;
390 static int gta02_usb_vbus_draw;
391
392 static void gta02_charger_worker(struct work_struct *work)
393 {
394         if (gta02_usb_vbus_draw) {
395                 pcf50633_mbc_usb_curlim_set(gta02_pcf, gta02_usb_vbus_draw);
396                 return;
397         }
398
399 #ifdef CONFIG_PCF50633_ADC
400         pcf50633_adc_async_read(gta02_pcf,
401                                 PCF50633_ADCC1_MUX_ADCIN1,
402                                 PCF50633_ADCC1_AVERAGE_16,
403                                 gta02_configure_pmu_for_charger,
404                                 NULL);
405 #else
406         /*
407          * If the PCF50633 ADC is disabled we fallback to a
408          * 100mA limit for safety.
409          */
410         pcf50633_mbc_usb_curlim_set(pcf, 100);
411 #endif
412 }
413
414 #define GTA02_CHARGER_CONFIGURE_TIMEOUT ((3000 * HZ) / 1000)
415
416 static void gta02_pmu_event_callback(struct pcf50633 *pcf, int irq)
417 {
418         if (irq == PCF50633_IRQ_USBINS) {
419                 schedule_delayed_work(&gta02_charger_work,
420                                       GTA02_CHARGER_CONFIGURE_TIMEOUT);
421
422                 return;
423         }
424
425         if (irq == PCF50633_IRQ_USBREM) {
426                 cancel_delayed_work_sync(&gta02_charger_work);
427                 gta02_usb_vbus_draw = 0;
428         }
429 }
430
431 static void gta02_udc_vbus_draw(unsigned int ma)
432 {
433         if (!gta02_pcf)
434                 return;
435
436         gta02_usb_vbus_draw = ma;
437
438         schedule_delayed_work(&gta02_charger_work,
439                               GTA02_CHARGER_CONFIGURE_TIMEOUT);
440 }
441 #else /* !CONFIG_CHARGER_PCF50633 */
442 #define gta02_pmu_event_callback        NULL
443 #define gta02_udc_vbus_draw             NULL
444 #endif
445
446 /*
447  * This is called when pc50633 is probed, unfortunately quite late in the
448  * day since it is an I2C bus device. Here we can belatedly define some
449  * platform devices with the advantage that we can mark the pcf50633 as the
450  * parent. This makes them get suspended and resumed with their parent
451  * the pcf50633 still around.
452  */
453
454 static void gta02_pmu_attach_child_devices(struct pcf50633 *pcf);
455
456
457 static char *gta02_batteries[] = {
458         "battery",
459 };
460
461 static struct regulator_consumer_supply ldo4_consumers[] = {
462         {
463                 .dev = &gta02_pm_bt_dev.dev,
464                 .supply = "BT_3V2",
465         },
466 };
467
468 static struct regulator_consumer_supply ldo5_consumers[] = {
469         {
470                 .dev = &gta02_pm_gps_dev.dev,
471                 .supply = "RF_3V",
472         },
473 };
474
475 static struct regulator_consumer_supply ldo6_consumers[] = {
476         REGULATOR_SUPPLY("VDC", "spi2.0"),
477         REGULATOR_SUPPLY("VDDIO", "spi2.0"),
478 };
479
480 static struct regulator_consumer_supply hcldo_consumers[] = {
481         {
482                 .dev = &gta02_glamo_dev.dev,
483                 .supply = "SD_3V3",
484         },
485 };
486
487 struct pcf50633_platform_data gta02_pcf_pdata = {
488         .resumers = {
489                 [0] =   PCF50633_INT1_USBINS |
490                         PCF50633_INT1_USBREM |
491                         PCF50633_INT1_ALARM,
492                 [1] =   PCF50633_INT2_ONKEYF,
493                 [2] =   PCF50633_INT3_ONKEY1S,
494                 [3] =   PCF50633_INT4_LOWSYS |
495                         PCF50633_INT4_LOWBAT |
496                         PCF50633_INT4_HIGHTMP,
497         },
498
499         .batteries = gta02_batteries,
500         .num_batteries = ARRAY_SIZE(gta02_batteries),
501
502         .charger_reference_current_ma = 1000,
503
504         .gpio_base = GTA02_GPIO_PCF_BASE,
505
506         .reg_init_data = {
507                 [PCF50633_REGULATOR_AUTO] = {
508                         .constraints = {
509                                 .min_uV = 3300000,
510                                 .max_uV = 3300000,
511                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
512                                 .always_on = 1,
513                                 .apply_uV = 1,
514                                 .state_mem = {
515                                         .enabled = 1,
516                                 },
517                         },
518                 },
519                 [PCF50633_REGULATOR_DOWN1] = {
520                         .constraints = {
521                                 .min_uV = 1300000,
522                                 .max_uV = 1600000,
523                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
524                                 .always_on = 1,
525                                 .apply_uV = 1,
526                         },
527                 },
528                 [PCF50633_REGULATOR_DOWN2] = {
529                         .constraints = {
530                                 .min_uV = 1800000,
531                                 .max_uV = 1800000,
532                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
533                                 .apply_uV = 1,
534                                 .always_on = 1,
535                                 .state_mem = {
536                                         .enabled = 1,
537                                 },
538                         },
539                 },
540                 [PCF50633_REGULATOR_HCLDO] = {
541                         .constraints = {
542                                 .min_uV = 2000000,
543                                 .max_uV = 3300000,
544                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
545                                 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE | 
546                                                 REGULATOR_CHANGE_STATUS,
547                         },
548                         .num_consumer_supplies = ARRAY_SIZE(hcldo_consumers),
549                         .consumer_supplies = hcldo_consumers,
550                 },
551                 [PCF50633_REGULATOR_LDO1] = {
552                         .constraints = {
553                                 .min_uV = 3300000,
554                                 .max_uV = 3300000,
555                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
556                                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
557                                 .apply_uV = 1,
558                                 .state_mem = {
559                                         .enabled = 0,
560                                 },
561                         },
562                 },
563                 [PCF50633_REGULATOR_LDO2] = {
564                         .constraints = {
565                                 .min_uV = 3300000,
566                                 .max_uV = 3300000,
567                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
568                                 .apply_uV = 1,
569                         },
570                 },
571                 [PCF50633_REGULATOR_LDO3] = {
572                         .constraints = {
573                                 .min_uV = 3000000,
574                                 .max_uV = 3000000,
575                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
576                                 .apply_uV = 1,
577                         },
578                 },
579                 [PCF50633_REGULATOR_LDO4] = {
580                         .constraints = {
581                                 .min_uV = 3200000,
582                                 .max_uV = 3200000,
583                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
584                                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
585                                 .apply_uV = 1,
586                         },
587                         .num_consumer_supplies = ARRAY_SIZE(ldo4_consumers),
588                         .consumer_supplies = ldo4_consumers,
589                 },
590                 [PCF50633_REGULATOR_LDO5] = {
591                         .constraints = {
592                                 .min_uV = 3000000,
593                                 .max_uV = 3000000,
594                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
595                                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
596                                 .apply_uV = 1,
597                                 .state_mem = {
598                                         .enabled = 1,
599                                 },
600                         },
601                         .num_consumer_supplies = ARRAY_SIZE(ldo5_consumers),
602                         .consumer_supplies = ldo5_consumers,
603                 },
604                 [PCF50633_REGULATOR_LDO6] = {
605                         .constraints = {
606                                 .min_uV = 3000000,
607                                 .max_uV = 3000000,
608                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
609                                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
610                         },
611                         .num_consumer_supplies = ARRAY_SIZE(ldo6_consumers),
612                         .consumer_supplies = ldo6_consumers,
613                 },
614                 [PCF50633_REGULATOR_MEMLDO] = {
615                         .constraints = {
616                                 .min_uV = 1800000,
617                                 .max_uV = 1800000,
618                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
619                                 .state_mem = {
620                                         .enabled = 1,
621                                 },
622                         },
623                 },
624
625         },
626         .probe_done = gta02_pmu_attach_child_devices,
627         .mbc_event_callback = gta02_pmu_event_callback,
628 };
629
630
631 /* NOR Flash. */
632
633 #define GTA02_FLASH_BASE        0x18000000 /* GCS3 */
634 #define GTA02_FLASH_SIZE        0x200000 /* 2MBytes */
635
636 static struct physmap_flash_data gta02_nor_flash_data = {
637         .width          = 2,
638 };
639
640 static struct resource gta02_nor_flash_resource = {
641         .start          = GTA02_FLASH_BASE,
642         .end            = GTA02_FLASH_BASE + GTA02_FLASH_SIZE - 1,
643         .flags          = IORESOURCE_MEM,
644 };
645
646 static struct platform_device gta02_nor_flash = {
647         .name           = "physmap-flash",
648         .id             = 0,
649         .dev            = {
650                 .platform_data  = &gta02_nor_flash_data,
651         },
652         .resource       = &gta02_nor_flash_resource,
653         .num_resources  = 1,
654 };
655
656 static struct i2c_board_info gta02_i2c_devs[] __initdata = {
657         {
658                 I2C_BOARD_INFO("pcf50633", 0x73),
659                 .irq = GTA02_IRQ_PCF50633,
660                 .platform_data = &gta02_pcf_pdata,
661         },
662         {
663                 I2C_BOARD_INFO("wm8753", 0x1a),
664         },
665 };
666
667 static struct s3c2410_nand_set __initdata gta02_nand_sets[] = {
668         [0] = {
669                 /*
670                  * This name is also hard-coded in the boot loaders, so
671                  * changing it would would require all users to upgrade
672                  * their boot loaders, some of which are stored in a NOR
673                  * that is considered to be immutable.
674                  */
675                 .name           = "neo1973-nand",
676                 .nr_chips       = 1,
677                 .flash_bbt      = 1,
678         },
679 };
680
681 /*
682  * Choose a set of timings derived from S3C@2442B MCP54
683  * data sheet (K5D2G13ACM-D075 MCP Memory).
684  */
685
686 static struct s3c2410_platform_nand __initdata gta02_nand_info = {
687         .tacls          = 0,
688         .twrph0         = 25,
689         .twrph1         = 15,
690         .nr_sets        = ARRAY_SIZE(gta02_nand_sets),
691         .sets           = gta02_nand_sets,
692         .software_ecc   = 1,
693 };
694
695
696 static void gta02_udc_command(enum s3c2410_udc_cmd_e cmd)
697 {
698         switch (cmd) {
699         case S3C2410_UDC_P_ENABLE:
700                 pr_debug("%s S3C2410_UDC_P_ENABLE\n", __func__);
701                 gpio_set_value(GTA02_GPIO_USB_PULLUP, 1);
702                 break;
703         case S3C2410_UDC_P_DISABLE:
704                 pr_debug("%s S3C2410_UDC_P_DISABLE\n", __func__);
705                 gpio_set_value(GTA02_GPIO_USB_PULLUP, 0);
706                 break;
707         case S3C2410_UDC_P_RESET:
708                 pr_debug("%s S3C2410_UDC_P_RESET\n", __func__);
709                 /* FIXME: Do something here. */
710         }
711 }
712
713 /* Get PMU to set USB current limit accordingly. */
714 static struct s3c2410_udc_mach_info gta02_udc_cfg = {
715         .vbus_draw      = gta02_udc_vbus_draw,
716         .udc_command    = gta02_udc_command,
717
718 };
719
720
721
722 static void gta02_bl_set_intensity(int intensity)
723 {
724         struct pcf50633 *pcf = gta02_pcf;
725         int old_intensity = pcf50633_reg_read(pcf, PCF50633_REG_LEDOUT);
726
727         /* We map 8-bit intensity to 6-bit intensity in hardware. */
728         intensity >>= 2;
729
730         /*
731          * This can happen during, eg, print of panic on blanked console,
732          * but we can't service i2c without interrupts active, so abort.
733          */
734         if (in_atomic()) {
735                 printk(KERN_ERR "gta02_bl_set_intensity called while atomic\n");
736                 return;
737         }
738
739         old_intensity = pcf50633_reg_read(pcf, PCF50633_REG_LEDOUT);
740         if (intensity == old_intensity)
741                 return;
742
743         /* We can't do this anywhere else. */
744         pcf50633_reg_write(pcf, PCF50633_REG_LEDDIM, 5);
745
746         if (!(pcf50633_reg_read(pcf, PCF50633_REG_LEDENA) & 3))
747                 old_intensity = 0;
748
749         /*
750          * The PCF50633 cannot handle LEDOUT = 0 (datasheet p60)
751          * if seen, you have to re-enable the LED unit.
752          */
753         if (!intensity || !old_intensity)
754                 pcf50633_reg_write(pcf, PCF50633_REG_LEDENA, 0);
755
756         /* Illegal to set LEDOUT to 0. */
757         if (!intensity)
758                 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f, 2);
759         else
760                 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f,
761                                           intensity);
762
763         if (intensity)
764                 pcf50633_reg_write(pcf, PCF50633_REG_LEDENA, 2);
765
766 }
767
768 static struct generic_bl_info gta02_bl_info = {
769         .name                   = "gta02-bl",
770         .max_intensity          = 0xff,
771         .default_intensity      = 0xff,
772         .set_bl_intensity       = gta02_bl_set_intensity,
773 };
774
775 static struct platform_device gta02_bl_dev = {
776         .name                   = "generic-bl",
777         .id                     = 1,
778         .dev = {
779                 .platform_data = &gta02_bl_info,
780         },
781 };
782
783
784
785 /* USB */
786 static struct s3c2410_hcd_info gta02_usb_info __initdata = {
787         .port[0]        = {
788                 .flags  = S3C_HCDFLG_USED,
789         },
790         .port[1]        = {
791                 .flags  = 0,
792         },
793 };
794
795 /* Buttons */
796 static struct gpio_keys_button gta02_buttons[] = {
797         {
798                 .gpio = GTA02_GPIO_AUX_KEY,
799                 .code = KEY_PHONE,
800                 .desc = "Aux",
801                 .type = EV_KEY,
802                 .debounce_interval = 100,
803         },
804         {
805                 .gpio = GTA02_GPIO_HOLD_KEY,
806                 .code = KEY_PAUSE,
807                 .desc = "Hold",
808                 .type = EV_KEY,
809                 .debounce_interval = 100,
810         },
811 };
812
813 static struct gpio_keys_platform_data gta02_buttons_pdata = {
814         .buttons = gta02_buttons,
815         .nbuttons = ARRAY_SIZE(gta02_buttons),
816 };
817
818 static struct platform_device gta02_buttons_device = {
819         .name = "gpio-keys",
820         .id = -1,
821         .dev = {
822                 .platform_data = &gta02_buttons_pdata,
823         },
824 };
825
826 /* LEDs */
827 static struct gpio_led gta02_gpio_leds[] = {
828         {
829                 .name   = "gta02:red:aux",
830                 .gpio   = GTA02_GPIO_AUX_LED,
831         },
832 };
833
834 static struct gpio_led_platform_data gta02_gpio_leds_pdata = {
835         .leds = gta02_gpio_leds,
836         .num_leds = ARRAY_SIZE(gta02_gpio_leds),
837 };
838
839 static struct platform_device gta02_leds_device = {
840         .name   = "leds-gpio",
841         .id             = -1,
842         .dev = {
843                 .platform_data = &gta02_gpio_leds_pdata,
844         },
845 };
846
847 static inline int gta02_pwm_to_gpio(int pwm_id)
848 {
849         return S3C2410_GPB(pwm_id);
850 }
851
852 static int gta02_pwm_led_init(struct device *dev, struct led_pwm *led)
853 {
854         int ret;
855         int gpio = gta02_pwm_to_gpio(led->pwm_id);
856
857         ret = gpio_request(gpio, dev_name(dev));
858         if (ret)
859                 return ret;
860
861         gpio_direction_output(gpio, 0);
862
863         return 0;
864 }
865
866 static enum led_brightness gta02_pwm_led_notify(struct device *dev,
867         struct led_pwm *led, enum led_brightness brightness)
868 {
869         int gpio = gta02_pwm_to_gpio(led->pwm_id);
870
871         if (brightness == led->max_brightness || brightness == 0) {
872                 s3c2410_gpio_cfgpin(gpio, S3C2410_GPIO_OUTPUT);
873                 gpio_set_value(gpio, brightness ? 1 : 0);
874
875                 brightness = 0;
876         } else {
877                 s3c2410_gpio_cfgpin(gpio, S3C2410_GPIO_SFN2);
878         }
879
880         return brightness;
881 }
882
883 static void gta02_pwm_led_exit(struct device *dev, struct led_pwm *led)
884 {
885         gpio_free(gta02_pwm_to_gpio(led->pwm_id));
886 }
887
888 static struct led_pwm gta02_pwm_leds[] = {
889         {
890                 .name = "gta02:orange:power",
891                 .max_brightness = 0xff,
892                 .pwm_period_ns = 1000000,
893                 .pwm_id = 0,
894         },
895         {
896                 .name = "gta02:blue:power",
897                 .max_brightness = 0xff,
898                 .pwm_period_ns = 1000000,
899                 .pwm_id = 1,
900         },
901         {
902                 .name = "gta02::vibrator",
903                 .max_brightness = 0x3f,
904                 .pwm_period_ns = 60000000,
905                 .pwm_id = 3,
906         }
907 };
908
909 static struct led_pwm_platform_data gta02_pwm_leds_pdata = {
910         .num_leds = ARRAY_SIZE(gta02_pwm_leds),
911         .leds = gta02_pwm_leds,
912
913         .init = gta02_pwm_led_init,
914         .notify = gta02_pwm_led_notify,
915         .exit = gta02_pwm_led_exit,
916 };
917
918 static struct platform_device gta02_pwm_leds_device = {
919         .name   = "leds_pwm",
920         .id     = -1,
921         .dev = {
922                 .platform_data = &gta02_pwm_leds_pdata,
923         }
924 };
925
926 static void __init gta02_map_io(void)
927 {
928         s3c24xx_init_io(gta02_iodesc, ARRAY_SIZE(gta02_iodesc));
929         s3c24xx_init_clocks(12000000);
930         s3c24xx_init_uarts(gta02_uartcfgs, ARRAY_SIZE(gta02_uartcfgs));
931 }
932
933
934 /* These are the guys that don't need to be children of PMU. */
935
936 static struct platform_device *gta02_devices[] __initdata = {
937         &s3c_device_ohci,
938         &s3c_device_wdt,
939         &s3c_device_sdi,
940         &s3c_device_usbgadget,
941         &s3c_device_nand,
942         &gta02_nor_flash,
943         &s3c_device_timer[0],
944         &s3c_device_timer[1],
945         &s3c_device_timer[3],
946         &s3c_device_iis,
947         &s3c_device_i2c0,
948         &gta02_buttons_device,
949         &gta02_leds_device,
950         &gta02_pwm_leds_device,
951         &gta02_pm_gps_dev,
952         &gta02_pm_bt_dev,
953         &gta02_pm_wlan_dev,
954         &gta02_glamo_dev,
955 };
956
957 /* These guys DO need to be children of PMU. */
958
959 static struct platform_device *gta02_devices_pmu_children[] = {
960         &gta02_bl_dev,
961 };
962
963
964 /*
965  * This is called when pc50633 is probed, quite late in the day since it is an
966  * I2C bus device.  Here we can define platform devices with the advantage that
967  * we can mark the pcf50633 as the parent.  This makes them get suspended and
968  * resumed with their parent the pcf50633 still around.  All devices whose
969  * operation depends on something from pcf50633 must have this relationship
970  * made explicit like this, or suspend and resume will become an unreliable
971  * hellworld.
972  */
973
974 static void gta02_pmu_attach_child_devices(struct pcf50633 *pcf)
975 {
976         int n;
977
978         /* Grab a copy of the now probed PMU pointer. */
979         gta02_pcf = pcf;
980
981         for (n = 0; n < ARRAY_SIZE(gta02_devices_pmu_children); n++)
982                 gta02_devices_pmu_children[n]->dev.parent = pcf->dev;
983
984         platform_add_devices(gta02_devices_pmu_children,
985                              ARRAY_SIZE(gta02_devices_pmu_children));
986 }
987
988 static void gta02_poweroff(void)
989 {
990         pcf50633_reg_set_bit_mask(gta02_pcf, PCF50633_REG_OOCSHDWN, 1, 1);
991 }
992
993 struct gta02_device_children {
994         const char *dev_name;
995         size_t num_children;
996         struct platform_device **children;
997         void (*probed_callback)(struct device *dev);
998 };
999
1000 static struct platform_device* gta02_pcf50633_gpio_children[] = {
1001         &gta02_gsm_supply_device,
1002 };
1003
1004 static struct platform_device* gta02_gsm_supply_children[] = {
1005         &gta02_pm_gsm_dev,
1006 };
1007
1008 static struct gta02_device_children gta02_device_children[] = {
1009         {
1010                 .dev_name = "pcf50633-gpio",
1011                 .num_children = 1,
1012                 .children = gta02_pcf50633_gpio_children,
1013         },
1014         {
1015                 .dev_name = "reg-fixed-voltage.1",
1016                 .num_children = 1,
1017                 .children = gta02_gsm_supply_children,
1018         },
1019         {
1020                 .dev_name = "spi2.0",
1021                 .probed_callback = gta02_jbt6k74_probe_completed,
1022         },
1023 };
1024
1025 static int gta02_add_child_devices(struct device *parent,
1026                                    struct platform_device **children,
1027                                                                    size_t num_children)
1028 {
1029         size_t i;
1030
1031         for (i = 0; i < num_children; ++i)
1032                 children[i]->dev.parent = parent;
1033
1034         return platform_add_devices(children, num_children);
1035 }
1036
1037 static int gta02_device_registered(struct notifier_block *block,
1038                                    unsigned long action, void *data)
1039 {
1040         struct device *dev = data;
1041         const char *devname = dev_name(dev);
1042         size_t i;
1043
1044         if (action != BUS_NOTIFY_BOUND_DRIVER)
1045                 return 0;
1046
1047         for (i = 0; i < ARRAY_SIZE(gta02_device_children); ++i) {
1048                 if (strcmp(devname, gta02_device_children[i].dev_name) == 0) {
1049                         gta02_add_child_devices(dev, gta02_device_children[i].children,
1050                         gta02_device_children[i].num_children);
1051
1052                         if (gta02_device_children[i].probed_callback)
1053                                 gta02_device_children[i].probed_callback(dev);
1054                         break;
1055                 }
1056         }
1057
1058         return 0;
1059 }
1060
1061 static struct notifier_block gta02_device_register_notifier = {
1062         .notifier_call = gta02_device_registered,
1063         .priority = INT_MAX,
1064 };
1065
1066
1067 /* On hardware rev 5 and earlier the leds are missing a resistor and reading
1068  * from their gpio pins will always return 0, so we have to shadow the
1069  * led states software */
1070 static unsigned long gpb_shadow;
1071 extern struct s3c_gpio_chip s3c24xx_gpios[];
1072
1073 static void gta02_gpb_set(struct gpio_chip *chip,
1074                                 unsigned offset, int value)
1075 {
1076         void __iomem *base = S3C24XX_GPIO_BASE(S3C2410_GPB(0));
1077         unsigned long flags;
1078         unsigned long dat;
1079
1080         local_irq_save(flags);
1081
1082         dat = __raw_readl(base + 0x04) | gpb_shadow;
1083         dat &= ~(1 << offset);
1084         gpb_shadow &= ~(1 << offset);
1085         if (value) {
1086                 dat |= 1 << offset;
1087                 switch (offset) {
1088                 case 0 ... 2:
1089                         gpb_shadow |= 1 << offset;
1090                         break;
1091                 default:
1092                         break;
1093                 }
1094         }
1095         __raw_writel(dat, base + 0x04);
1096
1097         local_irq_restore(flags);
1098 }
1099
1100 static int gta02_gpb_get(struct gpio_chip *chip, unsigned offset)
1101 {
1102         void __iomem *base = S3C24XX_GPIO_BASE(S3C2410_GPB(0));
1103         unsigned long val;
1104
1105         val = __raw_readl(base + 0x04) | gpb_shadow;
1106         val >>= offset;
1107         val &= 1;
1108
1109         return val;
1110 }
1111
1112 static void gta02_hijack_gpb(void)
1113 {
1114 /* Uncomment this, once support for S3C_SYSTEM_REV_ATAG has been merged
1115  * upstream.
1116         if (S3C_SYSTEM_REV_ATAG > GTA02v5_SYSTEM_REV)
1117                 return;
1118 */
1119
1120         s3c24xx_gpios[1].chip.set = gta02_gpb_set;
1121         s3c24xx_gpios[1].chip.get = gta02_gpb_get;
1122 }
1123
1124 /*
1125  * Allow the bootloader to enable hw ecc
1126  * hardware_ecc=1|0
1127  */
1128 static int __init hardware_ecc_setup(char *str)
1129 {
1130         if (str && str[0] == '1')
1131                 gta02_nand_info.software_ecc = 0;
1132         return 1;
1133 }
1134 __setup("hardware_ecc=", hardware_ecc_setup);
1135
1136 static void gta02_request_gpios(void)
1137 {
1138         int ret;
1139         ret = gpio_request(GTA02_GPIO_USB_PULLUP, "USB pullup");
1140         if (ret) {
1141                 printk(KERN_ERR "Failed to request USB pullup gpio pin: %d\n", ret);
1142         } else {
1143                 ret = gpio_direction_output(GTA02_GPIO_USB_PULLUP, 0);
1144                 if (ret)
1145                         printk(KERN_ERR "Failed to set USB pullup gpio direction: %d\n", ret);
1146     }
1147 }
1148
1149 static void __init gta02_machine_init(void)
1150 {
1151         /* Set the panic callback to make AUX LED blink at ~5Hz. */
1152         panic_blink = gta02_panic_blink;
1153
1154         bus_register_notifier(&platform_bus_type, &gta02_device_register_notifier);
1155         bus_register_notifier(&spi_bus_type, &gta02_device_register_notifier);
1156
1157         gta02_hijack_gpb();
1158
1159         gta02_request_gpios();
1160
1161         s3c_pm_init();
1162
1163 #ifdef CONFIG_CHARGER_PCF50633
1164         INIT_DELAYED_WORK(&gta02_charger_work, gta02_charger_worker);
1165 #endif
1166
1167         s3c24xx_udc_set_platdata(&gta02_udc_cfg);
1168         s3c_ohci_set_platdata(&gta02_usb_info);
1169         s3c_nand_set_platdata(&gta02_nand_info);
1170         s3c_i2c0_set_platdata(NULL);
1171         spi_register_board_info(gta02_spi_board_info,
1172                                 ARRAY_SIZE(gta02_spi_board_info));
1173
1174         i2c_register_board_info(0, gta02_i2c_devs, ARRAY_SIZE(gta02_i2c_devs));
1175  
1176         platform_add_devices(gta02_devices, ARRAY_SIZE(gta02_devices));
1177
1178         pm_power_off = gta02_poweroff;
1179 }
1180
1181
1182 MACHINE_START(NEO1973_GTA02, "GTA02")
1183         /* Maintainer: Nelson Castillo <arhuaco@freaks-unidos.net> */
1184         .phys_io        = S3C2410_PA_UART,
1185         .io_pg_offst    = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
1186         .boot_params    = S3C2410_SDRAM_PA + 0x100,
1187         .map_io         = gta02_map_io,
1188         .init_irq       = s3c24xx_init_irq,
1189         .init_machine   = gta02_machine_init,
1190         .timer          = &s3c24xx_timer,
1191 MACHINE_END