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