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