Merge branch 'for-linus-4.21-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / arm / mach-davinci / board-dm365-evm.c
1 /*
2  * TI DaVinci DM365 EVM board support
3  *
4  * Copyright (C) 2009 Texas Instruments Incorporated
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/io.h>
20 #include <linux/clk.h>
21 #include <linux/platform_data/at24.h>
22 #include <linux/leds.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/slab.h>
26 #include <linux/mtd/rawnand.h>
27 #include <linux/nvmem-provider.h>
28 #include <linux/input.h>
29 #include <linux/spi/spi.h>
30 #include <linux/spi/eeprom.h>
31 #include <linux/v4l2-dv-timings.h>
32 #include <linux/platform_data/ti-aemif.h>
33
34 #include <asm/mach-types.h>
35 #include <asm/mach/arch.h>
36
37 #include <mach/mux.h>
38 #include <mach/common.h>
39 #include <linux/platform_data/i2c-davinci.h>
40 #include <mach/serial.h>
41 #include <linux/platform_data/mmc-davinci.h>
42 #include <linux/platform_data/mtd-davinci.h>
43 #include <linux/platform_data/keyscan-davinci.h>
44
45 #include <media/i2c/ths7303.h>
46 #include <media/i2c/tvp514x.h>
47
48 #include "davinci.h"
49
50 static inline int have_imager(void)
51 {
52         /* REVISIT when it's supported, trigger via Kconfig */
53         return 0;
54 }
55
56 static inline int have_tvp7002(void)
57 {
58         /* REVISIT when it's supported, trigger via Kconfig */
59         return 0;
60 }
61
62 #define DM365_EVM_PHY_ID                "davinci_mdio-0:01"
63 /*
64  * A MAX-II CPLD is used for various board control functions.
65  */
66 #define CPLD_OFFSET(a13a8,a2a1)         (((a13a8) << 10) + ((a2a1) << 3))
67
68 #define CPLD_VERSION    CPLD_OFFSET(0,0)        /* r/o */
69 #define CPLD_TEST       CPLD_OFFSET(0,1)
70 #define CPLD_LEDS       CPLD_OFFSET(0,2)
71 #define CPLD_MUX        CPLD_OFFSET(0,3)
72 #define CPLD_SWITCH     CPLD_OFFSET(1,0)        /* r/o */
73 #define CPLD_POWER      CPLD_OFFSET(1,1)
74 #define CPLD_VIDEO      CPLD_OFFSET(1,2)
75 #define CPLD_CARDSTAT   CPLD_OFFSET(1,3)        /* r/o */
76
77 #define CPLD_DILC_OUT   CPLD_OFFSET(2,0)
78 #define CPLD_DILC_IN    CPLD_OFFSET(2,1)        /* r/o */
79
80 #define CPLD_IMG_DIR0   CPLD_OFFSET(2,2)
81 #define CPLD_IMG_MUX0   CPLD_OFFSET(2,3)
82 #define CPLD_IMG_MUX1   CPLD_OFFSET(3,0)
83 #define CPLD_IMG_DIR1   CPLD_OFFSET(3,1)
84 #define CPLD_IMG_MUX2   CPLD_OFFSET(3,2)
85 #define CPLD_IMG_MUX3   CPLD_OFFSET(3,3)
86 #define CPLD_IMG_DIR2   CPLD_OFFSET(4,0)
87 #define CPLD_IMG_MUX4   CPLD_OFFSET(4,1)
88 #define CPLD_IMG_MUX5   CPLD_OFFSET(4,2)
89
90 #define CPLD_RESETS     CPLD_OFFSET(4,3)
91
92 #define CPLD_CCD_DIR1   CPLD_OFFSET(0x3e,0)
93 #define CPLD_CCD_IO1    CPLD_OFFSET(0x3e,1)
94 #define CPLD_CCD_DIR2   CPLD_OFFSET(0x3e,2)
95 #define CPLD_CCD_IO2    CPLD_OFFSET(0x3e,3)
96 #define CPLD_CCD_DIR3   CPLD_OFFSET(0x3f,0)
97 #define CPLD_CCD_IO3    CPLD_OFFSET(0x3f,1)
98
99 static void __iomem *cpld;
100
101
102 /* NOTE:  this is geared for the standard config, with a socketed
103  * 2 GByte Micron NAND (MT29F16G08FAA) using 128KB sectors.  If you
104  * swap chips with a different block size, partitioning will
105  * need to be changed. This NAND chip MT29F16G08FAA is the default
106  * NAND shipped with the Spectrum Digital DM365 EVM
107  */
108 #define NAND_BLOCK_SIZE         SZ_128K
109
110 static struct mtd_partition davinci_nand_partitions[] = {
111         {
112                 /* UBL (a few copies) plus U-Boot */
113                 .name           = "bootloader",
114                 .offset         = 0,
115                 .size           = 30 * NAND_BLOCK_SIZE,
116                 .mask_flags     = MTD_WRITEABLE, /* force read-only */
117         }, {
118                 /* U-Boot environment */
119                 .name           = "params",
120                 .offset         = MTDPART_OFS_APPEND,
121                 .size           = 2 * NAND_BLOCK_SIZE,
122                 .mask_flags     = 0,
123         }, {
124                 .name           = "kernel",
125                 .offset         = MTDPART_OFS_APPEND,
126                 .size           = SZ_4M,
127                 .mask_flags     = 0,
128         }, {
129                 .name           = "filesystem1",
130                 .offset         = MTDPART_OFS_APPEND,
131                 .size           = SZ_512M,
132                 .mask_flags     = 0,
133         }, {
134                 .name           = "filesystem2",
135                 .offset         = MTDPART_OFS_APPEND,
136                 .size           = MTDPART_SIZ_FULL,
137                 .mask_flags     = 0,
138         }
139         /* two blocks with bad block table (and mirror) at the end */
140 };
141
142 static struct davinci_nand_pdata davinci_nand_data = {
143         .core_chipsel           = 0,
144         .mask_chipsel           = BIT(14),
145         .parts                  = davinci_nand_partitions,
146         .nr_parts               = ARRAY_SIZE(davinci_nand_partitions),
147         .ecc_mode               = NAND_ECC_HW,
148         .bbt_options            = NAND_BBT_USE_FLASH,
149         .ecc_bits               = 4,
150 };
151
152 static struct resource davinci_nand_resources[] = {
153         {
154                 .start          = DM365_ASYNC_EMIF_DATA_CE0_BASE,
155                 .end            = DM365_ASYNC_EMIF_DATA_CE0_BASE + SZ_32M - 1,
156                 .flags          = IORESOURCE_MEM,
157         }, {
158                 .start          = DM365_ASYNC_EMIF_CONTROL_BASE,
159                 .end            = DM365_ASYNC_EMIF_CONTROL_BASE + SZ_4K - 1,
160                 .flags          = IORESOURCE_MEM,
161         },
162 };
163
164 static struct platform_device davinci_aemif_devices[] = {
165         {
166                 .name           = "davinci_nand",
167                 .id             = 0,
168                 .num_resources  = ARRAY_SIZE(davinci_nand_resources),
169                 .resource       = davinci_nand_resources,
170                 .dev            = {
171                         .platform_data  = &davinci_nand_data,
172                 },
173         }
174 };
175
176 static struct resource davinci_aemif_resources[] = {
177         {
178                 .start          = DM365_ASYNC_EMIF_CONTROL_BASE,
179                 .end            = DM365_ASYNC_EMIF_CONTROL_BASE + SZ_4K - 1,
180                 .flags          = IORESOURCE_MEM,
181         },
182 };
183
184 static struct aemif_abus_data da850_evm_aemif_abus_data[] = {
185         {
186                 .cs             = 1,
187         },
188 };
189
190 static struct aemif_platform_data davinci_aemif_pdata = {
191         .abus_data              = da850_evm_aemif_abus_data,
192         .num_abus_data          = ARRAY_SIZE(da850_evm_aemif_abus_data),
193         .sub_devices            = davinci_aemif_devices,
194         .num_sub_devices        = ARRAY_SIZE(davinci_aemif_devices),
195 };
196
197 static struct platform_device davinci_aemif_device = {
198         .name                   = "ti-aemif",
199         .id                     = -1,
200         .dev = {
201                 .platform_data  = &davinci_aemif_pdata,
202         },
203         .resource               = davinci_aemif_resources,
204         .num_resources          = ARRAY_SIZE(davinci_aemif_resources),
205 };
206
207 static struct nvmem_cell_info davinci_nvmem_cells[] = {
208         {
209                 .name           = "macaddr",
210                 .offset         = 0x7f00,
211                 .bytes          = ETH_ALEN,
212         }
213 };
214
215 static struct nvmem_cell_table davinci_nvmem_cell_table = {
216         .nvmem_name     = "1-00500",
217         .cells          = davinci_nvmem_cells,
218         .ncells         = ARRAY_SIZE(davinci_nvmem_cells),
219 };
220
221 static struct nvmem_cell_lookup davinci_nvmem_cell_lookup = {
222         .nvmem_name     = "1-00500",
223         .cell_name      = "macaddr",
224         .dev_id         = "davinci_emac.1",
225         .con_id         = "mac-address",
226 };
227
228 static struct at24_platform_data eeprom_info = {
229         .byte_len       = (256*1024) / 8,
230         .page_size      = 64,
231         .flags          = AT24_FLAG_ADDR16,
232         .setup          = davinci_get_mac_addr,
233         .context        = (void *)0x7f00,
234 };
235
236 static struct i2c_board_info i2c_info[] = {
237         {
238                 I2C_BOARD_INFO("24c256", 0x50),
239                 .platform_data  = &eeprom_info,
240         },
241         {
242                 I2C_BOARD_INFO("tlv320aic3x", 0x18),
243         },
244 };
245
246 static struct davinci_i2c_platform_data i2c_pdata = {
247         .bus_freq       = 400   /* kHz */,
248         .bus_delay      = 0     /* usec */,
249 };
250
251 static int dm365evm_keyscan_enable(struct device *dev)
252 {
253         return davinci_cfg_reg(DM365_KEYSCAN);
254 }
255
256 static unsigned short dm365evm_keymap[] = {
257         KEY_KP2,
258         KEY_LEFT,
259         KEY_EXIT,
260         KEY_DOWN,
261         KEY_ENTER,
262         KEY_UP,
263         KEY_KP1,
264         KEY_RIGHT,
265         KEY_MENU,
266         KEY_RECORD,
267         KEY_REWIND,
268         KEY_KPMINUS,
269         KEY_STOP,
270         KEY_FASTFORWARD,
271         KEY_KPPLUS,
272         KEY_PLAYPAUSE,
273         0
274 };
275
276 static struct davinci_ks_platform_data dm365evm_ks_data = {
277         .device_enable  = dm365evm_keyscan_enable,
278         .keymap         = dm365evm_keymap,
279         .keymapsize     = ARRAY_SIZE(dm365evm_keymap),
280         .rep            = 1,
281         /* Scan period = strobe + interval */
282         .strobe         = 0x5,
283         .interval       = 0x2,
284         .matrix_type    = DAVINCI_KEYSCAN_MATRIX_4X4,
285 };
286
287 static int cpld_mmc_get_cd(int module)
288 {
289         if (!cpld)
290                 return -ENXIO;
291
292         /* low == card present */
293         return !(__raw_readb(cpld + CPLD_CARDSTAT) & BIT(module ? 4 : 0));
294 }
295
296 static int cpld_mmc_get_ro(int module)
297 {
298         if (!cpld)
299                 return -ENXIO;
300
301         /* high == card's write protect switch active */
302         return !!(__raw_readb(cpld + CPLD_CARDSTAT) & BIT(module ? 5 : 1));
303 }
304
305 static struct davinci_mmc_config dm365evm_mmc_config = {
306         .get_cd         = cpld_mmc_get_cd,
307         .get_ro         = cpld_mmc_get_ro,
308         .wires          = 4,
309         .max_freq       = 50000000,
310         .caps           = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED,
311 };
312
313 static void dm365evm_emac_configure(void)
314 {
315         /*
316          * EMAC pins are multiplexed with GPIO and UART
317          * Further details are available at the DM365 ARM
318          * Subsystem Users Guide(sprufg5.pdf) pages 125 - 127
319          */
320         davinci_cfg_reg(DM365_EMAC_TX_EN);
321         davinci_cfg_reg(DM365_EMAC_TX_CLK);
322         davinci_cfg_reg(DM365_EMAC_COL);
323         davinci_cfg_reg(DM365_EMAC_TXD3);
324         davinci_cfg_reg(DM365_EMAC_TXD2);
325         davinci_cfg_reg(DM365_EMAC_TXD1);
326         davinci_cfg_reg(DM365_EMAC_TXD0);
327         davinci_cfg_reg(DM365_EMAC_RXD3);
328         davinci_cfg_reg(DM365_EMAC_RXD2);
329         davinci_cfg_reg(DM365_EMAC_RXD1);
330         davinci_cfg_reg(DM365_EMAC_RXD0);
331         davinci_cfg_reg(DM365_EMAC_RX_CLK);
332         davinci_cfg_reg(DM365_EMAC_RX_DV);
333         davinci_cfg_reg(DM365_EMAC_RX_ER);
334         davinci_cfg_reg(DM365_EMAC_CRS);
335         davinci_cfg_reg(DM365_EMAC_MDIO);
336         davinci_cfg_reg(DM365_EMAC_MDCLK);
337
338         /*
339          * EMAC interrupts are multiplexed with GPIO interrupts
340          * Details are available at the DM365 ARM
341          * Subsystem Users Guide(sprufg5.pdf) pages 133 - 134
342          */
343         davinci_cfg_reg(DM365_INT_EMAC_RXTHRESH);
344         davinci_cfg_reg(DM365_INT_EMAC_RXPULSE);
345         davinci_cfg_reg(DM365_INT_EMAC_TXPULSE);
346         davinci_cfg_reg(DM365_INT_EMAC_MISCPULSE);
347 }
348
349 static void dm365evm_mmc_configure(void)
350 {
351         /*
352          * MMC/SD pins are multiplexed with GPIO and EMIF
353          * Further details are available at the DM365 ARM
354          * Subsystem Users Guide(sprufg5.pdf) pages 118, 128 - 131
355          */
356         davinci_cfg_reg(DM365_SD1_CLK);
357         davinci_cfg_reg(DM365_SD1_CMD);
358         davinci_cfg_reg(DM365_SD1_DATA3);
359         davinci_cfg_reg(DM365_SD1_DATA2);
360         davinci_cfg_reg(DM365_SD1_DATA1);
361         davinci_cfg_reg(DM365_SD1_DATA0);
362 }
363
364 static struct tvp514x_platform_data tvp5146_pdata = {
365         .clk_polarity = 0,
366         .hs_polarity = 1,
367         .vs_polarity = 1
368 };
369
370 #define TVP514X_STD_ALL        (V4L2_STD_NTSC | V4L2_STD_PAL)
371 /* Inputs available at the TVP5146 */
372 static struct v4l2_input tvp5146_inputs[] = {
373         {
374                 .index = 0,
375                 .name = "Composite",
376                 .type = V4L2_INPUT_TYPE_CAMERA,
377                 .std = TVP514X_STD_ALL,
378         },
379         {
380                 .index = 1,
381                 .name = "S-Video",
382                 .type = V4L2_INPUT_TYPE_CAMERA,
383                 .std = TVP514X_STD_ALL,
384         },
385 };
386
387 /*
388  * this is the route info for connecting each input to decoder
389  * ouput that goes to vpfe. There is a one to one correspondence
390  * with tvp5146_inputs
391  */
392 static struct vpfe_route tvp5146_routes[] = {
393         {
394                 .input = INPUT_CVBS_VI2B,
395                 .output = OUTPUT_10BIT_422_EMBEDDED_SYNC,
396         },
397 {
398                 .input = INPUT_SVIDEO_VI2C_VI1C,
399                 .output = OUTPUT_10BIT_422_EMBEDDED_SYNC,
400         },
401 };
402
403 static struct vpfe_subdev_info vpfe_sub_devs[] = {
404         {
405                 .name = "tvp5146",
406                 .grp_id = 0,
407                 .num_inputs = ARRAY_SIZE(tvp5146_inputs),
408                 .inputs = tvp5146_inputs,
409                 .routes = tvp5146_routes,
410                 .can_route = 1,
411                 .ccdc_if_params = {
412                         .if_type = VPFE_BT656,
413                         .hdpol = VPFE_PINPOL_POSITIVE,
414                         .vdpol = VPFE_PINPOL_POSITIVE,
415                 },
416                 .board_info = {
417                         I2C_BOARD_INFO("tvp5146", 0x5d),
418                         .platform_data = &tvp5146_pdata,
419                 },
420         },
421 };
422
423 static struct vpfe_config vpfe_cfg = {
424         .num_subdevs = ARRAY_SIZE(vpfe_sub_devs),
425         .sub_devs = vpfe_sub_devs,
426         .i2c_adapter_id = 1,
427         .card_name = "DM365 EVM",
428         .ccdc = "ISIF",
429 };
430
431 /* venc standards timings */
432 static struct vpbe_enc_mode_info dm365evm_enc_std_timing[] = {
433         {
434                 .name           = "ntsc",
435                 .timings_type   = VPBE_ENC_STD,
436                 .std_id         = V4L2_STD_NTSC,
437                 .interlaced     = 1,
438                 .xres           = 720,
439                 .yres           = 480,
440                 .aspect         = {11, 10},
441                 .fps            = {30000, 1001},
442                 .left_margin    = 0x79,
443                 .upper_margin   = 0x10,
444         },
445         {
446                 .name           = "pal",
447                 .timings_type   = VPBE_ENC_STD,
448                 .std_id         = V4L2_STD_PAL,
449                 .interlaced     = 1,
450                 .xres           = 720,
451                 .yres           = 576,
452                 .aspect         = {54, 59},
453                 .fps            = {25, 1},
454                 .left_margin    = 0x7E,
455                 .upper_margin   = 0x16,
456         },
457 };
458
459 /* venc dv timings */
460 static struct vpbe_enc_mode_info dm365evm_enc_preset_timing[] = {
461         {
462                 .name           = "480p59_94",
463                 .timings_type   = VPBE_ENC_DV_TIMINGS,
464                 .dv_timings     = V4L2_DV_BT_CEA_720X480P59_94,
465                 .interlaced     = 0,
466                 .xres           = 720,
467                 .yres           = 480,
468                 .aspect         = {1, 1},
469                 .fps            = {5994, 100},
470                 .left_margin    = 0x8F,
471                 .upper_margin   = 0x2D,
472         },
473         {
474                 .name           = "576p50",
475                 .timings_type   = VPBE_ENC_DV_TIMINGS,
476                 .dv_timings     = V4L2_DV_BT_CEA_720X576P50,
477                 .interlaced     = 0,
478                 .xres           = 720,
479                 .yres           = 576,
480                 .aspect         = {1, 1},
481                 .fps            = {50, 1},
482                 .left_margin    = 0x8C,
483                 .upper_margin   = 0x36,
484         },
485         {
486                 .name           = "720p60",
487                 .timings_type   = VPBE_ENC_DV_TIMINGS,
488                 .dv_timings     = V4L2_DV_BT_CEA_1280X720P60,
489                 .interlaced     = 0,
490                 .xres           = 1280,
491                 .yres           = 720,
492                 .aspect         = {1, 1},
493                 .fps            = {60, 1},
494                 .left_margin    = 0x117,
495                 .right_margin   = 70,
496                 .upper_margin   = 38,
497                 .lower_margin   = 3,
498                 .hsync_len      = 80,
499                 .vsync_len      = 5,
500         },
501         {
502                 .name           = "1080i60",
503                 .timings_type   = VPBE_ENC_DV_TIMINGS,
504                 .dv_timings     = V4L2_DV_BT_CEA_1920X1080I60,
505                 .interlaced     = 1,
506                 .xres           = 1920,
507                 .yres           = 1080,
508                 .aspect         = {1, 1},
509                 .fps            = {30, 1},
510                 .left_margin    = 0xc9,
511                 .right_margin   = 80,
512                 .upper_margin   = 30,
513                 .lower_margin   = 3,
514                 .hsync_len      = 88,
515                 .vsync_len      = 5,
516         },
517 };
518
519 #define VENC_STD_ALL    (V4L2_STD_NTSC | V4L2_STD_PAL)
520
521 /*
522  * The outputs available from VPBE + ecnoders. Keep the
523  * the order same as that of encoders. First those from venc followed by that
524  * from encoders. Index in the output refers to index on a particular
525  * encoder.Driver uses this index to pass it to encoder when it supports more
526  * than one output. Application uses index of the array to set an output.
527  */
528 static struct vpbe_output dm365evm_vpbe_outputs[] = {
529         {
530                 .output         = {
531                         .index          = 0,
532                         .name           = "Composite",
533                         .type           = V4L2_OUTPUT_TYPE_ANALOG,
534                         .std            = VENC_STD_ALL,
535                         .capabilities   = V4L2_OUT_CAP_STD,
536                 },
537                 .subdev_name    = DM365_VPBE_VENC_SUBDEV_NAME,
538                 .default_mode   = "ntsc",
539                 .num_modes      = ARRAY_SIZE(dm365evm_enc_std_timing),
540                 .modes          = dm365evm_enc_std_timing,
541                 .if_params      = MEDIA_BUS_FMT_FIXED,
542         },
543         {
544                 .output         = {
545                         .index          = 1,
546                         .name           = "Component",
547                         .type           = V4L2_OUTPUT_TYPE_ANALOG,
548                         .capabilities   = V4L2_OUT_CAP_DV_TIMINGS,
549                 },
550                 .subdev_name    = DM365_VPBE_VENC_SUBDEV_NAME,
551                 .default_mode   = "480p59_94",
552                 .num_modes      = ARRAY_SIZE(dm365evm_enc_preset_timing),
553                 .modes          = dm365evm_enc_preset_timing,
554                 .if_params      = MEDIA_BUS_FMT_FIXED,
555         },
556 };
557
558 /*
559  * Amplifiers on the board
560  */
561 static struct ths7303_platform_data ths7303_pdata = {
562         .ch_1 = 3,
563         .ch_2 = 3,
564         .ch_3 = 3,
565 };
566
567 static struct amp_config_info vpbe_amp = {
568         .module_name    = "ths7303",
569         .is_i2c         = 1,
570         .board_info     = {
571                 I2C_BOARD_INFO("ths7303", 0x2c),
572                 .platform_data = &ths7303_pdata,
573         }
574 };
575
576 static struct vpbe_config dm365evm_display_cfg = {
577         .module_name    = "dm365-vpbe-display",
578         .i2c_adapter_id = 1,
579         .amp            = &vpbe_amp,
580         .osd            = {
581                 .module_name    = DM365_VPBE_OSD_SUBDEV_NAME,
582         },
583         .venc           = {
584                 .module_name    = DM365_VPBE_VENC_SUBDEV_NAME,
585         },
586         .num_outputs    = ARRAY_SIZE(dm365evm_vpbe_outputs),
587         .outputs        = dm365evm_vpbe_outputs,
588 };
589
590 static void __init evm_init_i2c(void)
591 {
592         davinci_init_i2c(&i2c_pdata);
593         i2c_register_board_info(1, i2c_info, ARRAY_SIZE(i2c_info));
594 }
595
596 static inline int have_leds(void)
597 {
598 #ifdef CONFIG_LEDS_CLASS
599         return 1;
600 #else
601         return 0;
602 #endif
603 }
604
605 struct cpld_led {
606         struct led_classdev     cdev;
607         u8                      mask;
608 };
609
610 static const struct {
611         const char *name;
612         const char *trigger;
613 } cpld_leds[] = {
614         { "dm365evm::ds2", },
615         { "dm365evm::ds3", },
616         { "dm365evm::ds4", },
617         { "dm365evm::ds5", },
618         { "dm365evm::ds6", "nand-disk", },
619         { "dm365evm::ds7", "mmc1", },
620         { "dm365evm::ds8", "mmc0", },
621         { "dm365evm::ds9", "heartbeat", },
622 };
623
624 static void cpld_led_set(struct led_classdev *cdev, enum led_brightness b)
625 {
626         struct cpld_led *led = container_of(cdev, struct cpld_led, cdev);
627         u8 reg = __raw_readb(cpld + CPLD_LEDS);
628
629         if (b != LED_OFF)
630                 reg &= ~led->mask;
631         else
632                 reg |= led->mask;
633         __raw_writeb(reg, cpld + CPLD_LEDS);
634 }
635
636 static enum led_brightness cpld_led_get(struct led_classdev *cdev)
637 {
638         struct cpld_led *led = container_of(cdev, struct cpld_led, cdev);
639         u8 reg = __raw_readb(cpld + CPLD_LEDS);
640
641         return (reg & led->mask) ? LED_OFF : LED_FULL;
642 }
643
644 static int __init cpld_leds_init(void)
645 {
646         int     i;
647
648         if (!have_leds() ||  !cpld)
649                 return 0;
650
651         /* setup LEDs */
652         __raw_writeb(0xff, cpld + CPLD_LEDS);
653         for (i = 0; i < ARRAY_SIZE(cpld_leds); i++) {
654                 struct cpld_led *led;
655
656                 led = kzalloc(sizeof(*led), GFP_KERNEL);
657                 if (!led)
658                         break;
659
660                 led->cdev.name = cpld_leds[i].name;
661                 led->cdev.brightness_set = cpld_led_set;
662                 led->cdev.brightness_get = cpld_led_get;
663                 led->cdev.default_trigger = cpld_leds[i].trigger;
664                 led->mask = BIT(i);
665
666                 if (led_classdev_register(NULL, &led->cdev) < 0) {
667                         kfree(led);
668                         break;
669                 }
670         }
671
672         return 0;
673 }
674 /* run after subsys_initcall() for LEDs */
675 fs_initcall(cpld_leds_init);
676
677
678 static void __init evm_init_cpld(void)
679 {
680         u8 mux, resets;
681         const char *label;
682         struct clk *aemif_clk;
683         int rc;
684
685         /* Make sure we can configure the CPLD through CS1.  Then
686          * leave it on for later access to MMC and LED registers.
687          */
688         aemif_clk = clk_get(NULL, "aemif");
689         if (IS_ERR(aemif_clk))
690                 return;
691         clk_prepare_enable(aemif_clk);
692
693         if (request_mem_region(DM365_ASYNC_EMIF_DATA_CE1_BASE, SECTION_SIZE,
694                         "cpld") == NULL)
695                 goto fail;
696         cpld = ioremap(DM365_ASYNC_EMIF_DATA_CE1_BASE, SECTION_SIZE);
697         if (!cpld) {
698                 release_mem_region(DM365_ASYNC_EMIF_DATA_CE1_BASE,
699                                 SECTION_SIZE);
700 fail:
701                 pr_err("ERROR: can't map CPLD\n");
702                 clk_disable_unprepare(aemif_clk);
703                 return;
704         }
705
706         /* External muxing for some signals */
707         mux = 0;
708
709         /* Read SW5 to set up NAND + keypad _or_ OneNAND (sync read).
710          * NOTE:  SW4 bus width setting must match!
711          */
712         if ((__raw_readb(cpld + CPLD_SWITCH) & BIT(5)) == 0) {
713                 /* external keypad mux */
714                 mux |= BIT(7);
715
716                 rc = platform_device_register(&davinci_aemif_device);
717                 if (rc)
718                         pr_warn("%s(): error registering the aemif device: %d\n",
719                                 __func__, rc);
720         } else {
721                 /* no OneNAND support yet */
722         }
723
724         /* Leave external chips in reset when unused. */
725         resets = BIT(3) | BIT(2) | BIT(1) | BIT(0);
726
727         /* Static video input config with SN74CBT16214 1-of-3 mux:
728          *  - port b1 == tvp7002 (mux lowbits == 1 or 6)
729          *  - port b2 == imager (mux lowbits == 2 or 7)
730          *  - port b3 == tvp5146 (mux lowbits == 5)
731          *
732          * Runtime switching could work too, with limitations.
733          */
734         if (have_imager()) {
735                 label = "HD imager";
736                 mux |= 2;
737
738                 /* externally mux MMC1/ENET/AIC33 to imager */
739                 mux |= BIT(6) | BIT(5) | BIT(3);
740         } else {
741                 struct davinci_soc_info *soc_info = &davinci_soc_info;
742
743                 /* we can use MMC1 ... */
744                 dm365evm_mmc_configure();
745                 davinci_setup_mmc(1, &dm365evm_mmc_config);
746
747                 /* ... and ENET ... */
748                 dm365evm_emac_configure();
749                 soc_info->emac_pdata->phy_id = DM365_EVM_PHY_ID;
750                 resets &= ~BIT(3);
751
752                 /* ... and AIC33 */
753                 resets &= ~BIT(1);
754
755                 if (have_tvp7002()) {
756                         mux |= 1;
757                         resets &= ~BIT(2);
758                         label = "tvp7002 HD";
759                 } else {
760                         /* default to tvp5146 */
761                         mux |= 5;
762                         resets &= ~BIT(0);
763                         label = "tvp5146 SD";
764                 }
765         }
766         __raw_writeb(mux, cpld + CPLD_MUX);
767         __raw_writeb(resets, cpld + CPLD_RESETS);
768         pr_info("EVM: %s video input\n", label);
769
770         /* REVISIT export switches: NTSC/PAL (SW5.6), EXTRA1 (SW5.2), etc */
771 }
772
773 static void __init dm365_evm_map_io(void)
774 {
775         dm365_init();
776 }
777
778 static struct spi_eeprom at25640 = {
779         .byte_len       = SZ_64K / 8,
780         .name           = "at25640",
781         .page_size      = 32,
782         .flags          = EE_ADDR2,
783 };
784
785 static const struct spi_board_info dm365_evm_spi_info[] __initconst = {
786         {
787                 .modalias       = "at25",
788                 .platform_data  = &at25640,
789                 .max_speed_hz   = 10 * 1000 * 1000,
790                 .bus_num        = 0,
791                 .chip_select    = 0,
792                 .mode           = SPI_MODE_0,
793         },
794 };
795
796 static __init void dm365_evm_init(void)
797 {
798         int ret;
799
800         dm365_register_clocks();
801
802         ret = dm365_gpio_register();
803         if (ret)
804                 pr_warn("%s: GPIO init failed: %d\n", __func__, ret);
805
806         nvmem_add_cell_table(&davinci_nvmem_cell_table);
807         nvmem_add_cell_lookups(&davinci_nvmem_cell_lookup, 1);
808
809         evm_init_i2c();
810         davinci_serial_init(dm365_serial_device);
811
812         dm365evm_emac_configure();
813         dm365evm_mmc_configure();
814
815         davinci_setup_mmc(0, &dm365evm_mmc_config);
816
817         dm365_init_video(&vpfe_cfg, &dm365evm_display_cfg);
818
819         /* maybe setup mmc1/etc ... _after_ mmc0 */
820         evm_init_cpld();
821
822 #ifdef CONFIG_SND_SOC_DM365_AIC3X_CODEC
823         dm365_init_asp();
824 #elif defined(CONFIG_SND_SOC_DM365_VOICE_CODEC)
825         dm365_init_vc();
826 #endif
827         dm365_init_rtc();
828         dm365_init_ks(&dm365evm_ks_data);
829
830         dm365_init_spi0(BIT(0), dm365_evm_spi_info,
831                         ARRAY_SIZE(dm365_evm_spi_info));
832 }
833
834 MACHINE_START(DAVINCI_DM365_EVM, "DaVinci DM365 EVM")
835         .atag_offset    = 0x100,
836         .map_io         = dm365_evm_map_io,
837         .init_irq       = davinci_irq_init,
838         .init_time      = dm365_init_time,
839         .init_machine   = dm365_evm_init,
840         .init_late      = davinci_init_late,
841         .dma_zone_size  = SZ_128M,
842 MACHINE_END
843