[ubicom32]: move new files out from platform support patch
[openwrt.git] / target / linux / ubicom32 / files / arch / ubicom32 / mach-ip7k / board-ip7145dpf.c
1 /*
2  * arch/ubicom32/mach-ip7k/board-ip7145dpf.c
3  *   Board file for IP7145DPF, rev 1.0, P/N 8007-0410
4  *
5  * (C) Copyright 2009, Ubicom, Inc.
6  *
7  * This file is part of the Ubicom32 Linux Kernel Port.
8  *
9  * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10  * it and/or modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation, either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  * the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with the Ubicom32 Linux Kernel Port.  If not,
21  * see <http://www.gnu.org/licenses/>.
22  *
23  * Ubicom32 implementation derived from (with many thanks):
24  *   arch/m68knommu
25  *   arch/blackfin
26  *   arch/parisc
27  */
28 #include <linux/device.h>
29 #include <linux/platform_device.h>
30 #include <linux/gpio.h>
31
32 #include <linux/input.h>
33
34 #include <linux/i2c.h>
35 #include <linux/i2c-gpio.h>
36 #include <linux/i2c/pca953x.h>
37
38 #include <asm/board.h>
39 #include <asm/machdep.h>
40 #include <asm/ubicom32input.h>
41 #include <asm/ubicom32input_i2c.h>
42 #include <asm/ubicom32bl.h>
43 #include <asm/ubicom32lcdpower.h>
44 #include <asm/vdc_tio.h>
45
46 #include <asm/ubicom32sd.h>
47 #include <asm/sd_tio.h>
48 #include <asm/devtree.h>
49 #include <asm/audio.h>
50
51 #include <asm/ring_tio.h>
52
53 /******************************************************************************
54  * SD/IO Port F (Slot 1) platform data
55  */
56 static struct resource ip7145dpf_portf_sd_resources[] = {
57         /*
58          * Send IRQ
59          */
60         [0] = {
61                 /*
62                  * The init routine will query the devtree and fill this in
63                  */
64                 .flags  = IORESOURCE_IRQ,
65         },
66
67         /*
68          * Receive IRQ
69          */
70         [1] = {
71                 /*
72                  * The init routine will query the devtree and fill this in
73                  */
74                 .flags  = IORESOURCE_IRQ,
75         },
76
77         /*
78          * Memory Mapped Registers
79          */
80         [2] = {
81                 /*
82                  * The init routine will query the devtree and fill this in
83                  */
84                 .flags  = IORESOURCE_MEM,
85         },
86 };
87
88 static struct ubicom32sd_card ip7145dpf_portf_sd_cards[] = {
89         [0] = {
90                 .pin_wp         = IP7145DPF_IOB0,
91                 .wp_polarity    = 1,
92                 .pin_pwr        = IP7145DPF_IOB4,
93                 .pin_cd         = GPIO_RA_4,
94         },
95         [1] = {
96                 .pin_wp         = IP7145DPF_IOB1,
97                 .wp_polarity    = 1,
98                 .pin_pwr        = IP7145DPF_IOB5,
99                 .pin_cd         = GPIO_RA_6,
100         },
101 };
102
103 static struct ubicom32sd_platform_data ip7145dpf_portf_sd_platform_data = {
104         .ncards         = 2,
105         .cards          = ip7145dpf_portf_sd_cards,
106 };
107
108 static struct platform_device ip7145dpf_portf_sd_device = {
109         .name           = "ubicom32sd",
110         .id             = 0,
111         .resource       = ip7145dpf_portf_sd_resources,
112         .num_resources  = ARRAY_SIZE(ip7145dpf_portf_sd_resources),
113         .dev            = {
114                         .platform_data = &ip7145dpf_portf_sd_platform_data,
115         },
116
117 };
118
119 /*
120  * ip7145dpf_portf_sd_init
121  */
122 static void ip7145dpf_portf_sd_init(void)
123 {
124         /*
125          * Check the device tree for the sd_tio
126          */
127         struct sd_tio_node *sd_node = (struct sd_tio_node *)devtree_find_node("portf_sd");
128         if (!sd_node) {
129                 printk(KERN_INFO "PortF SDTIO not found\n");
130                 return;
131         }
132
133         /*
134          * Fill in the resources and platform data from devtree information
135          */
136         ip7145dpf_portf_sd_resources[0].start = sd_node->dn.sendirq;
137         ip7145dpf_portf_sd_resources[1].start = sd_node->dn.recvirq;
138         ip7145dpf_portf_sd_resources[2].start = (u32_t)&(sd_node->regs);
139         ip7145dpf_portf_sd_resources[2].end = (u32_t)&(sd_node->regs) + sizeof(sd_node->regs);
140
141         platform_device_register(&ip7145dpf_portf_sd_device);
142 }
143
144 /******************************************************************************
145  * SD/IO Port B (Slot 2) platform data
146  */
147 static struct resource ip7145dpf_portb_sd_resources[] = {
148         /*
149          * Send IRQ
150          */
151         [0] = {
152                 /*
153                  * The init routine will query the devtree and fill this in
154                  */
155                 .flags  = IORESOURCE_IRQ,
156         },
157
158         /*
159          * Receive IRQ
160          */
161         [1] = {
162                 /*
163                  * The init routine will query the devtree and fill this in
164                  */
165                 .flags  = IORESOURCE_IRQ,
166         },
167
168         /*
169          * Memory Mapped Registers
170          */
171         [2] = {
172                 /*
173                  * The init routine will query the devtree and fill this in
174                  */
175                 .flags  = IORESOURCE_MEM,
176         },
177 };
178
179 static struct ubicom32sd_card ip7145dpf_portb_sd_cards[] = {
180         [0] = {
181                 .pin_wp         = IP7145DPF_IOB2,
182                 .wp_polarity    = 1,
183                 .pin_pwr        = IP7145DPF_IOB6,
184                 .pin_cd         = IP7145DPF_IOB3,
185         },
186 };
187
188 static struct ubicom32sd_platform_data ip7145dpf_portb_sd_platform_data = {
189         .ncards         = 1,
190         .cards          = ip7145dpf_portb_sd_cards,
191 };
192
193 static struct platform_device ip7145dpf_portb_sd_device = {
194         .name           = "ubicom32sd",
195         .id             = 1,
196         .resource       = ip7145dpf_portb_sd_resources,
197         .num_resources  = ARRAY_SIZE(ip7145dpf_portb_sd_resources),
198         .dev            = {
199                         .platform_data = &ip7145dpf_portb_sd_platform_data,
200         },
201
202 };
203
204 /*
205  * ip7145dpf_portb_sd_init
206  */
207 static void ip7145dpf_portb_sd_init(void)
208 {
209         /*
210          * Check the device tree for the sd_tio
211          */
212         struct sd_tio_node *sd_node = (struct sd_tio_node *)devtree_find_node("portb_sd");
213         if (!sd_node) {
214                 printk(KERN_INFO "PortB SDTIO not found\n");
215                 return;
216         }
217
218         /*
219          * Fill in the resources and platform data from devtree information
220          */
221         ip7145dpf_portb_sd_resources[0].start = sd_node->dn.sendirq;
222         ip7145dpf_portb_sd_resources[1].start = sd_node->dn.recvirq;
223         ip7145dpf_portb_sd_resources[2].start = (u32_t)&(sd_node->regs);
224         ip7145dpf_portb_sd_resources[2].end = (u32_t)&(sd_node->regs) + sizeof(sd_node->regs);
225
226         platform_device_register(&ip7145dpf_portb_sd_device);
227 }
228
229
230 #ifdef IP7145DPF_USE_MMC_SPI
231 /******************************************************************************
232  * SPI over GPIO (MMC_SPI)
233  */
234 #include <linux/spi/spi.h>
235 #include <linux/spi/mmc_spi.h>
236 #include <linux/mmc/host.h>
237 #include <asm/ubicom32-spi-gpio.h>
238
239 #define MMC_CS  GPIO_RF_5       // PF5 D3
240 #define MMC_CD  GPIO_RA_4       // PA4 CD
241 #define MMC_WP  IP7145DPF_IOB0  // IOB0 WP
242 #define MMC_PWR IP7145DPF_IOB4  // IOB4 PWR
243
244 /*
245  * SPI bus over GPIO (for SD card)
246  */
247 static struct ubicom32_spi_gpio_platform_data ip7145dpf_spi_gpio_data = {
248         .pin_mosi       = GPIO_RF_0,    // PF0 CMD
249         .pin_miso       = GPIO_RF_2,    // PF2 D0
250         .pin_clk        = GPIO_RF_1,    // PF1 CLK
251         .bus_num        = 0,            // We'll call this SPI bus 0
252         .num_chipselect = 1,            // only one device on this SPI bus
253 };
254
255 static struct platform_device ip7145dpf_spi_gpio_device = {
256         .name   = "ubicom32-spi-gpio",
257         .id     = 0,
258         .dev    = {
259                 .platform_data = &ip7145dpf_spi_gpio_data,
260         },
261 };
262
263 /*
264  * ip7145dpf_mmc_spi_setpower_slot_a
265  *      Set the power state for slot A
266  */
267 static void ip7145dpf_mmc_spi_setpower_slot_a(struct device *dev, unsigned int vdd)
268 {
269         struct mmc_spi_platform_data *pd = dev->platform_data;
270
271         /*
272          * Power is inverted, we could tell the IOB to do it, but it's cleaner this way.
273          */
274         if ((1 << vdd) & pd->ocr_mask) {
275                 gpio_set_value(MMC_PWR, 0);
276                 return;
277         }
278         gpio_set_value(MMC_PWR, 1);
279 }
280
281 /*
282  * ip7145dpf_mmc_spi_get_cd_slot_a
283  *      Get the CD bit for slot A
284  */
285 static int ip7145dpf_mmc_spi_get_cd_slot_a(struct device *dev)
286 {
287         /*
288          * Note that the sense of the GPIO is inverted
289          */
290         return !gpio_get_value(MMC_CD);
291 }
292
293 /*
294  * ip7145dpf_mmc_spi_get_ro_slot_a
295  *      Get the WP bit for slot A
296  */
297 static int ip7145dpf_mmc_spi_get_ro_slot_a(struct device *dev)
298 {
299         /*
300          * Note that the sense of the GPIO is inverted, we could tell the IOB to do it, but
301          * it's clearer this way.
302          */
303         return !gpio_get_value(MMC_WP);
304 }
305
306 /*
307  * ip7145dpf_mmc_spi_exit_slot_a
308  *      Free the appropriate GPIOs for slot A SD slot.
309  */
310 static void ip7145dpf_mmc_spi_exit_slot_a(struct device *dev, void *appdata)
311 {
312         gpio_free(MMC_CD);
313         gpio_free(MMC_CS);
314         gpio_free(MMC_WP);
315         gpio_free(MMC_PWR);
316         platform_device_unregister(&ip7145dpf_spi_gpio_device);
317 }
318
319 /*
320  * ip7145dpf_mmc_spi_init_slot_a
321  *      Allocate the appropriate GPIOs for slot A SD slot.
322  *      WP is on IOB0, CD is PA4, CS is on PF5
323  *      TODO: make CD an interrupt
324  */
325 static int ip7145dpf_mmc_spi_init_slot_a(void)
326 {
327         int ret = gpio_request(MMC_CD, "mmc-a-cd");
328         if (ret) {
329                 printk(KERN_ERR "%s: could not request mmc-a-cd pin\n", __FUNCTION__);
330                 return -ENOSYS;
331         }
332         gpio_direction_input(MMC_CD);
333
334         ret = gpio_request(MMC_CS, "mmc-a-cs");
335         if (ret) {
336                 printk(KERN_ERR "%s: could not request mmc-a-cs pin\n", __FUNCTION__);
337                 goto no_cs;
338         }
339         gpio_direction_output(MMC_CS, 0);
340
341         ret = gpio_request(MMC_WP, "mmc-a-wp");
342         if (ret) {
343                 printk(KERN_ERR "%s: could not request mmc-a-wp pin\n", __FUNCTION__);
344                 goto no_wp;
345         }
346         gpio_direction_input(MMC_WP);
347
348         /*
349          * Start off with power off
350          */
351         ret = gpio_request(MMC_PWR, "mmc-a-pwr");
352         if (ret) {
353                 printk(KERN_ERR "%s: could not request mmc-a-pwr pin\n", __FUNCTION__);
354                 goto no_pwr;
355         }
356         ret = gpio_direction_output(MMC_PWR, 1);
357
358         return 0;
359
360 no_pwr:
361         gpio_free(MMC_WP);
362
363 no_wp:
364         gpio_free(MMC_CS);
365
366 no_cs:
367         gpio_free(MMC_CD);
368         return -ENOSYS;
369 }
370
371 /*
372  * MMC_SPI driver (currently bitbang)
373  */
374 static struct mmc_spi_platform_data ip7145dpf_mmc_platform_data = {
375         .ocr_mask       = MMC_VDD_33_34,
376         .exit           = ip7145dpf_mmc_spi_exit_slot_a,
377         .get_ro         = ip7145dpf_mmc_spi_get_ro_slot_a,
378         .get_cd         = ip7145dpf_mmc_spi_get_cd_slot_a,
379
380         .setpower       = ip7145dpf_mmc_spi_setpower_slot_a,
381         .powerup_msecs  = 500,
382
383         .detect_delay   = 100,
384
385         .caps           = MMC_CAP_NEEDS_POLL,
386 };
387
388 static struct ubicom32_spi_gpio_controller_data ip7145dpf_mmc_controller_data = {
389         .pin_cs =  MMC_CS,
390 };
391
392 static struct spi_board_info ip7145dpf_spi_board_info[] = {
393         {
394                 .modalias = "mmc_spi",
395                 .bus_num = 0,
396                 .chip_select = 0,
397                 .max_speed_hz = 2000000,
398                 .platform_data = &ip7145dpf_mmc_platform_data,
399                 .controller_data = &ip7145dpf_mmc_controller_data,
400         }
401 };
402 #endif /* IP7145DPF_USE_MMC_SPI */
403
404 /*
405  * ip7145dpf_u72_setup
406  *      Called by I2C to tell us that u72 is setup.
407  *
408  * This function is called by I2C to tell us that u72 has been setup.  All
409  * devices which rely on this chip being initialized (or even present) need to
410  * be initialized in this function otherwise they may get initialized too early.
411  *
412  * Currently the only device depending on u72 is the SPI
413  */
414 static int __init ip7145dpf_u72_setup(struct i2c_client *client, unsigned gpio, unsigned ngpio, void *context)
415 {
416 #ifdef IP7145DPF_USE_MMC_SPI
417         if (ip7145dpf_mmc_spi_init_slot_a()) {
418                 printk(KERN_ERR "%s: could not request mmc resources\n", __FUNCTION__);
419         } else {
420                 printk(KERN_INFO "%s: registering SPI resources\n", __FUNCTION__);
421                 spi_register_board_info(ip7145dpf_spi_board_info, ARRAY_SIZE(ip7145dpf_spi_board_info));
422                 platform_device_register(&ip7145dpf_spi_gpio_device);
423         }
424 #else
425         /*
426          * Initialize the Port F/Port B SD slots
427          */
428         ip7145dpf_portf_sd_init();
429         ip7145dpf_portb_sd_init();
430 #endif
431         return 0;
432 }
433
434 /******************************************************************************
435  * LCD VGH on the board at PE6
436  */
437 static struct ubicom32lcdpower_platform_data ip7145dpf_lcdpower_data = {
438         .vgh_gpio               = GPIO_RE_6,
439         .vgh_polarity           = true,
440 };
441
442 static struct platform_device ip7145dpf_lcdpower_device = {
443         .name   = "ubicom32lcdpower",
444         .id     = -1,
445         .dev    = {
446                 .platform_data = &ip7145dpf_lcdpower_data,
447         },
448 };
449
450 /******************************************************************************
451  * Backlight on the board PD0, hardware PWM
452  */
453 static struct ubicom32bl_platform_data ip7145dpf_backlight_data = {
454         .type                   = UBICOM32BL_TYPE_PWM,
455         .pwm_channel            = 2,
456         .pwm_prescale           = 15,
457         .pwm_period             = 60,
458         .default_intensity      = 0x80,
459 };
460
461 static struct platform_device ip7145dpf_backlight_device = {
462         .name   = "ubicom32bl",
463         .id     = -1,
464         .dev    = {
465                 .platform_data = &ip7145dpf_backlight_data,
466         },
467 };
468
469 /******************************************************************************
470  * Ubicom32Input on I2C, U48 MAX7310, address 0x18, 8 bits
471  */
472 static struct ubicom32input_i2c_button ip7145dpf_ubicom32input_i2c_u48_buttons[] = {
473         {
474                 .type           = EV_KEY,
475                 .code           = KEY_UP,
476                 .bit            = 0,
477                 .active_low     = 1,
478         },
479         {
480                 .type           = EV_KEY,
481                 .code           = KEY_LEFT,
482                 .bit            = 1,
483                 .active_low     = 1,
484         },
485         {
486                 .type           = EV_KEY,
487                 .code           = KEY_RIGHT,
488                 .bit            = 2,
489                 .active_low     = 1,
490         },
491         {
492                 .type           = EV_KEY,
493                 .code           = KEY_DOWN,
494                 .bit            = 3,
495                 .active_low     = 1,
496         },
497         {
498                 .type           = EV_KEY,
499                 .code           = KEY_ENTER,
500                 .bit            = 4,
501                 .active_low     = 1,
502         },
503         {
504                 .type           = EV_KEY,
505                 .code           = KEY_MENU,
506                 .bit            = 5,
507                 .active_low     = 1,
508         },
509         {
510                 .type           = EV_KEY,
511                 .code           = KEY_ESC,
512                 .bit            = 6,
513                 .active_low     = 1,
514         },
515 };
516
517 static struct ubicom32input_i2c_platform_data ip7145dpf_ubicom32input_i2c_u48_platform_data = {
518         .buttons        = ip7145dpf_ubicom32input_i2c_u48_buttons,
519         .nbuttons       = ARRAY_SIZE(ip7145dpf_ubicom32input_i2c_u48_buttons),
520         .name           = "Ubicom32 Input I2C U48",
521 };
522
523 /******************************************************************************
524  * Additional GPIO chips
525  */
526 static struct pca953x_platform_data ip7145dpf_gpio_u72_platform_data = {
527         .gpio_base = IP7145DPF_U72_BASE,
528         .setup = ip7145dpf_u72_setup,
529 };
530
531 /******************************************************************************
532  * Devices on the I2C bus
533  */
534 static struct i2c_board_info __initdata ip7145dpf_i2c_board_info[] = {
535         /*
536          * U51, S35390A RTC, address 0x30
537          */
538         {
539                 .type           = "s35390a",
540                 .addr           = 0x30,
541         },
542
543         /*
544          * U48, MAX7310 IO expander, 8 bits, address 0x18
545          */
546         {
547                 .type           = "ubicom32in_max7310",
548                 .addr           = 0x18,
549                 .platform_data  = &ip7145dpf_ubicom32input_i2c_u48_platform_data,
550         },
551
552         /*
553          * U72, MAX7310 IOB expander, 8 bits, address 0x19
554          */
555         {
556                 .type           = "max7310",
557                 .addr           = 0x19,
558                 .platform_data  = &ip7145dpf_gpio_u72_platform_data,
559         },
560 };
561
562 /*
563  * I2C bus on the board, SDA PE1, SCL PE2
564  */
565 static struct i2c_gpio_platform_data ip7145dpf_i2c_data = {
566         .sda_pin                = GPIO_RE_1,
567         .scl_pin                = GPIO_RE_2,
568         .sda_is_open_drain      = 0,
569         .scl_is_open_drain      = 0,
570 };
571
572 static struct platform_device ip7145dpf_i2c_device = {
573         .name   = "i2c-gpio",
574         .id     = 0,
575         .dev    = {
576                 .platform_data = &ip7145dpf_i2c_data,
577         },
578 };
579
580 /******************************************************************************
581  * Use ubicom32input driver to monitor the various pushbuttons on this board.
582  *
583  * WPS                  PF12
584  * FACT_DEFAULT         PF13
585  * POWER                PE4
586  *
587  * Not sutable for the keypad buttons since those run on I2C GPIO.  The polling
588  * of ubicom32input would seem to be excessive for this.
589  *
590  * TODO: pick some ubicom understood EV_xxx define for WPS and Fact Default
591  */
592 static struct ubicom32input_button ip7145dpf_ubicom32input_buttons[] = {
593         {
594                 .type           = EV_KEY,
595                 .code           = KEY_FN_F1,
596                 .gpio           = GPIO_RF_12,
597                 .desc           = "WPS",
598                 .active_low     = 1,
599         },
600         {
601                 .type           = EV_KEY,
602                 .code           = KEY_FN_F2,
603                 .gpio           = GPIO_RF_13,
604                 .desc           = "Factory Default",
605                 .active_low     = 1,
606         },
607         {
608                 .type           = EV_KEY,
609                 .code           = KEY_POWER,
610                 .gpio           = GPIO_RE_4,
611                 .desc           = "Power",
612                 .active_low     = 1,
613         },
614 };
615
616 static struct ubicom32input_platform_data ip7145dpf_ubicom32input_data = {
617         .buttons        = ip7145dpf_ubicom32input_buttons,
618         .nbuttons       = ARRAY_SIZE(ip7145dpf_ubicom32input_buttons),
619 };
620
621 static struct platform_device ip7145dpf_ubicom32input_device = {
622         .name   = "ubicom32input",
623         .id     = -1,
624         .dev    = {
625                 .platform_data = &ip7145dpf_ubicom32input_data,
626         },
627 };
628
629 /*
630  * List of all devices in our system
631  */
632 static struct platform_device *ip7145dpf_devices[] __initdata = {
633         &ip7145dpf_i2c_device,
634         &ip7145dpf_lcdpower_device,
635         &ip7145dpf_backlight_device,
636         &ip7145dpf_ubicom32input_device,
637 };
638
639 /*
640  * ip7145dpf_power_off
641  *      Called to turn the power off for this board
642  */
643 static void ip7145dpf_power_off(void)
644 {
645         gpio_set_value(GPIO_RE_5, 0);
646 }
647
648 /*
649  * ip7145dpf_init
650  *      Called to add the devices which we have on this board
651  */
652 static int __init ip7145dpf_init(void)
653 {
654         int ret;
655         struct platform_device *audio_dev;
656
657         ubi_gpio_init();
658
659 #ifdef CONFIG_UIO_UBICOM32RING
660         ring_tio_init("decoder_ring");
661 #endif
662
663         /*
664          * Start up the video driver first
665          */
666         vdc_tio_init();
667
668         /*
669          * Take over holding of the power from the system
670          */
671         ret = gpio_request(GPIO_RE_5, "power_hold");
672         if (ret) {
673                 printk(KERN_ERR "%s: could not request power hold GPIO\n", __FUNCTION__);
674         }
675         gpio_direction_output(GPIO_RE_5, 1);
676         mach_power_off = ip7145dpf_power_off;
677
678         /*
679          * USB SEL_HOST_USB line
680          */
681         ret = gpio_request(GPIO_RF_11, "SEL_HOST_USB");
682         if (ret) {
683                 printk(KERN_ERR "%s: could not request SEL_HOST_USB GPIO\n", __FUNCTION__);
684         }
685         gpio_direction_output(GPIO_RF_11, 0);
686
687         /*
688          * Setup audio
689          */
690         audio_dev = audio_device_alloc("snd-ubi32-generic", "audio", "audio-i2sout", 0);
691         if (audio_dev) {
692                 platform_device_register(audio_dev);
693         }
694
695         /*
696          * Register all of the devices we have on this board
697          */
698         printk(KERN_INFO "%s: registering device resources\n", __FUNCTION__);
699         platform_add_devices(ip7145dpf_devices, ARRAY_SIZE(ip7145dpf_devices));
700
701         /*
702          * Register all of the devices which sit on the I2C bus
703          */
704         printk(KERN_INFO "%s: registering i2c resources\n", __FUNCTION__);
705         i2c_register_board_info(0, ip7145dpf_i2c_board_info, ARRAY_SIZE(ip7145dpf_i2c_board_info));
706
707         /*
708          * We have to initialize the SPI after the I2C IOB gets setup. SPI is initialized in
709          * ip7145dpf_u72_setup
710          */
711
712         return 0;
713 }
714
715 arch_initcall(ip7145dpf_init);