Merge branch 'for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[sfrench/cifs-2.6.git] / arch / arm / mach-pxa / magician.c
1 /*
2  * Support for HTC Magician PDA phones:
3  * i-mate JAM, O2 Xda mini, Orange SPV M500, Qtek s100, Qtek s110
4  * and T-Mobile MDA Compact.
5  *
6  * Copyright (c) 2006-2007 Philipp Zabel
7  *
8  * Based on hx4700.c, spitz.c and others.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/delay.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/machine.h>
22 #include <linux/gpio_keys.h>
23 #include <linux/input.h>
24 #include <linux/mfd/htc-pasic3.h>
25 #include <linux/mtd/physmap.h>
26 #include <linux/pda_power.h>
27 #include <linux/platform_data/gpio-htc-egpio.h>
28 #include <linux/pwm.h>
29 #include <linux/pwm_backlight.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/fixed.h>
32 #include <linux/regulator/gpio-regulator.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/usb/gpio_vbus.h>
35 #include <linux/platform_data/i2c-pxa.h>
36
37 #include <mach/hardware.h>
38 #include <asm/mach-types.h>
39 #include <asm/mach/arch.h>
40 #include <asm/system_info.h>
41
42 #include "pxa27x.h"
43 #include <mach/magician.h>
44 #include <linux/platform_data/video-pxafb.h>
45 #include <linux/platform_data/mmc-pxamci.h>
46 #include <linux/platform_data/irda-pxaficp.h>
47 #include <linux/platform_data/usb-ohci-pxa27x.h>
48
49 #include <linux/regulator/max1586.h>
50
51 #include <linux/platform_data/pxa2xx_udc.h>
52
53 #include "udc.h"
54 #include "pxa27x-udc.h"
55 #include "devices.h"
56 #include "generic.h"
57
58 #include <linux/spi/spi.h>
59 #include <linux/spi/pxa2xx_spi.h>
60 #include <linux/spi/ads7846.h>
61
62 static unsigned long magician_pin_config[] __initdata = {
63
64         /* SDRAM and Static Memory I/O Signals */
65         GPIO20_nSDCS_2,
66         GPIO21_nSDCS_3,
67         GPIO15_nCS_1,
68         GPIO78_nCS_2,   /* PASIC3 */
69         GPIO79_nCS_3,   /* EGPIO CPLD */
70         GPIO80_nCS_4,
71         GPIO33_nCS_5,
72
73         /* I2C UDA1380 + OV9640 */
74         GPIO117_I2C_SCL,
75         GPIO118_I2C_SDA,
76
77         /* PWM 0 - LCD backlight */
78         GPIO16_PWM0_OUT,
79
80         /* I2S UDA1380 capture */
81         GPIO28_I2S_BITCLK_OUT,
82         GPIO29_I2S_SDATA_IN,
83         GPIO31_I2S_SYNC,
84         GPIO113_I2S_SYSCLK,
85
86         /* SSP 1 UDA1380 playback */
87         GPIO23_SSP1_SCLK,
88         GPIO24_SSP1_SFRM,
89         GPIO25_SSP1_TXD,
90
91         /* SSP 2 TSC2046 touchscreen */
92         GPIO19_SSP2_SCLK,
93         MFP_CFG_OUT(GPIO14, AF0, DRIVE_HIGH),   /* frame as GPIO */
94         GPIO89_SSP2_TXD,
95         GPIO88_SSP2_RXD,
96
97         /* MMC/SD/SDHC slot */
98         GPIO32_MMC_CLK,
99         GPIO92_MMC_DAT_0,
100         GPIO109_MMC_DAT_1,
101         GPIO110_MMC_DAT_2,
102         GPIO111_MMC_DAT_3,
103         GPIO112_MMC_CMD,
104
105         /* LCD */
106         GPIOxx_LCD_TFT_16BPP,
107
108         /* QCI camera interface */
109         GPIO12_CIF_DD_7,
110         GPIO17_CIF_DD_6,
111         GPIO50_CIF_DD_3,
112         GPIO51_CIF_DD_2,
113         GPIO52_CIF_DD_4,
114         GPIO53_CIF_MCLK,
115         GPIO54_CIF_PCLK,
116         GPIO55_CIF_DD_1,
117         GPIO81_CIF_DD_0,
118         GPIO82_CIF_DD_5,
119         GPIO84_CIF_FV,
120         GPIO85_CIF_LV,
121
122         /* Magician specific input GPIOs */
123         GPIO9_GPIO,     /* unknown */
124         GPIO10_GPIO,    /* GSM_IRQ */
125         GPIO13_GPIO,    /* CPLD_IRQ */
126         GPIO107_GPIO,   /* DS1WM_IRQ */
127         GPIO108_GPIO,   /* GSM_READY */
128         GPIO115_GPIO,   /* nPEN_IRQ */
129 };
130
131 /*
132  * IrDA
133  */
134
135 static struct pxaficp_platform_data magician_ficp_info = {
136         .gpio_pwdown            = GPIO83_MAGICIAN_nIR_EN,
137         .transceiver_cap        = IR_SIRMODE | IR_OFF,
138         .gpio_pwdown_inverted   = 0,
139 };
140
141 /*
142  * GPIO Keys
143  */
144
145 #define INIT_KEY(_code, _gpio, _desc)   \
146         {                               \
147                 .code   = KEY_##_code,  \
148                 .gpio   = _gpio,        \
149                 .desc   = _desc,        \
150                 .type   = EV_KEY,       \
151                 .wakeup = 1,            \
152         }
153
154 static struct gpio_keys_button magician_button_table[] = {
155         INIT_KEY(POWER,      GPIO0_MAGICIAN_KEY_POWER,      "Power button"),
156         INIT_KEY(ESC,        GPIO37_MAGICIAN_KEY_HANGUP,    "Hangup button"),
157         INIT_KEY(F10,        GPIO38_MAGICIAN_KEY_CONTACTS,  "Contacts button"),
158         INIT_KEY(CALENDAR,   GPIO90_MAGICIAN_KEY_CALENDAR,  "Calendar button"),
159         INIT_KEY(CAMERA,     GPIO91_MAGICIAN_KEY_CAMERA,    "Camera button"),
160         INIT_KEY(UP,         GPIO93_MAGICIAN_KEY_UP,        "Up button"),
161         INIT_KEY(DOWN,       GPIO94_MAGICIAN_KEY_DOWN,      "Down button"),
162         INIT_KEY(LEFT,       GPIO95_MAGICIAN_KEY_LEFT,      "Left button"),
163         INIT_KEY(RIGHT,      GPIO96_MAGICIAN_KEY_RIGHT,     "Right button"),
164         INIT_KEY(KPENTER,    GPIO97_MAGICIAN_KEY_ENTER,     "Action button"),
165         INIT_KEY(RECORD,     GPIO98_MAGICIAN_KEY_RECORD,    "Record button"),
166         INIT_KEY(VOLUMEUP,   GPIO100_MAGICIAN_KEY_VOL_UP,   "Volume up"),
167         INIT_KEY(VOLUMEDOWN, GPIO101_MAGICIAN_KEY_VOL_DOWN, "Volume down"),
168         INIT_KEY(PHONE,      GPIO102_MAGICIAN_KEY_PHONE,    "Phone button"),
169         INIT_KEY(PLAY,       GPIO99_MAGICIAN_HEADPHONE_IN,  "Headset button"),
170 };
171
172 static struct gpio_keys_platform_data gpio_keys_data = {
173         .buttons        = magician_button_table,
174         .nbuttons       = ARRAY_SIZE(magician_button_table),
175 };
176
177 static struct platform_device gpio_keys = {
178         .name   = "gpio-keys",
179         .dev    = {
180                 .platform_data = &gpio_keys_data,
181         },
182         .id     = -1,
183 };
184
185 /*
186  * EGPIO (Xilinx CPLD)
187  *
188  * 32-bit aligned 8-bit registers
189  * 16 possible registers (reg windows size), only 7 used:
190  * 3x output, 1x irq, 3x input
191  */
192
193 static struct resource egpio_resources[] = {
194         [0] = {
195                 .start  = PXA_CS3_PHYS,
196                 .end    = PXA_CS3_PHYS + 0x20 - 1,
197                 .flags  = IORESOURCE_MEM,
198         },
199         [1] = {
200                 .start  = PXA_GPIO_TO_IRQ(GPIO13_MAGICIAN_CPLD_IRQ),
201                 .end    = PXA_GPIO_TO_IRQ(GPIO13_MAGICIAN_CPLD_IRQ),
202                 .flags  = IORESOURCE_IRQ,
203         },
204 };
205
206 static struct htc_egpio_chip egpio_chips[] = {
207         [0] = {
208                 .reg_start      = 0,
209                 .gpio_base      = MAGICIAN_EGPIO(0, 0),
210                 .num_gpios      = 24,
211                 .direction      = HTC_EGPIO_OUTPUT,
212                 /*
213                  * Depends on modules configuration
214                  */
215                 .initial_values = 0x40, /* EGPIO_MAGICIAN_GSM_RESET */
216         },
217         [1] = {
218                 .reg_start      = 4,
219                 .gpio_base      = MAGICIAN_EGPIO(4, 0),
220                 .num_gpios      = 24,
221                 .direction      = HTC_EGPIO_INPUT,
222         },
223 };
224
225 static struct htc_egpio_platform_data egpio_info = {
226         .reg_width      = 8,
227         .bus_width      = 32,
228         .irq_base       = IRQ_BOARD_START,
229         .num_irqs       = 4,
230         .ack_register   = 3,
231         .chip           = egpio_chips,
232         .num_chips      = ARRAY_SIZE(egpio_chips),
233 };
234
235 static struct platform_device egpio = {
236         .name           = "htc-egpio",
237         .id             = -1,
238         .resource       = egpio_resources,
239         .num_resources  = ARRAY_SIZE(egpio_resources),
240         .dev = {
241                 .platform_data = &egpio_info,
242         },
243 };
244
245 /*
246  * PXAFB LCD - Toppoly TD028STEB1 or Samsung LTP280QV
247  */
248
249 static struct pxafb_mode_info toppoly_modes[] = {
250         {
251                 .pixclock       = 96153,
252                 .bpp            = 16,
253                 .xres           = 240,
254                 .yres           = 320,
255                 .hsync_len      = 11,
256                 .vsync_len      = 3,
257                 .left_margin    = 19,
258                 .upper_margin   = 2,
259                 .right_margin   = 10,
260                 .lower_margin   = 2,
261                 .sync           = 0,
262         },
263 };
264
265 static struct pxafb_mode_info samsung_modes[] = {
266         {
267                 .pixclock       = 226469,
268                 .bpp            = 16,
269                 .xres           = 240,
270                 .yres           = 320,
271                 .hsync_len      = 8,
272                 .vsync_len      = 4,
273                 .left_margin    = 9,
274                 .upper_margin   = 4,
275                 .right_margin   = 9,
276                 .lower_margin   = 4,
277                 .sync   = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
278         },
279 };
280
281 static void toppoly_lcd_power(int on, struct fb_var_screeninfo *si)
282 {
283         pr_debug("Toppoly LCD power: %s\n", on ? "on" : "off");
284
285         if (on) {
286                 gpio_set_value(EGPIO_MAGICIAN_TOPPOLY_POWER, 1);
287                 gpio_set_value(GPIO106_MAGICIAN_LCD_DCDC_NRESET, 1);
288                 udelay(2000);
289                 gpio_set_value(EGPIO_MAGICIAN_LCD_POWER, 1);
290                 udelay(2000);
291                 /* FIXME: enable LCDC here */
292                 udelay(2000);
293                 gpio_set_value(GPIO104_MAGICIAN_LCD_VOFF_EN, 1);
294                 udelay(2000);
295                 gpio_set_value(GPIO105_MAGICIAN_LCD_VON_EN, 1);
296         } else {
297                 msleep(15);
298                 gpio_set_value(GPIO105_MAGICIAN_LCD_VON_EN, 0);
299                 udelay(500);
300                 gpio_set_value(GPIO104_MAGICIAN_LCD_VOFF_EN, 0);
301                 udelay(1000);
302                 gpio_set_value(GPIO106_MAGICIAN_LCD_DCDC_NRESET, 0);
303                 gpio_set_value(EGPIO_MAGICIAN_LCD_POWER, 0);
304         }
305 }
306
307 static void samsung_lcd_power(int on, struct fb_var_screeninfo *si)
308 {
309         pr_debug("Samsung LCD power: %s\n", on ? "on" : "off");
310
311         if (on) {
312                 if (system_rev < 3)
313                         gpio_set_value(GPIO75_MAGICIAN_SAMSUNG_POWER, 1);
314                 else
315                         gpio_set_value(EGPIO_MAGICIAN_LCD_POWER, 1);
316                 mdelay(6);
317                 gpio_set_value(GPIO106_MAGICIAN_LCD_DCDC_NRESET, 1);
318                 mdelay(6);      /* Avdd -> Voff >5ms */
319                 gpio_set_value(GPIO104_MAGICIAN_LCD_VOFF_EN, 1);
320                 mdelay(16);     /* Voff -> Von >(5+10)ms */
321                 gpio_set_value(GPIO105_MAGICIAN_LCD_VON_EN, 1);
322         } else {
323                 gpio_set_value(GPIO105_MAGICIAN_LCD_VON_EN, 0);
324                 mdelay(16);
325                 gpio_set_value(GPIO104_MAGICIAN_LCD_VOFF_EN, 0);
326                 mdelay(6);
327                 gpio_set_value(GPIO106_MAGICIAN_LCD_DCDC_NRESET, 0);
328                 mdelay(6);
329                 if (system_rev < 3)
330                         gpio_set_value(GPIO75_MAGICIAN_SAMSUNG_POWER, 0);
331                 else
332                         gpio_set_value(EGPIO_MAGICIAN_LCD_POWER, 0);
333         }
334 }
335
336 static struct pxafb_mach_info toppoly_info = {
337         .modes                  = toppoly_modes,
338         .num_modes              = 1,
339         .fixed_modes            = 1,
340         .lcd_conn               = LCD_COLOR_TFT_16BPP,
341         .pxafb_lcd_power        = toppoly_lcd_power,
342 };
343
344 static struct pxafb_mach_info samsung_info = {
345         .modes                  = samsung_modes,
346         .num_modes              = 1,
347         .fixed_modes            = 1,
348         .lcd_conn               = LCD_COLOR_TFT_16BPP | LCD_PCLK_EDGE_FALL |
349                 LCD_ALTERNATE_MAPPING,
350         .pxafb_lcd_power        = samsung_lcd_power,
351 };
352
353 /*
354  * Backlight
355  */
356
357 static struct pwm_lookup magician_pwm_lookup[] = {
358         PWM_LOOKUP("pxa27x-pwm.0", 0, "pwm-backlight", NULL, 30923,
359                    PWM_POLARITY_NORMAL),
360 };
361
362  /*
363  * fixed regulator for pwm_backlight
364  */
365
366 static struct regulator_consumer_supply pwm_backlight_supply[] = {
367         REGULATOR_SUPPLY("power", "pwm_backlight"),
368 };
369
370
371 static struct gpio magician_bl_gpios[] = {
372         { EGPIO_MAGICIAN_BL_POWER,      GPIOF_DIR_OUT, "Backlight power" },
373         { EGPIO_MAGICIAN_BL_POWER2,     GPIOF_DIR_OUT, "Backlight power 2" },
374 };
375
376 static int magician_backlight_init(struct device *dev)
377 {
378         return gpio_request_array(ARRAY_AND_SIZE(magician_bl_gpios));
379 }
380
381 static int magician_backlight_notify(struct device *dev, int brightness)
382 {
383         pr_debug("Brightness = %i\n", brightness);
384         gpio_set_value(EGPIO_MAGICIAN_BL_POWER, brightness);
385         if (brightness >= 200) {
386                 gpio_set_value(EGPIO_MAGICIAN_BL_POWER2, 1);
387                 return brightness - 72;
388         } else {
389                 gpio_set_value(EGPIO_MAGICIAN_BL_POWER2, 0);
390                 return brightness;
391         }
392 }
393
394 static void magician_backlight_exit(struct device *dev)
395 {
396         gpio_free_array(ARRAY_AND_SIZE(magician_bl_gpios));
397 }
398
399 /*
400  * LCD PWM backlight (main)
401  *
402  * MP1521 frequency should be:
403  *      100-400 Hz = 2 .5*10^6 - 10 *10^6 ns
404  */
405
406 static struct platform_pwm_backlight_data backlight_data = {
407         .max_brightness = 272,
408         .dft_brightness = 100,
409         .enable_gpio    = -1,
410         .init           = magician_backlight_init,
411         .notify         = magician_backlight_notify,
412         .exit           = magician_backlight_exit,
413 };
414
415 static struct platform_device backlight = {
416         .name   = "pwm-backlight",
417         .id     = -1,
418         .dev    = {
419                 .parent         = &pxa27x_device_pwm0.dev,
420                 .platform_data  = &backlight_data,
421         },
422 };
423
424 /*
425  * GPIO LEDs, Phone keys backlight, vibra
426  */
427
428 static struct gpio_led gpio_leds[] = {
429         {
430                 .name = "magician::vibra",
431                 .default_trigger = "none",
432                 .gpio = GPIO22_MAGICIAN_VIBRA_EN,
433         },
434         {
435                 .name = "magician::phone_bl",
436                 .default_trigger = "backlight",
437                 .gpio = GPIO103_MAGICIAN_LED_KP,
438         },
439 };
440
441 static struct gpio_led_platform_data gpio_led_info = {
442         .leds = gpio_leds,
443         .num_leds = ARRAY_SIZE(gpio_leds),
444 };
445
446 static struct platform_device leds_gpio = {
447         .name   = "leds-gpio",
448         .id     = -1,
449         .dev    = {
450                 .platform_data = &gpio_led_info,
451         },
452 };
453
454 /*
455  * PASIC3 with DS1WM
456  */
457
458 static struct resource pasic3_resources[] = {
459         [0] = {
460                 .start  = PXA_CS2_PHYS,
461                 .end    = PXA_CS2_PHYS + 0x1b,
462                 .flags  = IORESOURCE_MEM,
463         },
464         /* No IRQ handler in the PASIC3, DS1WM needs an external IRQ */
465         [1] = {
466                 .start  = PXA_GPIO_TO_IRQ(GPIO107_MAGICIAN_DS1WM_IRQ),
467                 .end    = PXA_GPIO_TO_IRQ(GPIO107_MAGICIAN_DS1WM_IRQ),
468                 .flags  = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
469         }
470 };
471
472 static struct pasic3_platform_data pasic3_platform_data = {
473         .clock_rate = 4000000,
474 };
475
476 static struct platform_device pasic3 = {
477         .name           = "pasic3",
478         .id             = -1,
479         .num_resources  = ARRAY_SIZE(pasic3_resources),
480         .resource       = pasic3_resources,
481         .dev = {
482                 .platform_data = &pasic3_platform_data,
483         },
484 };
485
486 /*
487  * PXA UDC
488  */
489
490 static void magician_udc_command(int cmd)
491 {
492         if (cmd == PXA2XX_UDC_CMD_CONNECT)
493                 UP2OCR |= UP2OCR_DPPUE | UP2OCR_DPPUBE;
494         else if (cmd == PXA2XX_UDC_CMD_DISCONNECT)
495                 UP2OCR &= ~(UP2OCR_DPPUE | UP2OCR_DPPUBE);
496 }
497
498 static struct pxa2xx_udc_mach_info magician_udc_info __initdata = {
499         .udc_command    = magician_udc_command,
500         .gpio_pullup    = GPIO27_MAGICIAN_USBC_PUEN,
501 };
502
503 /*
504  * USB device VBus detection
505  */
506
507 static struct resource gpio_vbus_resource = {
508         .flags  = IORESOURCE_IRQ,
509         .start  = IRQ_MAGICIAN_VBUS,
510         .end    = IRQ_MAGICIAN_VBUS,
511 };
512
513 static struct gpio_vbus_mach_info gpio_vbus_info = {
514         .gpio_pullup    = GPIO27_MAGICIAN_USBC_PUEN,
515         .gpio_vbus      = EGPIO_MAGICIAN_CABLE_VBUS,
516 };
517
518 static struct platform_device gpio_vbus = {
519         .name           = "gpio-vbus",
520         .id             = -1,
521         .num_resources  = 1,
522         .resource       = &gpio_vbus_resource,
523         .dev = {
524                 .platform_data = &gpio_vbus_info,
525         },
526 };
527
528 /*
529  * External power
530  */
531
532 static int magician_supply_init(struct device *dev)
533 {
534         int ret = -1;
535
536         ret = gpio_request(EGPIO_MAGICIAN_CABLE_TYPE, "Cable is AC charger");
537         if (ret) {
538                 pr_err("Cannot request AC/USB charger GPIO (%i)\n", ret);
539                 goto err_ac;
540         }
541
542         ret = gpio_request(EGPIO_MAGICIAN_CABLE_INSERTED, "Cable inserted");
543         if (ret) {
544                 pr_err("Cannot request cable detection GPIO (%i)\n", ret);
545                 goto err_usb;
546         }
547
548         return 0;
549
550 err_usb:
551         gpio_free(EGPIO_MAGICIAN_CABLE_TYPE);
552 err_ac:
553         return ret;
554 }
555
556 static void magician_set_charge(int flags)
557 {
558         if (flags & PDA_POWER_CHARGE_AC) {
559                 pr_debug("Charging from AC\n");
560                 gpio_set_value(EGPIO_MAGICIAN_NICD_CHARGE, 1);
561         } else if (flags & PDA_POWER_CHARGE_USB) {
562                 pr_debug("Charging from USB\n");
563                 gpio_set_value(EGPIO_MAGICIAN_NICD_CHARGE, 1);
564         } else {
565                 pr_debug("Charging disabled\n");
566                 gpio_set_value(EGPIO_MAGICIAN_NICD_CHARGE, 0);
567         }
568 }
569
570 static int magician_is_ac_online(void)
571 {
572         return gpio_get_value(EGPIO_MAGICIAN_CABLE_INSERTED) &&
573                 gpio_get_value(EGPIO_MAGICIAN_CABLE_TYPE); /* AC=1 */
574 }
575
576 static int magician_is_usb_online(void)
577 {
578         return gpio_get_value(EGPIO_MAGICIAN_CABLE_INSERTED) &&
579                 (!gpio_get_value(EGPIO_MAGICIAN_CABLE_TYPE)); /* USB=0 */
580 }
581
582 static void magician_supply_exit(struct device *dev)
583 {
584         gpio_free(EGPIO_MAGICIAN_CABLE_INSERTED);
585         gpio_free(EGPIO_MAGICIAN_CABLE_TYPE);
586 }
587
588 static char *magician_supplicants[] = {
589         "ds2760-battery.0", "backup-battery"
590 };
591
592 static struct pda_power_pdata power_supply_info = {
593         .init                   = magician_supply_init,
594         .exit                   = magician_supply_exit,
595         .is_ac_online           = magician_is_ac_online,
596         .is_usb_online          = magician_is_usb_online,
597         .set_charge             = magician_set_charge,
598         .supplied_to            = magician_supplicants,
599         .num_supplicants        = ARRAY_SIZE(magician_supplicants),
600 };
601
602 static struct resource power_supply_resources[] = {
603         [0] = {
604                 .name   = "ac",
605                 .flags  = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE |
606                         IORESOURCE_IRQ_LOWEDGE,
607                 .start  = IRQ_MAGICIAN_VBUS,
608                 .end    = IRQ_MAGICIAN_VBUS,
609         },
610         [1] = {
611                 .name   = "usb",
612                 .flags  = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE |
613                         IORESOURCE_IRQ_LOWEDGE,
614                 .start  = IRQ_MAGICIAN_VBUS,
615                 .end    = IRQ_MAGICIAN_VBUS,
616         },
617 };
618
619 static struct platform_device power_supply = {
620         .name   = "pda-power",
621         .id     = -1,
622         .dev = {
623                 .platform_data = &power_supply_info,
624         },
625         .resource       = power_supply_resources,
626         .num_resources  = ARRAY_SIZE(power_supply_resources),
627 };
628
629 /*
630  * Battery charger
631  */
632
633 static struct regulator_consumer_supply bq24022_consumers[] = {
634         REGULATOR_SUPPLY("vbus_draw", NULL),
635         REGULATOR_SUPPLY("ac_draw", NULL),
636 };
637
638 static struct regulator_init_data bq24022_init_data = {
639         .constraints = {
640                 .max_uA         = 500000,
641                 .valid_ops_mask = REGULATOR_CHANGE_CURRENT |
642                         REGULATOR_CHANGE_STATUS,
643         },
644         .num_consumer_supplies  = ARRAY_SIZE(bq24022_consumers),
645         .consumer_supplies      = bq24022_consumers,
646 };
647
648
649 static enum gpiod_flags bq24022_gpiod_gflags[] = { GPIOD_OUT_LOW };
650
651 static struct gpio_regulator_state bq24022_states[] = {
652         { .value = 100000, .gpios = (0 << 0) },
653         { .value = 500000, .gpios = (1 << 0) },
654 };
655
656 static struct gpio_regulator_config bq24022_info = {
657         .supply_name            = "bq24022",
658
659         .enabled_at_boot        = 1,
660
661         .gflags = bq24022_gpiod_gflags,
662         .ngpios = ARRAY_SIZE(bq24022_gpiod_gflags),
663
664         .states                 = bq24022_states,
665         .nr_states              = ARRAY_SIZE(bq24022_states),
666
667         .type                   = REGULATOR_CURRENT,
668         .init_data              = &bq24022_init_data,
669 };
670
671 static struct platform_device bq24022 = {
672         .name   = "gpio-regulator",
673         .id     = -1,
674         .dev    = {
675                 .platform_data = &bq24022_info,
676         },
677 };
678
679 static struct gpiod_lookup_table bq24022_gpiod_table = {
680         .dev_id = "gpio-regulator",
681         .table = {
682                 GPIO_LOOKUP("gpio-pxa", EGPIO_MAGICIAN_BQ24022_ISET2,
683                             NULL, GPIO_ACTIVE_HIGH),
684                 GPIO_LOOKUP("gpio-pxa", GPIO30_MAGICIAN_BQ24022_nCHARGE_EN,
685                             "enable", GPIO_ACTIVE_LOW),
686                 { },
687         },
688 };
689
690 /*
691  * fixed regulator for ads7846
692  */
693
694 static struct regulator_consumer_supply ads7846_supply =
695         REGULATOR_SUPPLY("vcc", "spi2.0");
696
697 static struct regulator_init_data vads7846_regulator = {
698         .constraints    = {
699                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
700         },
701         .num_consumer_supplies  = 1,
702         .consumer_supplies      = &ads7846_supply,
703 };
704
705 static struct fixed_voltage_config vads7846 = {
706         .supply_name    = "vads7846",
707         .microvolts     = 3300000, /* probably */
708         .startup_delay  = 0,
709         .init_data      = &vads7846_regulator,
710 };
711
712 static struct platform_device vads7846_device = {
713         .name   = "reg-fixed-voltage",
714         .id     = -1,
715         .dev    = {
716                 .platform_data  = &vads7846,
717         },
718 };
719
720 /*
721  * Vcore regulator MAX1587A
722  */
723
724 static struct regulator_consumer_supply magician_max1587a_consumers[] = {
725         REGULATOR_SUPPLY("vcc_core", NULL),
726 };
727
728 static struct regulator_init_data magician_max1587a_v3_info = {
729         .constraints = {
730                 .name           = "vcc_core range",
731                 .min_uV         = 700000,
732                 .max_uV         = 1475000,
733                 .always_on      = 1,
734                 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,
735         },
736         .consumer_supplies      = magician_max1587a_consumers,
737         .num_consumer_supplies  = ARRAY_SIZE(magician_max1587a_consumers),
738 };
739
740 static struct max1586_subdev_data magician_max1587a_subdevs[] = {
741         {
742                 .name           = "vcc_core",
743                 .id             = MAX1586_V3,
744                 .platform_data  = &magician_max1587a_v3_info,
745         }
746 };
747
748 static struct max1586_platform_data magician_max1587a_info = {
749         .subdevs     = magician_max1587a_subdevs,
750         .num_subdevs = ARRAY_SIZE(magician_max1587a_subdevs),
751         /*
752          * NOTICE measured directly on the PCB (board_id == 0x3a), but
753          * if R24 is present, it will boost the voltage
754          * (write 1.475V, get 1.645V and smoke)
755          */
756         .v3_gain     = MAX1586_GAIN_NO_R24,
757 };
758
759 static struct i2c_board_info magician_pwr_i2c_board_info[] __initdata = {
760         {
761                 I2C_BOARD_INFO("max1586", 0x14),
762                 .platform_data  = &magician_max1587a_info,
763         },
764 };
765
766 /*
767  * MMC/SD
768  */
769
770 static int magician_mci_init(struct device *dev,
771         irq_handler_t detect_irq, void *data)
772 {
773         return request_irq(IRQ_MAGICIAN_SD, detect_irq, 0,
774                 "mmc card detect", data);
775 }
776
777 static void magician_mci_exit(struct device *dev, void *data)
778 {
779         free_irq(IRQ_MAGICIAN_SD, data);
780 }
781
782 static struct pxamci_platform_data magician_mci_info = {
783         .ocr_mask               = MMC_VDD_32_33|MMC_VDD_33_34,
784         .init                   = magician_mci_init,
785         .exit                   = magician_mci_exit,
786         .gpio_card_ro_invert    = 1,
787 };
788
789 /*
790  * Write protect on EGPIO register 5 index 4, this is on the second HTC
791  * EGPIO chip which starts at register 4, so we need offset 8+4=12 on that
792  * particular chip.
793  */
794 #define EGPIO_MAGICIAN_nSD_READONLY_OFFSET 12
795 /*
796  * Power on EGPIO register 2 index 0, so this is on the first HTC EGPIO chip
797  * starting at register 0 so we need offset 2*8+0 = 16 on that chip.
798  */
799 #define EGPIO_MAGICIAN_nSD_POWER_OFFSET 16
800
801 static struct gpiod_lookup_table magician_mci_gpio_table = {
802         .dev_id = "pxa2xx-mci.0",
803         .table = {
804                 GPIO_LOOKUP("htc-egpio-1", EGPIO_MAGICIAN_nSD_READONLY_OFFSET,
805                             "wp", GPIO_ACTIVE_HIGH),
806                 GPIO_LOOKUP("htc-egpio-0", EGPIO_MAGICIAN_nSD_POWER_OFFSET,
807                             "power", GPIO_ACTIVE_HIGH),
808                 { },
809         },
810 };
811
812 /*
813  * USB OHCI
814  */
815
816 static struct pxaohci_platform_data magician_ohci_info = {
817         .port_mode      = PMM_PERPORT_MODE,
818         /* port1: CSR Bluetooth, port2: OTG with UDC */
819         .flags          = ENABLE_PORT1 | ENABLE_PORT2 | POWER_CONTROL_LOW,
820         .power_budget   = 0,
821         .power_on_delay = 100,
822 };
823
824 /*
825  * StrataFlash
826  */
827
828 static int magician_flash_init(struct platform_device *pdev)
829 {
830         int ret = gpio_request(EGPIO_MAGICIAN_FLASH_VPP, "flash Vpp enable");
831
832         if (ret) {
833                 pr_err("Cannot request flash enable GPIO (%i)\n", ret);
834                 return ret;
835         }
836
837         ret = gpio_direction_output(EGPIO_MAGICIAN_FLASH_VPP, 1);
838         if (ret) {
839                 pr_err("Cannot set direction for flash enable (%i)\n", ret);
840                 gpio_free(EGPIO_MAGICIAN_FLASH_VPP);
841         }
842
843         return ret;
844 }
845
846 static void magician_set_vpp(struct platform_device *pdev, int vpp)
847 {
848         gpio_set_value(EGPIO_MAGICIAN_FLASH_VPP, vpp);
849 }
850
851 static void magician_flash_exit(struct platform_device *pdev)
852 {
853         gpio_free(EGPIO_MAGICIAN_FLASH_VPP);
854 }
855
856 static struct resource strataflash_resource = {
857         .start  = PXA_CS0_PHYS,
858         .end    = PXA_CS0_PHYS + SZ_64M - 1,
859         .flags  = IORESOURCE_MEM,
860 };
861
862 static struct mtd_partition magician_flash_parts[] = {
863         {
864                 .name           = "Bootloader",
865                 .offset         = 0x0,
866                 .size           = 0x40000,
867                 .mask_flags     = MTD_WRITEABLE, /* EXPERIMENTAL */
868         },
869         {
870                 .name           = "Linux Kernel",
871                 .offset         = 0x40000,
872                 .size           = MTDPART_SIZ_FULL,
873         },
874 };
875
876 /*
877  * physmap-flash driver
878  */
879
880 static struct physmap_flash_data strataflash_data = {
881         .width          = 4,
882         .init           = magician_flash_init,
883         .set_vpp        = magician_set_vpp,
884         .exit           = magician_flash_exit,
885         .parts          = magician_flash_parts,
886         .nr_parts       = ARRAY_SIZE(magician_flash_parts),
887 };
888
889 static struct platform_device strataflash = {
890         .name           = "physmap-flash",
891         .id             = -1,
892         .resource       = &strataflash_resource,
893         .num_resources  = 1,
894         .dev = {
895                 .platform_data = &strataflash_data,
896         },
897 };
898
899 /*
900  * PXA I2C main controller
901  */
902
903 static struct i2c_pxa_platform_data i2c_info = {
904         /* OV9640 I2C device doesn't support fast mode */
905         .fast_mode      = 0,
906 };
907
908 /*
909  * PXA I2C power controller
910  */
911
912 static struct i2c_pxa_platform_data magician_i2c_power_info = {
913         .fast_mode      = 1,
914 };
915
916 /*
917  * Touchscreen
918  */
919
920 static struct ads7846_platform_data ads7846_pdata = {
921         .model          = 7846,
922         .x_plate_ohms   = 317,
923         .y_plate_ohms   = 500,
924         .pressure_max   = 1023, /* with x plate ohms it will overflow 255 */
925         .debounce_max   = 3,    /* first readout is always bad */
926         .debounce_tol   = 30,
927         .debounce_rep   = 0,
928         .gpio_pendown   = GPIO115_MAGICIAN_nPEN_IRQ,
929         .keep_vref_on   = 1,
930         .wakeup         = true,
931         .vref_delay_usecs               = 100,
932         .penirq_recheck_delay_usecs     = 100,
933 };
934
935 struct pxa2xx_spi_chip tsc2046_chip_info = {
936         .tx_threshold   = 1,
937         .rx_threshold   = 2,
938         .timeout        = 64,
939         /* NOTICE must be GPIO, incompatibility with hw PXA SPI framing */
940         .gpio_cs        = GPIO14_MAGICIAN_TSC2046_CS,
941 };
942
943 static struct pxa2xx_spi_controller magician_spi_info = {
944         .num_chipselect = 1,
945         .enable_dma     = 1,
946 };
947
948 static struct spi_board_info ads7846_spi_board_info[] __initdata = {
949         {
950                 .modalias               = "ads7846",
951                 .bus_num                = 2,
952                 .max_speed_hz           = 2500000,
953                 .platform_data          = &ads7846_pdata,
954                 .controller_data        = &tsc2046_chip_info,
955                 .irq = PXA_GPIO_TO_IRQ(GPIO115_MAGICIAN_nPEN_IRQ),
956         },
957 };
958
959 /*
960  * Platform devices
961  */
962
963 static struct platform_device *devices[] __initdata = {
964         &gpio_keys,
965         &egpio,
966         &backlight,
967         &pasic3,
968         &bq24022,
969         &gpio_vbus,
970         &power_supply,
971         &strataflash,
972         &leds_gpio,
973         &vads7846_device,
974 };
975
976 static struct gpio magician_global_gpios[] = {
977         { GPIO13_MAGICIAN_CPLD_IRQ, GPIOF_IN, "CPLD_IRQ" },
978         { GPIO107_MAGICIAN_DS1WM_IRQ, GPIOF_IN, "DS1WM_IRQ" },
979
980         /* NOTICE valid LCD init sequence */
981         { GPIO106_MAGICIAN_LCD_DCDC_NRESET, GPIOF_OUT_INIT_LOW, "LCD DCDC nreset" },
982         { GPIO104_MAGICIAN_LCD_VOFF_EN, GPIOF_OUT_INIT_LOW, "LCD VOFF enable" },
983         { GPIO105_MAGICIAN_LCD_VON_EN, GPIOF_OUT_INIT_LOW, "LCD VON enable" },
984 };
985
986 static void __init magician_init(void)
987 {
988         void __iomem *cpld;
989         int lcd_select;
990         int err;
991
992         pxa2xx_mfp_config(ARRAY_AND_SIZE(magician_pin_config));
993         err = gpio_request_array(ARRAY_AND_SIZE(magician_global_gpios));
994         if (err)
995                 pr_err("magician: Failed to request global GPIOs: %d\n", err);
996
997         pxa_set_ffuart_info(NULL);
998         pxa_set_btuart_info(NULL);
999
1000         pwm_add_table(magician_pwm_lookup, ARRAY_SIZE(magician_pwm_lookup));
1001
1002         pxa_set_ficp_info(&magician_ficp_info);
1003         pxa27x_set_i2c_power_info(&magician_i2c_power_info);
1004         pxa_set_i2c_info(&i2c_info);
1005
1006         i2c_register_board_info(1,
1007                 ARRAY_AND_SIZE(magician_pwr_i2c_board_info));
1008
1009         gpiod_add_lookup_table(&magician_mci_gpio_table);
1010         pxa_set_mci_info(&magician_mci_info);
1011         pxa_set_ohci_info(&magician_ohci_info);
1012         pxa_set_udc_info(&magician_udc_info);
1013
1014         /* Check LCD type we have */
1015         cpld = ioremap_nocache(PXA_CS3_PHYS, 0x1000);
1016         if (cpld) {
1017                 u8 board_id = __raw_readb(cpld + 0x14);
1018
1019                 iounmap(cpld);
1020                 system_rev = board_id & 0x7;
1021                 lcd_select = board_id & 0x8;
1022                 pr_info("LCD type: %s\n", lcd_select ? "Samsung" : "Toppoly");
1023                 if (lcd_select && (system_rev < 3))
1024                         /* NOTICE valid LCD init sequence */
1025                         gpio_request_one(GPIO75_MAGICIAN_SAMSUNG_POWER,
1026                                 GPIOF_OUT_INIT_LOW, "Samsung LCD Power");
1027                 pxa_set_fb_info(NULL,
1028                         lcd_select ? &samsung_info : &toppoly_info);
1029         } else
1030                 pr_err("LCD detection: CPLD mapping failed\n");
1031
1032         pxa2xx_set_spi_info(2, &magician_spi_info);
1033         spi_register_board_info(ARRAY_AND_SIZE(ads7846_spi_board_info));
1034
1035         regulator_register_always_on(0, "power", pwm_backlight_supply,
1036                 ARRAY_SIZE(pwm_backlight_supply), 5000000);
1037
1038         gpiod_add_lookup_table(&bq24022_gpiod_table);
1039         platform_add_devices(ARRAY_AND_SIZE(devices));
1040 }
1041
1042 MACHINE_START(MAGICIAN, "HTC Magician")
1043         .atag_offset    = 0x100,
1044         .map_io         = pxa27x_map_io,
1045         .nr_irqs        = MAGICIAN_NR_IRQS,
1046         .init_irq       = pxa27x_init_irq,
1047         .handle_irq     = pxa27x_handle_irq,
1048         .init_machine   = magician_init,
1049         .init_time      = pxa_timer_init,
1050         .restart        = pxa_restart,
1051 MACHINE_END