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