ARM: gta02: Add button support
[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
55 #include <linux/mfd/pcf50633/core.h>
56 #include <linux/mfd/pcf50633/mbc.h>
57 #include <linux/mfd/pcf50633/adc.h>
58 #include <linux/mfd/pcf50633/gpio.h>
59 #include <linux/mfd/pcf50633/pmic.h>
60
61 #include <linux/input.h>
62 #include <linux/gpio_keys.h>
63
64 #include <asm/mach/arch.h>
65 #include <asm/mach/map.h>
66 #include <asm/mach/irq.h>
67
68 #include <asm/irq.h>
69 #include <asm/mach-types.h>
70
71 #include <mach/regs-irq.h>
72 #include <mach/regs-gpio.h>
73 #include <mach/regs-gpioj.h>
74 #include <mach/fb.h>
75
76 #include <mach/spi.h>
77 #include <mach/spi-gpio.h>
78 #include <plat/usb-control.h>
79 #include <mach/regs-mem.h>
80 #include <mach/hardware.h>
81
82 #include <mach/gta02.h>
83
84 #include <plat/regs-serial.h>
85 #include <plat/nand.h>
86 #include <plat/devs.h>
87 #include <plat/cpu.h>
88 #include <plat/pm.h>
89 #include <plat/udc.h>
90 #include <plat/gpio-cfg.h>
91 #include <plat/iic.h>
92
93 static struct pcf50633 *gta02_pcf;
94
95 /*
96  * This gets called every 1ms when we paniced.
97  */
98
99 static long gta02_panic_blink(long count)
100 {
101         long delay = 0;
102         static long last_blink;
103         static char led;
104
105         /* Fast blink: 200ms period. */
106         if (count - last_blink < 100)
107                 return 0;
108
109         led ^= 1;
110         gpio_direction_output(GTA02_GPIO_AUX_LED, led);
111
112         last_blink = count;
113
114         return delay;
115 }
116
117
118 static struct map_desc gta02_iodesc[] __initdata = {
119         {
120                 .virtual        = 0xe0000000,
121                 .pfn            = __phys_to_pfn(S3C2410_CS3 + 0x01000000),
122                 .length         = SZ_1M,
123                 .type           = MT_DEVICE
124         },
125 };
126
127 #define UCON (S3C2410_UCON_DEFAULT | S3C2443_UCON_RXERR_IRQEN)
128 #define ULCON (S3C2410_LCON_CS8 | S3C2410_LCON_PNONE | S3C2410_LCON_STOPB)
129 #define UFCON (S3C2410_UFCON_RXTRIG8 | S3C2410_UFCON_FIFOMODE)
130
131 static struct s3c2410_uartcfg gta02_uartcfgs[] = {
132         [0] = {
133                 .hwport         = 0,
134                 .flags          = 0,
135                 .ucon           = UCON,
136                 .ulcon          = ULCON,
137                 .ufcon          = UFCON,
138         },
139         [1] = {
140                 .hwport         = 1,
141                 .flags          = 0,
142                 .ucon           = UCON,
143                 .ulcon          = ULCON,
144                 .ufcon          = UFCON,
145         },
146         [2] = {
147                 .hwport         = 2,
148                 .flags          = 0,
149                 .ucon           = UCON,
150                 .ulcon          = ULCON,
151                 .ufcon          = UFCON,
152         },
153 };
154
155 #ifdef CONFIG_CHARGER_PCF50633
156 /*
157  * On GTA02 the 1A charger features a 48K resistor to 0V on the ID pin.
158  * We use this to recognize that we can pull 1A from the USB socket.
159  *
160  * These constants are the measured pcf50633 ADC levels with the 1A
161  * charger / 48K resistor, and with no pulldown resistor.
162  */
163
164 #define ADC_NOM_CHG_DETECT_1A 6
165 #define ADC_NOM_CHG_DETECT_USB 43
166
167 static void
168 gta02_configure_pmu_for_charger(struct pcf50633 *pcf, void *unused, int res)
169 {
170         int  ma;
171
172         /* Interpret charger type */
173         if (res < ((ADC_NOM_CHG_DETECT_USB + ADC_NOM_CHG_DETECT_1A) / 2)) {
174
175                 /*
176                  * Sanity - stop GPO driving out now that we have a 1A charger
177                  * GPO controls USB Host power generation on GTA02
178                  */
179                 pcf50633_gpio_set(pcf, PCF50633_GPO, 0);
180
181                 ma = 1000;
182         } else
183                 ma = 100;
184
185         pcf50633_mbc_usb_curlim_set(pcf, ma);
186 }
187
188 static struct delayed_work gta02_charger_work;
189 static int gta02_usb_vbus_draw;
190
191 static void gta02_charger_worker(struct work_struct *work)
192 {
193         if (gta02_usb_vbus_draw) {
194                 pcf50633_mbc_usb_curlim_set(gta02_pcf, gta02_usb_vbus_draw);
195                 return;
196         }
197
198 #ifdef CONFIG_PCF50633_ADC
199         pcf50633_adc_async_read(gta02_pcf,
200                                 PCF50633_ADCC1_MUX_ADCIN1,
201                                 PCF50633_ADCC1_AVERAGE_16,
202                                 gta02_configure_pmu_for_charger,
203                                 NULL);
204 #else
205         /*
206          * If the PCF50633 ADC is disabled we fallback to a
207          * 100mA limit for safety.
208          */
209         pcf50633_mbc_usb_curlim_set(pcf, 100);
210 #endif
211 }
212
213 #define GTA02_CHARGER_CONFIGURE_TIMEOUT ((3000 * HZ) / 1000)
214
215 static void gta02_pmu_event_callback(struct pcf50633 *pcf, int irq)
216 {
217         if (irq == PCF50633_IRQ_USBINS) {
218                 schedule_delayed_work(&gta02_charger_work,
219                                       GTA02_CHARGER_CONFIGURE_TIMEOUT);
220
221                 return;
222         }
223
224         if (irq == PCF50633_IRQ_USBREM) {
225                 cancel_delayed_work_sync(&gta02_charger_work);
226                 gta02_usb_vbus_draw = 0;
227         }
228 }
229
230 static void gta02_udc_vbus_draw(unsigned int ma)
231 {
232         if (!gta02_pcf)
233                 return;
234
235         gta02_usb_vbus_draw = ma;
236
237         schedule_delayed_work(&gta02_charger_work,
238                               GTA02_CHARGER_CONFIGURE_TIMEOUT);
239 }
240 #else /* !CONFIG_CHARGER_PCF50633 */
241 #define gta02_pmu_event_callback        NULL
242 #define gta02_udc_vbus_draw             NULL
243 #endif
244
245 /*
246  * This is called when pc50633 is probed, unfortunately quite late in the
247  * day since it is an I2C bus device. Here we can belatedly define some
248  * platform devices with the advantage that we can mark the pcf50633 as the
249  * parent. This makes them get suspended and resumed with their parent
250  * the pcf50633 still around.
251  */
252
253 static void gta02_pmu_attach_child_devices(struct pcf50633 *pcf);
254
255
256 static char *gta02_batteries[] = {
257         "battery",
258 };
259
260 struct pcf50633_platform_data gta02_pcf_pdata = {
261         .resumers = {
262                 [0] =   PCF50633_INT1_USBINS |
263                         PCF50633_INT1_USBREM |
264                         PCF50633_INT1_ALARM,
265                 [1] =   PCF50633_INT2_ONKEYF,
266                 [2] =   PCF50633_INT3_ONKEY1S,
267                 [3] =   PCF50633_INT4_LOWSYS |
268                         PCF50633_INT4_LOWBAT |
269                         PCF50633_INT4_HIGHTMP,
270         },
271
272         .batteries = gta02_batteries,
273         .num_batteries = ARRAY_SIZE(gta02_batteries),
274
275         .charger_reference_current_ma = 1000,
276
277         .reg_init_data = {
278                 [PCF50633_REGULATOR_AUTO] = {
279                         .constraints = {
280                                 .min_uV = 3300000,
281                                 .max_uV = 3300000,
282                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
283                                 .always_on = 1,
284                                 .apply_uV = 1,
285                                 .state_mem = {
286                                         .enabled = 1,
287                                 },
288                         },
289                 },
290                 [PCF50633_REGULATOR_DOWN1] = {
291                         .constraints = {
292                                 .min_uV = 1300000,
293                                 .max_uV = 1600000,
294                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
295                                 .always_on = 1,
296                                 .apply_uV = 1,
297                         },
298                 },
299                 [PCF50633_REGULATOR_DOWN2] = {
300                         .constraints = {
301                                 .min_uV = 1800000,
302                                 .max_uV = 1800000,
303                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
304                                 .apply_uV = 1,
305                                 .always_on = 1,
306                                 .state_mem = {
307                                         .enabled = 1,
308                                 },
309                         },
310                 },
311                 [PCF50633_REGULATOR_HCLDO] = {
312                         .constraints = {
313                                 .min_uV = 2000000,
314                                 .max_uV = 3300000,
315                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
316                                 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,
317                                 .always_on = 1,
318                         },
319                 },
320                 [PCF50633_REGULATOR_LDO1] = {
321                         .constraints = {
322                                 .min_uV = 3300000,
323                                 .max_uV = 3300000,
324                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
325                                 .apply_uV = 1,
326                                 .state_mem = {
327                                         .enabled = 0,
328                                 },
329                         },
330                 },
331                 [PCF50633_REGULATOR_LDO2] = {
332                         .constraints = {
333                                 .min_uV = 3300000,
334                                 .max_uV = 3300000,
335                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
336                                 .apply_uV = 1,
337                         },
338                 },
339                 [PCF50633_REGULATOR_LDO3] = {
340                         .constraints = {
341                                 .min_uV = 3000000,
342                                 .max_uV = 3000000,
343                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
344                                 .apply_uV = 1,
345                         },
346                 },
347                 [PCF50633_REGULATOR_LDO4] = {
348                         .constraints = {
349                                 .min_uV = 3200000,
350                                 .max_uV = 3200000,
351                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
352                                 .apply_uV = 1,
353                         },
354                 },
355                 [PCF50633_REGULATOR_LDO5] = {
356                         .constraints = {
357                                 .min_uV = 3000000,
358                                 .max_uV = 3000000,
359                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
360                                 .apply_uV = 1,
361                                 .state_mem = {
362                                         .enabled = 1,
363                                 },
364                         },
365                 },
366                 [PCF50633_REGULATOR_LDO6] = {
367                         .constraints = {
368                                 .min_uV = 3000000,
369                                 .max_uV = 3000000,
370                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
371                         },
372                 },
373                 [PCF50633_REGULATOR_MEMLDO] = {
374                         .constraints = {
375                                 .min_uV = 1800000,
376                                 .max_uV = 1800000,
377                                 .valid_modes_mask = REGULATOR_MODE_NORMAL,
378                                 .state_mem = {
379                                         .enabled = 1,
380                                 },
381                         },
382                 },
383
384         },
385         .probe_done = gta02_pmu_attach_child_devices,
386         .mbc_event_callback = gta02_pmu_event_callback,
387 };
388
389
390 /* NOR Flash. */
391
392 #define GTA02_FLASH_BASE        0x18000000 /* GCS3 */
393 #define GTA02_FLASH_SIZE        0x200000 /* 2MBytes */
394
395 static struct physmap_flash_data gta02_nor_flash_data = {
396         .width          = 2,
397 };
398
399 static struct resource gta02_nor_flash_resource = {
400         .start          = GTA02_FLASH_BASE,
401         .end            = GTA02_FLASH_BASE + GTA02_FLASH_SIZE - 1,
402         .flags          = IORESOURCE_MEM,
403 };
404
405 static struct platform_device gta02_nor_flash = {
406         .name           = "physmap-flash",
407         .id             = 0,
408         .dev            = {
409                 .platform_data  = &gta02_nor_flash_data,
410         },
411         .resource       = &gta02_nor_flash_resource,
412         .num_resources  = 1,
413 };
414
415
416 struct platform_device s3c24xx_pwm_device = {
417         .name           = "s3c24xx_pwm",
418         .num_resources  = 0,
419 };
420
421 static struct i2c_board_info gta02_i2c_devs[] __initdata = {
422         {
423                 I2C_BOARD_INFO("pcf50633", 0x73),
424                 .irq = GTA02_IRQ_PCF50633,
425                 .platform_data = &gta02_pcf_pdata,
426         },
427         {
428                 I2C_BOARD_INFO("wm8753", 0x1a),
429         },
430 };
431
432 static struct s3c2410_nand_set __initdata gta02_nand_sets[] = {
433         [0] = {
434                 /*
435                  * This name is also hard-coded in the boot loaders, so
436                  * changing it would would require all users to upgrade
437                  * their boot loaders, some of which are stored in a NOR
438                  * that is considered to be immutable.
439                  */
440                 .name           = "neo1973-nand",
441                 .nr_chips       = 1,
442                 .flash_bbt      = 1,
443         },
444 };
445
446 /*
447  * Choose a set of timings derived from S3C@2442B MCP54
448  * data sheet (K5D2G13ACM-D075 MCP Memory).
449  */
450
451 static struct s3c2410_platform_nand __initdata gta02_nand_info = {
452         .tacls          = 0,
453         .twrph0         = 25,
454         .twrph1         = 15,
455         .nr_sets        = ARRAY_SIZE(gta02_nand_sets),
456         .sets           = gta02_nand_sets,
457 };
458
459
460 static void gta02_udc_command(enum s3c2410_udc_cmd_e cmd)
461 {
462         switch (cmd) {
463         case S3C2410_UDC_P_ENABLE:
464                 pr_debug("%s S3C2410_UDC_P_ENABLE\n", __func__);
465                 gpio_direction_output(GTA02_GPIO_USB_PULLUP, 1);
466                 break;
467         case S3C2410_UDC_P_DISABLE:
468                 pr_debug("%s S3C2410_UDC_P_DISABLE\n", __func__);
469                 gpio_direction_output(GTA02_GPIO_USB_PULLUP, 0);
470                 break;
471         case S3C2410_UDC_P_RESET:
472                 pr_debug("%s S3C2410_UDC_P_RESET\n", __func__);
473                 /* FIXME: Do something here. */
474         }
475 }
476
477 /* Get PMU to set USB current limit accordingly. */
478 static struct s3c2410_udc_mach_info gta02_udc_cfg = {
479         .vbus_draw      = gta02_udc_vbus_draw,
480         .udc_command    = gta02_udc_command,
481
482 };
483
484
485
486 static void gta02_bl_set_intensity(int intensity)
487 {
488         struct pcf50633 *pcf = gta02_pcf;
489         int old_intensity = pcf50633_reg_read(pcf, PCF50633_REG_LEDOUT);
490
491         /* We map 8-bit intensity to 6-bit intensity in hardware. */
492         intensity >>= 2;
493
494         /*
495          * This can happen during, eg, print of panic on blanked console,
496          * but we can't service i2c without interrupts active, so abort.
497          */
498         if (in_atomic()) {
499                 printk(KERN_ERR "gta02_bl_set_intensity called while atomic\n");
500                 return;
501         }
502
503         old_intensity = pcf50633_reg_read(pcf, PCF50633_REG_LEDOUT);
504         if (intensity == old_intensity)
505                 return;
506
507         /* We can't do this anywhere else. */
508         pcf50633_reg_write(pcf, PCF50633_REG_LEDDIM, 5);
509
510         if (!(pcf50633_reg_read(pcf, PCF50633_REG_LEDENA) & 3))
511                 old_intensity = 0;
512
513         /*
514          * The PCF50633 cannot handle LEDOUT = 0 (datasheet p60)
515          * if seen, you have to re-enable the LED unit.
516          */
517         if (!intensity || !old_intensity)
518                 pcf50633_reg_write(pcf, PCF50633_REG_LEDENA, 0);
519
520         /* Illegal to set LEDOUT to 0. */
521         if (!intensity)
522                 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f, 2);
523         else
524                 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f,
525                                           intensity);
526
527         if (intensity)
528                 pcf50633_reg_write(pcf, PCF50633_REG_LEDENA, 2);
529
530 }
531
532 static struct generic_bl_info gta02_bl_info = {
533         .name                   = "gta02-bl",
534         .max_intensity          = 0xff,
535         .default_intensity      = 0xff,
536         .set_bl_intensity       = gta02_bl_set_intensity,
537 };
538
539 static struct platform_device gta02_bl_dev = {
540         .name                   = "generic-bl",
541         .id                     = 1,
542         .dev = {
543                 .platform_data = &gta02_bl_info,
544         },
545 };
546
547
548
549 /* USB */
550 static struct s3c2410_hcd_info gta02_usb_info __initdata = {
551         .port[0]        = {
552                 .flags  = S3C_HCDFLG_USED,
553         },
554         .port[1]        = {
555                 .flags  = 0,
556         },
557 };
558
559 /* Buttons */
560 static struct gpio_keys_button gta02_buttons[] = {
561         {
562                 .gpio = GTA02_GPIO_AUX_KEY,
563                 .code = KEY_PHONE,
564                 .desc = "Aux",
565                 .type = EV_KEY,
566                 .debounce_interval = 100,
567         },
568         {
569                 .gpio = GTA02_GPIO_HOLD_KEY,
570                 .code = KEY_PAUSE,
571                 .desc = "Hold",
572                 .type = EV_KEY,
573                 .debounce_interval = 100,
574         },
575 };
576
577 static struct gpio_keys_platform_data gta02_buttons_pdata = {
578         .buttons = gta02_buttons,
579         .nbuttons = ARRAY_SIZE(gta02_buttons),
580 };
581
582 static struct platform_device gta02_buttons_device = {
583         .name = "gpio-keys",
584         .id = -1,
585         .dev = {
586                 .platform_data = &gta02_buttons_pdata,
587         },
588 };
589
590 static void __init gta02_map_io(void)
591 {
592         s3c24xx_init_io(gta02_iodesc, ARRAY_SIZE(gta02_iodesc));
593         s3c24xx_init_clocks(12000000);
594         s3c24xx_init_uarts(gta02_uartcfgs, ARRAY_SIZE(gta02_uartcfgs));
595 }
596
597
598 /* These are the guys that don't need to be children of PMU. */
599
600 static struct platform_device *gta02_devices[] __initdata = {
601         &s3c_device_ohci,
602         &s3c_device_wdt,
603         &s3c_device_sdi,
604         &s3c_device_usbgadget,
605         &s3c_device_nand,
606         &gta02_nor_flash,
607         &s3c24xx_pwm_device,
608         &s3c_device_iis,
609         &s3c_device_i2c0,
610         &gta02_buttons_device,
611 };
612
613 /* These guys DO need to be children of PMU. */
614
615 static struct platform_device *gta02_devices_pmu_children[] = {
616         &gta02_bl_dev,
617 };
618
619
620 /*
621  * This is called when pc50633 is probed, quite late in the day since it is an
622  * I2C bus device.  Here we can define platform devices with the advantage that
623  * we can mark the pcf50633 as the parent.  This makes them get suspended and
624  * resumed with their parent the pcf50633 still around.  All devices whose
625  * operation depends on something from pcf50633 must have this relationship
626  * made explicit like this, or suspend and resume will become an unreliable
627  * hellworld.
628  */
629
630 static void gta02_pmu_attach_child_devices(struct pcf50633 *pcf)
631 {
632         int n;
633
634         /* Grab a copy of the now probed PMU pointer. */
635         gta02_pcf = pcf;
636
637         for (n = 0; n < ARRAY_SIZE(gta02_devices_pmu_children); n++)
638                 gta02_devices_pmu_children[n]->dev.parent = pcf->dev;
639
640         platform_add_devices(gta02_devices_pmu_children,
641                              ARRAY_SIZE(gta02_devices_pmu_children));
642 }
643
644 static void gta02_poweroff(void)
645 {
646         pcf50633_reg_set_bit_mask(gta02_pcf, PCF50633_REG_OOCSHDWN, 1, 1);
647 }
648
649 static void __init gta02_machine_init(void)
650 {
651         /* Set the panic callback to make AUX LED blink at ~5Hz. */
652         panic_blink = gta02_panic_blink;
653
654         s3c_pm_init();
655
656 #ifdef CONFIG_CHARGER_PCF50633
657         INIT_DELAYED_WORK(&gta02_charger_work, gta02_charger_worker);
658 #endif
659
660         s3c24xx_udc_set_platdata(&gta02_udc_cfg);
661         s3c_ohci_set_platdata(&gta02_usb_info);
662         s3c_nand_set_platdata(&gta02_nand_info);
663         s3c_i2c0_set_platdata(NULL);
664
665         i2c_register_board_info(0, gta02_i2c_devs, ARRAY_SIZE(gta02_i2c_devs));
666
667         platform_add_devices(gta02_devices, ARRAY_SIZE(gta02_devices));
668         pm_power_off = gta02_poweroff;
669 }
670
671
672 MACHINE_START(NEO1973_GTA02, "GTA02")
673         /* Maintainer: Nelson Castillo <arhuaco@freaks-unidos.net> */
674         .phys_io        = S3C2410_PA_UART,
675         .io_pg_offst    = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
676         .boot_params    = S3C2410_SDRAM_PA + 0x100,
677         .map_io         = gta02_map_io,
678         .init_irq       = s3c24xx_init_irq,
679         .init_machine   = gta02_machine_init,
680         .timer          = &s3c24xx_timer,
681 MACHINE_END