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