GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / avr32 / boards / merisc / setup.c
1 /*
2  * Board-specific setup code for the Merisc
3  *
4  * Copyright (C) 2008 Martinsson Elektronik AB
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/clk.h>
11 #include <linux/etherdevice.h>
12 #include <linux/i2c.h>
13 #include <linux/i2c-gpio.h>
14 #include <linux/gpio.h>
15 #include <linux/init.h>
16 #include <linux/linkage.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
19 #include <linux/leds.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/ads7846.h>
22 #include <linux/irq.h>
23 #include <linux/fb.h>
24 #include <linux/atmel-mci.h>
25 #include <linux/pwm.h>
26 #include <linux/leds_pwm.h>
27
28 #include <asm/io.h>
29 #include <asm/setup.h>
30
31 #include <mach/at32ap700x.h>
32 #include <mach/board.h>
33 #include <mach/init.h>
34 #include <mach/portmux.h>
35
36 #include "merisc.h"
37
38 /* Holds the autodetected board model and revision */
39 static int merisc_board_id;
40
41 /* Initialized by bootloader-specific startup code. */
42 struct tag *bootloader_tags __initdata;
43
44 /* Oscillator frequencies. These are board specific */
45 unsigned long at32_board_osc_rates[3] = {
46         [0]     = 32768,        /* 32.768 kHz on RTC osc */
47         [1]     = 20000000,     /* 20 MHz on osc0 */
48         [2]     = 12000000,     /* 12 MHz on osc1 */
49 };
50
51 struct eth_addr {
52         u8 addr[6];
53 };
54
55 static struct eth_addr __initdata hw_addr[2];
56 static struct macb_platform_data __initdata eth_data[2];
57
58 static int ads7846_get_pendown_state_PB26(void)
59 {
60         return !gpio_get_value(GPIO_PIN_PB(26));
61 }
62
63 static int ads7846_get_pendown_state_PB28(void)
64 {
65         return !gpio_get_value(GPIO_PIN_PB(28));
66 }
67
68 static struct ads7846_platform_data __initdata ads7846_data = {
69         .model                          = 7846,
70         .vref_delay_usecs               = 100,
71         .vref_mv                        = 0,
72         .keep_vref_on                   = 0,
73         .settle_delay_usecs             = 150,
74         .penirq_recheck_delay_usecs     = 1,
75         .x_plate_ohms                   = 800,
76         .debounce_rep                   = 4,
77         .debounce_max                   = 10,
78         .debounce_tol                   = 50,
79         .get_pendown_state              = ads7846_get_pendown_state_PB26,
80         .filter_init                    = NULL,
81         .filter                         = NULL,
82         .filter_cleanup                 = NULL,
83 };
84
85 static struct spi_board_info __initdata spi0_board_info[] = {
86         {
87                 .modalias       = "ads7846",
88                 .max_speed_hz   = 3250000,
89                 .chip_select    = 0,
90                 .bus_num        = 0,
91                 .platform_data  = &ads7846_data,
92                 .mode           = SPI_MODE_0,
93         },
94 };
95
96 static struct mci_platform_data __initdata mci0_data = {
97         .slot[0] = {
98                 .bus_width              = 4,
99                 .detect_pin             = GPIO_PIN_PE(19),
100                 .wp_pin                 = GPIO_PIN_PE(20),
101                 .detect_is_active_high  = true,
102         },
103 };
104
105 static int __init parse_tag_ethernet(struct tag *tag)
106 {
107         int i;
108
109         i = tag->u.ethernet.mac_index;
110         if (i < ARRAY_SIZE(hw_addr)) {
111                 memcpy(hw_addr[i].addr, tag->u.ethernet.hw_address,
112                        sizeof(hw_addr[i].addr));
113         }
114
115         return 0;
116 }
117 __tagtable(ATAG_ETHERNET, parse_tag_ethernet);
118
119 static void __init set_hw_addr(struct platform_device *pdev)
120 {
121         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122         const u8 *addr;
123         void __iomem *regs;
124         struct clk *pclk;
125
126         if (!res)
127                 return;
128
129         if (pdev->id >= ARRAY_SIZE(hw_addr))
130                 return;
131
132         addr = hw_addr[pdev->id].addr;
133         if (!is_valid_ether_addr(addr))
134                 return;
135
136         regs = (void __iomem __force *)res->start;
137         pclk = clk_get(&pdev->dev, "pclk");
138         if (IS_ERR(pclk))
139                 return;
140
141         clk_enable(pclk);
142         __raw_writel((addr[3] << 24) | (addr[2] << 16)
143                      | (addr[1] << 8) | addr[0], regs + 0x98);
144         __raw_writel((addr[5] << 8) | addr[4], regs + 0x9c);
145         clk_disable(pclk);
146         clk_put(pclk);
147 }
148
149 static struct i2c_gpio_platform_data i2c_gpio_data = {
150         .sda_pin                = GPIO_PIN_PA(6),
151         .scl_pin                = GPIO_PIN_PA(7),
152         .sda_is_open_drain      = 1,
153         .scl_is_open_drain      = 1,
154         .udelay                 = 2,
155 };
156
157 static struct platform_device i2c_gpio_device = {
158         .name   = "i2c-gpio",
159         .id     = 0,
160         .dev    = {
161                 .platform_data  = &i2c_gpio_data,
162         },
163 };
164
165 static struct i2c_board_info __initdata i2c_info[] = {
166         {
167                 I2C_BOARD_INFO("pcf8563", 0x51)
168         },
169 };
170
171 #if IS_ENABLED(CONFIG_LEDS_PWM)
172 static struct pwm_lookup pwm_lookup[] = {
173         PWM_LOOKUP("at91sam9rl-pwm", 0, "leds_pwm", "backlight",
174                    5000, PWM_POLARITY_NORMAL),
175 };
176
177 static struct led_pwm pwm_leds[] = {
178         {
179                 .name   = "backlight",
180                 .max_brightness = 255,
181         },
182 };
183
184 static struct led_pwm_platform_data pwm_data = {
185         .num_leds       = ARRAY_SIZE(pwm_leds),
186         .leds           = pwm_leds,
187 };
188
189 static struct platform_device leds_pwm = {
190         .name   = "leds_pwm",
191         .id     = -1,
192         .dev    = {
193                 .platform_data = &pwm_data,
194         },
195 };
196 #endif
197
198 const char *merisc_model(void)
199 {
200         switch (merisc_board_id) {
201         case 0:
202         case 1:
203                 return "500-01";
204         case 2:
205                 return "BT";
206         default:
207                 return "Unknown";
208         }
209 }
210
211 const char *merisc_revision(void)
212 {
213         switch (merisc_board_id) {
214         case 0:
215                 return "B";
216         case 1:
217                 return "D";
218         case 2:
219                 return "A";
220         default:
221                 return "Unknown";
222         }
223 }
224
225 static void detect_merisc_board_id(void)
226 {
227         /* Board ID pins MUST be set as input or the board may be damaged */
228         at32_select_gpio(GPIO_PIN_PA(24), AT32_GPIOF_PULLUP);
229         at32_select_gpio(GPIO_PIN_PA(25), AT32_GPIOF_PULLUP);
230         at32_select_gpio(GPIO_PIN_PA(26), AT32_GPIOF_PULLUP);
231         at32_select_gpio(GPIO_PIN_PA(27), AT32_GPIOF_PULLUP);
232
233         merisc_board_id = !gpio_get_value(GPIO_PIN_PA(24)) +
234                 !gpio_get_value(GPIO_PIN_PA(25)) * 2 +
235                 !gpio_get_value(GPIO_PIN_PA(26)) * 4 +
236                 !gpio_get_value(GPIO_PIN_PA(27)) * 8;
237 }
238
239 void __init setup_board(void)
240 {
241         at32_map_usart(0, 0, 0);
242         at32_map_usart(1, 1, 0);
243         at32_map_usart(3, 3, 0);
244         at32_setup_serial_console(1);
245 }
246
247 static int __init merisc_init(void)
248 {
249         detect_merisc_board_id();
250
251         printk(KERN_NOTICE "BOARD: Merisc %s revision %s\n", merisc_model(),
252                merisc_revision());
253
254         /* Reserve pins for SDRAM */
255         at32_reserve_pin(GPIO_PIOE_BASE, ATMEL_EBI_PE_DATA_ALL | (1 << 26));
256
257         if (merisc_board_id >= 1)
258                 at32_map_usart(2, 2, 0);
259
260         at32_add_device_usart(0);
261         at32_add_device_usart(1);
262         if (merisc_board_id >= 1)
263                 at32_add_device_usart(2);
264         at32_add_device_usart(3);
265         set_hw_addr(at32_add_device_eth(0, &eth_data[0]));
266
267         /* ADS7846 PENIRQ */
268         if (merisc_board_id == 0) {
269                 ads7846_data.get_pendown_state = ads7846_get_pendown_state_PB26;
270                 at32_select_periph(GPIO_PIOB_BASE, 1 << 26,
271                                    GPIO_PERIPH_A, AT32_GPIOF_PULLUP);
272                 spi0_board_info[0].irq = AT32_EXTINT(1);
273         } else {
274                 ads7846_data.get_pendown_state = ads7846_get_pendown_state_PB28;
275                 at32_select_periph(GPIO_PIOB_BASE, 1 << 28, GPIO_PERIPH_A,
276                                    AT32_GPIOF_PULLUP);
277                 spi0_board_info[0].irq = AT32_EXTINT(3);
278         }
279
280         /* ADS7846 busy pin */
281         at32_select_gpio(GPIO_PIN_PA(4), AT32_GPIOF_PULLUP);
282
283         at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info));
284
285         at32_add_device_mci(0, &mci0_data);
286
287 #if IS_ENABLED(CONFIG_LEDS_PWM)
288         pwm_add_table(pwm_lookup, ARRAY_SIZE(pwm_lookup));
289         at32_add_device_pwm((1 << 0) | (1 << 2));
290         platform_device_register(&leds_pwm);
291 #else
292         at32_add_device_pwm((1 << 2));
293 #endif
294
295         at32_select_gpio(i2c_gpio_data.sda_pin,
296                 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
297         at32_select_gpio(i2c_gpio_data.scl_pin,
298                 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
299         platform_device_register(&i2c_gpio_device);
300
301         i2c_register_board_info(0, i2c_info, ARRAY_SIZE(i2c_info));
302
303         return 0;
304 }
305 postcore_initcall(merisc_init);