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