1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/arm/mach-omap1/board-ams-delta.c
4  *
5  * Modified from board-generic.c
6  *
7  * Board specific inits for the Amstrad E3 (codename Delta) videophone
8  *
9  * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
10  */
11 #include <linux/gpio/driver.h>
12 #include <linux/gpio/machine.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/leds.h>
19 #include <linux/mtd/nand-gpio.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/fixed.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/serial_8250.h>
26 #include <linux/export.h>
27 #include <linux/omapfb.h>
28 #include <linux/io.h>
29 #include <linux/platform_data/gpio-omap.h>
30 #include <linux/soc/ti/omap1-mux.h>
31 
32 #include <asm/serial.h>
33 #include <asm/mach-types.h>
34 #include <asm/mach/arch.h>
35 #include <asm/mach/map.h>
36 
37 #include <linux/platform_data/keypad-omap.h>
38 
39 #include "hardware.h"
40 #include "usb.h"
41 #include "ams-delta-fiq.h"
42 #include "board-ams-delta.h"
43 #include "iomap.h"
44 #include "common.h"
45 
46 static const unsigned int ams_delta_keymap[] = {
47 	KEY(0, 0, KEY_F1),		/* Advert    */
48 
49 	KEY(0, 3, KEY_COFFEE),		/* Games     */
50 	KEY(0, 2, KEY_QUESTION),	/* Directory */
51 	KEY(2, 3, KEY_CONNECT),		/* Internet  */
52 	KEY(1, 2, KEY_SHOP),		/* Services  */
53 	KEY(1, 1, KEY_PHONE),		/* VoiceMail */
54 
55 	KEY(0, 1, KEY_DELETE),		/* Delete    */
56 	KEY(2, 2, KEY_PLAY),		/* Play      */
57 	KEY(1, 0, KEY_PAGEUP),		/* Up        */
58 	KEY(1, 3, KEY_PAGEDOWN),	/* Down      */
59 	KEY(2, 0, KEY_EMAIL),		/* ReadEmail */
60 	KEY(2, 1, KEY_STOP),		/* Stop      */
61 
62 	/* Numeric keypad portion */
63 	KEY(0, 7, KEY_KP1),
64 	KEY(0, 6, KEY_KP2),
65 	KEY(0, 5, KEY_KP3),
66 	KEY(1, 7, KEY_KP4),
67 	KEY(1, 6, KEY_KP5),
68 	KEY(1, 5, KEY_KP6),
69 	KEY(2, 7, KEY_KP7),
70 	KEY(2, 6, KEY_KP8),
71 	KEY(2, 5, KEY_KP9),
72 	KEY(3, 6, KEY_KP0),
73 	KEY(3, 7, KEY_KPASTERISK),
74 	KEY(3, 5, KEY_KPDOT),		/* # key     */
75 	KEY(7, 2, KEY_NUMLOCK),		/* Mute      */
76 	KEY(7, 1, KEY_KPMINUS),		/* Recall    */
77 	KEY(6, 1, KEY_KPPLUS),		/* Redial    */
78 	KEY(7, 6, KEY_KPSLASH),		/* Handsfree */
79 	KEY(6, 0, KEY_ENTER),		/* Video     */
80 
81 	KEY(7, 4, KEY_CAMERA),		/* Photo     */
82 
83 	KEY(0, 4, KEY_F2),		/* Home      */
84 	KEY(1, 4, KEY_F3),		/* Office    */
85 	KEY(2, 4, KEY_F4),		/* Mobile    */
86 	KEY(7, 7, KEY_F5),		/* SMS       */
87 	KEY(7, 5, KEY_F6),		/* Email     */
88 
89 	/* QWERTY portion of keypad */
90 	KEY(3, 4, KEY_Q),
91 	KEY(3, 3, KEY_W),
92 	KEY(3, 2, KEY_E),
93 	KEY(3, 1, KEY_R),
94 	KEY(3, 0, KEY_T),
95 	KEY(4, 7, KEY_Y),
96 	KEY(4, 6, KEY_U),
97 	KEY(4, 5, KEY_I),
98 	KEY(4, 4, KEY_O),
99 	KEY(4, 3, KEY_P),
100 
101 	KEY(4, 2, KEY_A),
102 	KEY(4, 1, KEY_S),
103 	KEY(4, 0, KEY_D),
104 	KEY(5, 7, KEY_F),
105 	KEY(5, 6, KEY_G),
106 	KEY(5, 5, KEY_H),
107 	KEY(5, 4, KEY_J),
108 	KEY(5, 3, KEY_K),
109 	KEY(5, 2, KEY_L),
110 
111 	KEY(5, 1, KEY_Z),
112 	KEY(5, 0, KEY_X),
113 	KEY(6, 7, KEY_C),
114 	KEY(6, 6, KEY_V),
115 	KEY(6, 5, KEY_B),
116 	KEY(6, 4, KEY_N),
117 	KEY(6, 3, KEY_M),
118 	KEY(6, 2, KEY_SPACE),
119 
120 	KEY(7, 0, KEY_LEFTSHIFT),	/* Vol up    */
121 	KEY(7, 3, KEY_LEFTCTRL),	/* Vol down  */
122 };
123 
124 #define LATCH1_PHYS	0x01000000
125 #define LATCH1_VIRT	0xEA000000
126 #define MODEM_PHYS	0x04000000
127 #define MODEM_VIRT	0xEB000000
128 #define LATCH2_PHYS	0x08000000
129 #define LATCH2_VIRT	0xEC000000
130 
131 static struct map_desc ams_delta_io_desc[] __initdata = {
132 	/* AMS_DELTA_LATCH1 */
133 	{
134 		.virtual	= LATCH1_VIRT,
135 		.pfn		= __phys_to_pfn(LATCH1_PHYS),
136 		.length		= 0x01000000,
137 		.type		= MT_DEVICE
138 	},
139 	/* AMS_DELTA_LATCH2 */
140 	{
141 		.virtual	= LATCH2_VIRT,
142 		.pfn		= __phys_to_pfn(LATCH2_PHYS),
143 		.length		= 0x01000000,
144 		.type		= MT_DEVICE
145 	},
146 	/* AMS_DELTA_MODEM */
147 	{
148 		.virtual	= MODEM_VIRT,
149 		.pfn		= __phys_to_pfn(MODEM_PHYS),
150 		.length		= 0x01000000,
151 		.type		= MT_DEVICE
152 	}
153 };
154 
155 static const struct omap_lcd_config ams_delta_lcd_config __initconst = {
156 	.ctrl_name	= "internal",
157 };
158 
159 static struct omap_usb_config ams_delta_usb_config __initdata = {
160 	.register_host	= 1,
161 	.hmc_mode	= 16,
162 	.pins[0]	= 2,
163 };
164 
165 #define LATCH1_NGPIO		8
166 
167 static struct resource latch1_resources[] = {
168 	[0] = {
169 		.name	= "dat",
170 		.start	= LATCH1_PHYS,
171 		.end	= LATCH1_PHYS + (LATCH1_NGPIO - 1) / 8,
172 		.flags	= IORESOURCE_MEM,
173 	},
174 };
175 
176 #define LATCH1_LABEL	"latch1"
177 
178 static struct bgpio_pdata latch1_pdata = {
179 	.label	= LATCH1_LABEL,
180 	.base	= -1,
181 	.ngpio	= LATCH1_NGPIO,
182 };
183 
184 static struct platform_device latch1_gpio_device = {
185 	.name		= "basic-mmio-gpio",
186 	.id		= 0,
187 	.resource	= latch1_resources,
188 	.num_resources	= ARRAY_SIZE(latch1_resources),
189 	.dev		= {
190 		.platform_data	= &latch1_pdata,
191 	},
192 };
193 
194 #define LATCH1_PIN_LED_CAMERA		0
195 #define LATCH1_PIN_LED_ADVERT		1
196 #define LATCH1_PIN_LED_MAIL		2
197 #define LATCH1_PIN_LED_HANDSFREE	3
198 #define LATCH1_PIN_LED_VOICEMAIL	4
199 #define LATCH1_PIN_LED_VOICE		5
200 #define LATCH1_PIN_DOCKIT1		6
201 #define LATCH1_PIN_DOCKIT2		7
202 
203 #define LATCH2_NGPIO			16
204 
205 static struct resource latch2_resources[] = {
206 	[0] = {
207 		.name	= "dat",
208 		.start	= LATCH2_PHYS,
209 		.end	= LATCH2_PHYS + (LATCH2_NGPIO - 1) / 8,
210 		.flags	= IORESOURCE_MEM,
211 	},
212 };
213 
214 #define LATCH2_LABEL	"latch2"
215 
216 static struct bgpio_pdata latch2_pdata = {
217 	.label	= LATCH2_LABEL,
218 	.base	= -1,
219 	.ngpio	= LATCH2_NGPIO,
220 };
221 
222 static struct platform_device latch2_gpio_device = {
223 	.name		= "basic-mmio-gpio",
224 	.id		= 1,
225 	.resource	= latch2_resources,
226 	.num_resources	= ARRAY_SIZE(latch2_resources),
227 	.dev		= {
228 		.platform_data	= &latch2_pdata,
229 	},
230 };
231 
232 #define LATCH2_PIN_LCD_VBLEN		0
233 #define LATCH2_PIN_LCD_NDISP		1
234 #define LATCH2_PIN_NAND_NCE		2
235 #define LATCH2_PIN_NAND_NRE		3
236 #define LATCH2_PIN_NAND_NWP		4
237 #define LATCH2_PIN_NAND_NWE		5
238 #define LATCH2_PIN_NAND_ALE		6
239 #define LATCH2_PIN_NAND_CLE		7
240 #define LATCH2_PIN_KEYBRD_PWR		8
241 #define LATCH2_PIN_KEYBRD_DATAOUT	9
242 #define LATCH2_PIN_SCARD_RSTIN		10
243 #define LATCH2_PIN_SCARD_CMDVCC		11
244 #define LATCH2_PIN_MODEM_NRESET		12
245 #define LATCH2_PIN_MODEM_CODEC		13
246 #define LATCH2_PIN_HANDSFREE_MUTE	14
247 #define LATCH2_PIN_HANDSET_MUTE		15
248 
249 static struct regulator_consumer_supply modem_nreset_consumers[] = {
250 	REGULATOR_SUPPLY("RESET#", "serial8250.1"),
251 	REGULATOR_SUPPLY("POR", "cx20442-codec"),
252 };
253 
254 static struct regulator_init_data modem_nreset_data = {
255 	.constraints		= {
256 		.valid_ops_mask		= REGULATOR_CHANGE_STATUS,
257 		.boot_on		= 1,
258 	},
259 	.num_consumer_supplies	= ARRAY_SIZE(modem_nreset_consumers),
260 	.consumer_supplies	= modem_nreset_consumers,
261 };
262 
263 static struct fixed_voltage_config modem_nreset_config = {
264 	.supply_name		= "modem_nreset",
265 	.microvolts		= 3300000,
266 	.startup_delay		= 25000,
267 	.enabled_at_boot	= 1,
268 	.init_data		= &modem_nreset_data,
269 };
270 
271 static struct platform_device modem_nreset_device = {
272 	.name	= "reg-fixed-voltage",
273 	.id	= -1,
274 	.dev	= {
275 		.platform_data	= &modem_nreset_config,
276 	},
277 };
278 
279 static struct gpiod_lookup_table ams_delta_nreset_gpiod_table = {
280 	.dev_id = "reg-fixed-voltage",
281 	.table = {
282 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_MODEM_NRESET,
283 			    NULL, GPIO_ACTIVE_HIGH),
284 		{ },
285 	},
286 };
287 
288 struct modem_private_data {
289 	struct regulator *regulator;
290 };
291 
292 static struct modem_private_data modem_priv;
293 
294 /*
295  * Define partitions for flash device
296  */
297 
298 static struct mtd_partition partition_info[] = {
299 	{ .name		= "Kernel",
300 	  .offset	= 0,
301 	  .size		= 3 * SZ_1M + SZ_512K },
302 	{ .name		= "u-boot",
303 	  .offset	= 3 * SZ_1M + SZ_512K,
304 	  .size		= SZ_256K },
305 	{ .name		= "u-boot params",
306 	  .offset	= 3 * SZ_1M + SZ_512K + SZ_256K,
307 	  .size		= SZ_256K },
308 	{ .name		= "Amstrad LDR",
309 	  .offset	= 4 * SZ_1M,
310 	  .size		= SZ_256K },
311 	{ .name		= "File system",
312 	  .offset	= 4 * SZ_1M + 1 * SZ_256K,
313 	  .size		= 27 * SZ_1M },
314 	{ .name		= "PBL reserved",
315 	  .offset	= 32 * SZ_1M - 3 * SZ_256K,
316 	  .size		=  3 * SZ_256K },
317 };
318 
319 static struct gpio_nand_platdata nand_platdata = {
320 	.parts		= partition_info,
321 	.num_parts	= ARRAY_SIZE(partition_info),
322 };
323 
324 static struct platform_device ams_delta_nand_device = {
325 	.name	= "ams-delta-nand",
326 	.id	= -1,
327 	.dev	= {
328 		.platform_data = &nand_platdata,
329 	},
330 };
331 
332 #define OMAP_GPIO_LABEL		"gpio-0-15"
333 #define OMAP_MPUIO_LABEL	"mpuio"
334 
335 static struct gpiod_lookup_table ams_delta_nand_gpio_table = {
336 	.table = {
337 		GPIO_LOOKUP(OMAP_GPIO_LABEL, AMS_DELTA_GPIO_PIN_NAND_RB, "rdy",
338 			    0),
339 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_NCE, "nce",
340 			    GPIO_ACTIVE_LOW),
341 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_NRE, "nre",
342 			    GPIO_ACTIVE_LOW),
343 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_NWP, "nwp",
344 			    GPIO_ACTIVE_LOW),
345 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_NWE, "nwe",
346 			    GPIO_ACTIVE_LOW),
347 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_ALE, "ale", 0),
348 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_NAND_CLE, "cle", 0),
349 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 0, "data", 0, 0),
350 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 1, "data", 1, 0),
351 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 2, "data", 2, 0),
352 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 3, "data", 3, 0),
353 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 4, "data", 4, 0),
354 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 5, "data", 5, 0),
355 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 6, "data", 6, 0),
356 		GPIO_LOOKUP_IDX(OMAP_MPUIO_LABEL, 7, "data", 7, 0),
357 		{ },
358 	},
359 };
360 
361 static struct resource ams_delta_kp_resources[] = {
362 	[0] = {
363 		.start	= INT_KEYBOARD,
364 		.end	= INT_KEYBOARD,
365 		.flags	= IORESOURCE_IRQ,
366 	},
367 };
368 
369 static const struct matrix_keymap_data ams_delta_keymap_data = {
370 	.keymap		= ams_delta_keymap,
371 	.keymap_size	= ARRAY_SIZE(ams_delta_keymap),
372 };
373 
374 static struct omap_kp_platform_data ams_delta_kp_data = {
375 	.rows		= 8,
376 	.cols		= 8,
377 	.keymap_data	= &ams_delta_keymap_data,
378 	.delay		= 9,
379 };
380 
381 static struct platform_device ams_delta_kp_device = {
382 	.name		= "omap-keypad",
383 	.id		= -1,
384 	.dev		= {
385 		.platform_data = &ams_delta_kp_data,
386 	},
387 	.num_resources	= ARRAY_SIZE(ams_delta_kp_resources),
388 	.resource	= ams_delta_kp_resources,
389 };
390 
391 static struct platform_device ams_delta_lcd_device = {
392 	.name	= "lcd_ams_delta",
393 	.id	= -1,
394 };
395 
396 static struct gpiod_lookup_table ams_delta_lcd_gpio_table = {
397 	.table = {
398 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_LCD_VBLEN, "vblen", 0),
399 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_LCD_NDISP, "ndisp", 0),
400 		{ },
401 	},
402 };
403 
404 static struct gpio_led gpio_leds[] __initdata = {
405 	[LATCH1_PIN_LED_CAMERA] = {
406 		.name		 = "camera",
407 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
408 	},
409 	[LATCH1_PIN_LED_ADVERT] = {
410 		.name		 = "advert",
411 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
412 	},
413 	[LATCH1_PIN_LED_MAIL] = {
414 		.name		 = "email",
415 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
416 	},
417 	[LATCH1_PIN_LED_HANDSFREE] = {
418 		.name		 = "handsfree",
419 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
420 	},
421 	[LATCH1_PIN_LED_VOICEMAIL] = {
422 		.name		 = "voicemail",
423 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
424 	},
425 	[LATCH1_PIN_LED_VOICE] = {
426 		.name		 = "voice",
427 		.default_state	 = LEDS_GPIO_DEFSTATE_OFF,
428 	},
429 };
430 
431 static const struct gpio_led_platform_data leds_pdata __initconst = {
432 	.leds		= gpio_leds,
433 	.num_leds	= ARRAY_SIZE(gpio_leds),
434 };
435 
436 static struct gpiod_lookup_table leds_gpio_table = {
437 	.table = {
438 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_CAMERA, NULL,
439 				LATCH1_PIN_LED_CAMERA, 0),
440 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_ADVERT, NULL,
441 				LATCH1_PIN_LED_ADVERT, 0),
442 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_MAIL, NULL,
443 				LATCH1_PIN_LED_MAIL, 0),
444 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_HANDSFREE, NULL,
445 				LATCH1_PIN_LED_HANDSFREE, 0),
446 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_VOICEMAIL, NULL,
447 				LATCH1_PIN_LED_VOICEMAIL, 0),
448 		GPIO_LOOKUP_IDX(LATCH1_LABEL, LATCH1_PIN_LED_VOICE, NULL,
449 				LATCH1_PIN_LED_VOICE, 0),
450 		{ },
451 	},
452 };
453 
454 static struct platform_device ams_delta_audio_device = {
455 	.name   = "ams-delta-audio",
456 	.id     = -1,
457 };
458 
459 static struct gpiod_lookup_table ams_delta_audio_gpio_table = {
460 	.table = {
461 		GPIO_LOOKUP(OMAP_GPIO_LABEL, AMS_DELTA_GPIO_PIN_HOOK_SWITCH,
462 			    "hook_switch", 0),
463 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_MODEM_CODEC,
464 			    "modem_codec", 0),
465 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_HANDSFREE_MUTE,
466 			    "handsfree_mute", 0),
467 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_HANDSET_MUTE,
468 			    "handset_mute", 0),
469 		{ },
470 	},
471 };
472 
473 static struct platform_device cx20442_codec_device = {
474 	.name   = "cx20442-codec",
475 	.id     = -1,
476 };
477 
478 static struct resource ams_delta_serio_resources[] = {
479 	{
480 		.flags	= IORESOURCE_IRQ,
481 		/*
482 		 * Initialize IRQ resource with invalid IRQ number.
483 		 * It will be replaced with dynamically allocated GPIO IRQ
484 		 * obtained from GPIO chip as soon as the chip is available.
485 		 */
486 		.start	= -EINVAL,
487 		.end	= -EINVAL,
488 	},
489 };
490 
491 static struct platform_device ams_delta_serio_device = {
492 	.name		= "ams-delta-serio",
493 	.id		= PLATFORM_DEVID_NONE,
494 	.dev		= {
495 		/*
496 		 * Initialize .platform_data explicitly with NULL to
497 		 * indicate it is going to be used.  It will be replaced
498 		 * with FIQ buffer address as soon as FIQ is initialized.
499 		 */
500 		.platform_data = NULL,
501 	},
502 	.num_resources	= ARRAY_SIZE(ams_delta_serio_resources),
503 	.resource	= ams_delta_serio_resources,
504 };
505 
506 static struct regulator_consumer_supply keybrd_pwr_consumers[] = {
507 	/*
508 	 * Initialize supply .dev_name with NULL.  It will be replaced
509 	 * with serio dev_name() as soon as the serio device is registered.
510 	 */
511 	REGULATOR_SUPPLY("vcc", NULL),
512 };
513 
514 static struct regulator_init_data keybrd_pwr_initdata = {
515 	.constraints		= {
516 		.valid_ops_mask		= REGULATOR_CHANGE_STATUS,
517 	},
518 	.num_consumer_supplies	= ARRAY_SIZE(keybrd_pwr_consumers),
519 	.consumer_supplies	= keybrd_pwr_consumers,
520 };
521 
522 static struct fixed_voltage_config keybrd_pwr_config = {
523 	.supply_name		= "keybrd_pwr",
524 	.microvolts		= 5000000,
525 	.init_data		= &keybrd_pwr_initdata,
526 };
527 
528 static struct platform_device keybrd_pwr_device = {
529 	.name	= "reg-fixed-voltage",
530 	.id	= PLATFORM_DEVID_AUTO,
531 	.dev	= {
532 		.platform_data	= &keybrd_pwr_config,
533 	},
534 };
535 
536 static struct gpiod_lookup_table keybrd_pwr_gpio_table = {
537 	.table = {
538 		GPIO_LOOKUP(LATCH2_LABEL, LATCH2_PIN_KEYBRD_PWR, NULL,
539 			    GPIO_ACTIVE_HIGH),
540 		{ },
541 	},
542 };
543 
544 static struct platform_device *ams_delta_devices[] __initdata = {
545 	&latch1_gpio_device,
546 	&latch2_gpio_device,
547 	&ams_delta_kp_device,
548 	&ams_delta_audio_device,
549 	&ams_delta_serio_device,
550 	&ams_delta_nand_device,
551 	&ams_delta_lcd_device,
552 	&cx20442_codec_device,
553 };
554 
555 static struct gpiod_lookup_table *ams_delta_gpio_tables[] __initdata = {
556 	&ams_delta_nreset_gpiod_table,
557 	&ams_delta_audio_gpio_table,
558 	&keybrd_pwr_gpio_table,
559 	&ams_delta_lcd_gpio_table,
560 	&ams_delta_nand_gpio_table,
561 };
562 
563 /*
564  * Some drivers may not use GPIO lookup tables but need to be provided
565  * with GPIO numbers.  The same applies to GPIO based IRQ lines - some
566  * drivers may even not use GPIO layer but expect just IRQ numbers.
567  * We could either define GPIO lookup tables then use them on behalf
568  * of those devices, or we can use GPIO driver level methods for
569  * identification of GPIO and IRQ numbers. For the purpose of the latter,
570  * defina a helper function which identifies GPIO chips by their labels.
571  */
572 static int gpiochip_match_by_label(struct gpio_chip *chip, void *data)
573 {
574 	char *label = data;
575 
576 	return !strcmp(label, chip->label);
577 }
578 
579 static struct gpiod_hog ams_delta_gpio_hogs[] = {
580 	GPIO_HOG(LATCH2_LABEL, LATCH2_PIN_KEYBRD_DATAOUT, "keybrd_dataout",
581 		 GPIO_ACTIVE_HIGH, GPIOD_OUT_LOW),
582 	{},
583 };
584 
585 static struct plat_serial8250_port ams_delta_modem_ports[];
586 
587 /*
588  * Obtain MODEM IRQ GPIO descriptor using its hardware pin
589  * number and assign related IRQ number to the MODEM port.
590  * Keep the GPIO descriptor open so nobody steps in.
591  */
592 static void __init modem_assign_irq(struct gpio_chip *chip)
593 {
594 	struct gpio_desc *gpiod;
595 
596 	gpiod = gpiochip_request_own_desc(chip, AMS_DELTA_GPIO_PIN_MODEM_IRQ,
597 					  "modem_irq", GPIO_ACTIVE_HIGH,
598 					  GPIOD_IN);
599 	if (IS_ERR(gpiod)) {
600 		pr_err("%s: modem IRQ GPIO request failed (%ld)\n", __func__,
601 		       PTR_ERR(gpiod));
602 	} else {
603 		ams_delta_modem_ports[0].irq = gpiod_to_irq(gpiod);
604 	}
605 }
606 
607 /*
608  * The purpose of this function is to take care of proper initialization of
609  * devices and data structures which depend on GPIO lines provided by OMAP GPIO
610  * banks but their drivers don't use GPIO lookup tables or GPIO layer at all.
611  * The function may be called as soon as OMAP GPIO devices are probed.
612  * Since that happens at postcore_initcall, it can be called successfully
613  * from init_machine or later.
614  * Dependent devices may be registered from within this function or later.
615  */
616 static void __init omap_gpio_deps_init(void)
617 {
618 	struct gpio_chip *chip;
619 
620 	chip = gpiochip_find(OMAP_GPIO_LABEL, gpiochip_match_by_label);
621 	if (!chip) {
622 		pr_err("%s: OMAP GPIO chip not found\n", __func__);
623 		return;
624 	}
625 
626 	/*
627 	 * Start with FIQ initialization as it may have to request
628 	 * and release successfully each OMAP GPIO pin in turn.
629 	 */
630 	ams_delta_init_fiq(chip, &ams_delta_serio_device);
631 
632 	modem_assign_irq(chip);
633 }
634 
635 /*
636  * Initialize latch2 pins with values which are safe for dependent on-board
637  * devices or useful for their successull initialization even before GPIO
638  * driver takes control over the latch pins:
639  * - LATCH2_PIN_LCD_VBLEN	= 0
640  * - LATCH2_PIN_LCD_NDISP	= 0	Keep LCD device powered off before its
641  *					driver takes control over it.
642  * - LATCH2_PIN_NAND_NCE	= 0
643  * - LATCH2_PIN_NAND_NWP	= 0	Keep NAND device down and write-
644  *					protected before its driver takes
645  *					control over it.
646  * - LATCH2_PIN_KEYBRD_PWR	= 0	Keep keyboard powered off before serio
647  *					driver takes control over it.
648  * - LATCH2_PIN_KEYBRD_DATAOUT	= 0	Keep low to avoid corruption of first
649  *					byte of data received from attached
650  *					keyboard when serio device is probed;
651  *					the pin is also hogged low by the latch2
652  *					GPIO driver as soon as it is ready.
653  * - LATCH2_PIN_MODEM_NRESET	= 1	Enable voice MODEM device, allowing for
654  *					its successful probe even before a
655  *					regulator it depends on, which in turn
656  *					takes control over the pin, is set up.
657  * - LATCH2_PIN_MODEM_CODEC	= 1	Attach voice MODEM CODEC data port
658  *					to the MODEM so the CODEC is under
659  *					control even if audio driver doesn't
660  *					take it over.
661  */
662 static void __init ams_delta_latch2_init(void)
663 {
664 	u16 latch2 = 1 << LATCH2_PIN_MODEM_NRESET | 1 << LATCH2_PIN_MODEM_CODEC;
665 
666 	__raw_writew(latch2, IOMEM(LATCH2_VIRT));
667 }
668 
669 static void __init ams_delta_init(void)
670 {
671 	struct platform_device *leds_pdev;
672 
673 	/* mux pins for uarts */
674 	omap_cfg_reg(UART1_TX);
675 	omap_cfg_reg(UART1_RTS);
676 
677 	/* parallel camera interface */
678 	omap_cfg_reg(H19_1610_CAM_EXCLK);
679 	omap_cfg_reg(J15_1610_CAM_LCLK);
680 	omap_cfg_reg(L18_1610_CAM_VS);
681 	omap_cfg_reg(L15_1610_CAM_HS);
682 	omap_cfg_reg(L19_1610_CAM_D0);
683 	omap_cfg_reg(K14_1610_CAM_D1);
684 	omap_cfg_reg(K15_1610_CAM_D2);
685 	omap_cfg_reg(K19_1610_CAM_D3);
686 	omap_cfg_reg(K18_1610_CAM_D4);
687 	omap_cfg_reg(J14_1610_CAM_D5);
688 	omap_cfg_reg(J19_1610_CAM_D6);
689 	omap_cfg_reg(J18_1610_CAM_D7);
690 
691 	omap_gpio_deps_init();
692 	ams_delta_latch2_init();
693 	gpiod_add_hogs(ams_delta_gpio_hogs);
694 
695 	omap_serial_init();
696 	omap_register_i2c_bus(1, 100, NULL, 0);
697 
698 	omap1_usb_init(&ams_delta_usb_config);
699 	platform_add_devices(ams_delta_devices, ARRAY_SIZE(ams_delta_devices));
700 
701 	/*
702 	 * As soon as regulator consumers have been registered, assign their
703 	 * dev_names to consumer supply entries of respective regulators.
704 	 */
705 	keybrd_pwr_consumers[0].dev_name =
706 			dev_name(&ams_delta_serio_device.dev);
707 
708 	/*
709 	 * Once consumer supply entries are populated with dev_names,
710 	 * register regulator devices.  At this stage only the keyboard
711 	 * power regulator has its consumer supply table fully populated.
712 	 */
713 	platform_device_register(&keybrd_pwr_device);
714 
715 	/*
716 	 * As soon as GPIO consumers have been registered, assign
717 	 * their dev_names to respective GPIO lookup tables.
718 	 */
719 	ams_delta_audio_gpio_table.dev_id =
720 			dev_name(&ams_delta_audio_device.dev);
721 	keybrd_pwr_gpio_table.dev_id = dev_name(&keybrd_pwr_device.dev);
722 	ams_delta_nand_gpio_table.dev_id = dev_name(&ams_delta_nand_device.dev);
723 	ams_delta_lcd_gpio_table.dev_id = dev_name(&ams_delta_lcd_device.dev);
724 
725 	/*
726 	 * Once GPIO lookup tables are populated with dev_names, register them.
727 	 */
728 	gpiod_add_lookup_tables(ams_delta_gpio_tables,
729 				ARRAY_SIZE(ams_delta_gpio_tables));
730 
731 	leds_pdev = gpio_led_register_device(PLATFORM_DEVID_NONE, &leds_pdata);
732 	if (!IS_ERR_OR_NULL(leds_pdev)) {
733 		leds_gpio_table.dev_id = dev_name(&leds_pdev->dev);
734 		gpiod_add_lookup_table(&leds_gpio_table);
735 	}
736 
737 	omap_writew(omap_readw(ARM_RSTCT1) | 0x0004, ARM_RSTCT1);
738 
739 	omapfb_set_lcd_config(&ams_delta_lcd_config);
740 }
741 
742 static void modem_pm(struct uart_port *port, unsigned int state, unsigned old)
743 {
744 	struct modem_private_data *priv = port->private_data;
745 	int ret;
746 
747 	if (!priv)
748 		return;
749 
750 	if (IS_ERR(priv->regulator))
751 		return;
752 
753 	if (state == old)
754 		return;
755 
756 	if (state == 0)
757 		ret = regulator_enable(priv->regulator);
758 	else if (old == 0)
759 		ret = regulator_disable(priv->regulator);
760 	else
761 		ret = 0;
762 
763 	if (ret)
764 		dev_warn(port->dev,
765 			 "ams_delta modem_pm: failed to %sable regulator: %d\n",
766 			 state ? "dis" : "en", ret);
767 }
768 
769 static struct plat_serial8250_port ams_delta_modem_ports[] = {
770 	{
771 		.membase	= IOMEM(MODEM_VIRT),
772 		.mapbase	= MODEM_PHYS,
773 		.irq		= IRQ_NOTCONNECTED, /* changed later */
774 		.flags		= UPF_BOOT_AUTOCONF,
775 		.irqflags	= IRQF_TRIGGER_RISING,
776 		.iotype		= UPIO_MEM,
777 		.regshift	= 1,
778 		.uartclk	= BASE_BAUD * 16,
779 		.pm		= modem_pm,
780 		.private_data	= &modem_priv,
781 	},
782 	{ },
783 };
784 
785 static struct platform_device ams_delta_modem_device = {
786 	.name	= "serial8250",
787 	.id	= PLAT8250_DEV_PLATFORM1,
788 	.dev		= {
789 		.platform_data = ams_delta_modem_ports,
790 	},
791 };
792 
793 static int __init modem_nreset_init(void)
794 {
795 	int err;
796 
797 	err = platform_device_register(&modem_nreset_device);
798 	if (err)
799 		pr_err("Couldn't register the modem regulator device\n");
800 
801 	return err;
802 }
803 
804 
805 /*
806  * This function expects MODEM IRQ number already assigned to the port.
807  * The MODEM device requires its RESET# pin kept high during probe.
808  * That requirement can be fulfilled in several ways:
809  * - with a descriptor of already functional modem_nreset regulator
810  *   assigned to the MODEM private data,
811  * - with the regulator not yet controlled by modem_pm function but
812  *   already enabled by default on probe,
813  * - before the modem_nreset regulator is probed, with the pin already
814  *   set high explicitly.
815  * The last one is already guaranteed by ams_delta_latch2_init() called
816  * from machine_init.
817  * In order to avoid taking over ttyS0 device slot, the MODEM device
818  * should be registered after OMAP serial ports.  Since those ports
819  * are registered at arch_initcall, this function can be called safely
820  * at arch_initcall_sync earliest.
821  */
822 static int __init ams_delta_modem_init(void)
823 {
824 	if (!machine_is_ams_delta())
825 		return -ENODEV;
826 
827 	omap_cfg_reg(M14_1510_GPIO2);
828 
829 	/* Initialize the modem_nreset regulator consumer before use */
830 	modem_priv.regulator = ERR_PTR(-ENODEV);
831 
832 	return platform_device_register(&ams_delta_modem_device);
833 }
834 arch_initcall_sync(ams_delta_modem_init);
835 
836 static int __init late_init(void)
837 {
838 	int err;
839 
840 	err = modem_nreset_init();
841 	if (err)
842 		return err;
843 
844 	/*
845 	 * Once the modem device is registered, the modem_nreset
846 	 * regulator can be requested on behalf of that device.
847 	 */
848 	modem_priv.regulator = regulator_get(&ams_delta_modem_device.dev,
849 			"RESET#");
850 	if (IS_ERR(modem_priv.regulator)) {
851 		err = PTR_ERR(modem_priv.regulator);
852 		goto unregister;
853 	}
854 	return 0;
855 
856 unregister:
857 	platform_device_unregister(&ams_delta_modem_device);
858 	return err;
859 }
860 
861 static void __init ams_delta_init_late(void)
862 {
863 	omap1_init_late();
864 	late_init();
865 }
866 
867 static void __init ams_delta_map_io(void)
868 {
869 	omap1_map_io();
870 	iotable_init(ams_delta_io_desc, ARRAY_SIZE(ams_delta_io_desc));
871 }
872 
873 MACHINE_START(AMS_DELTA, "Amstrad E3 (Delta)")
874 	/* Maintainer: Jonathan McDowell <noodles@earth.li> */
875 	.atag_offset	= 0x100,
876 	.map_io		= ams_delta_map_io,
877 	.init_early	= omap1_init_early,
878 	.init_irq	= omap1_init_irq,
879 	.init_machine	= ams_delta_init,
880 	.init_late	= ams_delta_init_late,
881 	.init_time	= omap1_timer_init,
882 	.restart	= omap1_restart,
883 MACHINE_END
884