sched/fair: Avoid redundant EAS calculation
[sfrench/cifs-2.6.git] / arch / mips / jz4740 / board-qi_lb60.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * linux/arch/mips/jz4740/board-qi_lb60.c
4  *
5  * QI_LB60 board support
6  *
7  * Copyright (c) 2009 Qi Hardware inc.,
8  * Author: Xiangfu Liu <xiangfu@qi-hardware.com>
9  * Copyright 2010, Lars-Peter Clausen <lars@metafoo.de>
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/machine.h>
16
17 #include <linux/input.h>
18 #include <linux/gpio_keys.h>
19 #include <linux/input/matrix_keypad.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi_gpio.h>
22 #include <linux/pinctrl/machine.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/power_supply.h>
25 #include <linux/power/jz4740-battery.h>
26 #include <linux/power/gpio-charger.h>
27 #include <linux/pwm.h>
28
29 #include <linux/platform_data/jz4740/jz4740_nand.h>
30
31 #include <asm/mach-jz4740/gpio.h>
32 #include <asm/mach-jz4740/jz4740_fb.h>
33 #include <asm/mach-jz4740/jz4740_mmc.h>
34
35 #include <linux/regulator/fixed.h>
36 #include <linux/regulator/machine.h>
37
38 #include <asm/mach-jz4740/platform.h>
39
40 /* GPIOs */
41 #define QI_LB60_GPIO_KEYOUT(x)          (JZ_GPIO_PORTC(10) + (x))
42 #define QI_LB60_GPIO_KEYIN(x)           (JZ_GPIO_PORTD(18) + (x))
43 #define QI_LB60_GPIO_KEYIN8             JZ_GPIO_PORTD(26)
44
45 /* NAND */
46
47 /* Early prototypes of the QI LB60 had only 1GB of NAND.
48  * In order to support these devices as well the partition and ecc layout is
49  * initialized depending on the NAND size */
50 static struct mtd_partition qi_lb60_partitions_1gb[] = {
51         {
52                 .name = "NAND BOOT partition",
53                 .offset = 0 * 0x100000,
54                 .size = 4 * 0x100000,
55         },
56         {
57                 .name = "NAND KERNEL partition",
58                 .offset = 4 * 0x100000,
59                 .size = 4 * 0x100000,
60         },
61         {
62                 .name = "NAND ROOTFS partition",
63                 .offset = 8 * 0x100000,
64                 .size = (504 + 512) * 0x100000,
65         },
66 };
67
68 static struct mtd_partition qi_lb60_partitions_2gb[] = {
69         {
70                 .name = "NAND BOOT partition",
71                 .offset = 0 * 0x100000,
72                 .size = 4 * 0x100000,
73         },
74         {
75                 .name = "NAND KERNEL partition",
76                 .offset = 4 * 0x100000,
77                 .size = 4 * 0x100000,
78         },
79         {
80                 .name = "NAND ROOTFS partition",
81                 .offset = 8 * 0x100000,
82                 .size = (504 + 512 + 1024) * 0x100000,
83         },
84 };
85
86 static int qi_lb60_ooblayout_ecc(struct mtd_info *mtd, int section,
87                                  struct mtd_oob_region *oobregion)
88 {
89         if (section)
90                 return -ERANGE;
91
92         oobregion->length = 36;
93         oobregion->offset = 6;
94
95         if (mtd->oobsize == 128) {
96                 oobregion->length *= 2;
97                 oobregion->offset *= 2;
98         }
99
100         return 0;
101 }
102
103 static int qi_lb60_ooblayout_free(struct mtd_info *mtd, int section,
104                                   struct mtd_oob_region *oobregion)
105 {
106         int eccbytes = 36, eccoff = 6;
107
108         if (section > 1)
109                 return -ERANGE;
110
111         if (mtd->oobsize == 128) {
112                 eccbytes *= 2;
113                 eccoff *= 2;
114         }
115
116         if (!section) {
117                 oobregion->offset = 2;
118                 oobregion->length = eccoff - 2;
119         } else {
120                 oobregion->offset = eccoff + eccbytes;
121                 oobregion->length = mtd->oobsize - oobregion->offset;
122         }
123
124         return 0;
125 }
126
127 static const struct mtd_ooblayout_ops qi_lb60_ooblayout_ops = {
128         .ecc = qi_lb60_ooblayout_ecc,
129         .free = qi_lb60_ooblayout_free,
130 };
131
132 static void qi_lb60_nand_ident(struct platform_device *pdev,
133                 struct mtd_info *mtd, struct mtd_partition **partitions,
134                 int *num_partitions)
135 {
136         struct nand_chip *chip = mtd_to_nand(mtd);
137
138         if (chip->page_shift == 12) {
139                 *partitions = qi_lb60_partitions_2gb;
140                 *num_partitions = ARRAY_SIZE(qi_lb60_partitions_2gb);
141         } else {
142                 *partitions = qi_lb60_partitions_1gb;
143                 *num_partitions = ARRAY_SIZE(qi_lb60_partitions_1gb);
144         }
145
146         mtd_set_ooblayout(mtd, &qi_lb60_ooblayout_ops);
147 }
148
149 static struct jz_nand_platform_data qi_lb60_nand_pdata = {
150         .ident_callback = qi_lb60_nand_ident,
151         .banks = { 1 },
152 };
153
154 static struct gpiod_lookup_table qi_lb60_nand_gpio_table = {
155         .dev_id = "jz4740-nand.0",
156         .table = {
157                 GPIO_LOOKUP("GPIOC", 30, "busy", 0),
158                 { },
159         },
160 };
161
162
163 /* Keyboard*/
164
165 #define KEY_QI_QI       KEY_F13
166 #define KEY_QI_UPRED    KEY_RIGHTALT
167 #define KEY_QI_VOLUP    KEY_VOLUMEUP
168 #define KEY_QI_VOLDOWN  KEY_VOLUMEDOWN
169 #define KEY_QI_FN       KEY_LEFTCTRL
170
171 static const uint32_t qi_lb60_keymap[] = {
172         KEY(0, 0, KEY_F1),      /* S2 */
173         KEY(0, 1, KEY_F2),      /* S3 */
174         KEY(0, 2, KEY_F3),      /* S4 */
175         KEY(0, 3, KEY_F4),      /* S5 */
176         KEY(0, 4, KEY_F5),      /* S6 */
177         KEY(0, 5, KEY_F6),      /* S7 */
178         KEY(0, 6, KEY_F7),      /* S8 */
179
180         KEY(1, 0, KEY_Q),       /* S10 */
181         KEY(1, 1, KEY_W),       /* S11 */
182         KEY(1, 2, KEY_E),       /* S12 */
183         KEY(1, 3, KEY_R),       /* S13 */
184         KEY(1, 4, KEY_T),       /* S14 */
185         KEY(1, 5, KEY_Y),       /* S15 */
186         KEY(1, 6, KEY_U),       /* S16 */
187         KEY(1, 7, KEY_I),       /* S17 */
188         KEY(2, 0, KEY_A),       /* S18 */
189         KEY(2, 1, KEY_S),       /* S19 */
190         KEY(2, 2, KEY_D),       /* S20 */
191         KEY(2, 3, KEY_F),       /* S21 */
192         KEY(2, 4, KEY_G),       /* S22 */
193         KEY(2, 5, KEY_H),       /* S23 */
194         KEY(2, 6, KEY_J),       /* S24 */
195         KEY(2, 7, KEY_K),       /* S25 */
196         KEY(3, 0, KEY_ESC),     /* S26 */
197         KEY(3, 1, KEY_Z),       /* S27 */
198         KEY(3, 2, KEY_X),       /* S28 */
199         KEY(3, 3, KEY_C),       /* S29 */
200         KEY(3, 4, KEY_V),       /* S30 */
201         KEY(3, 5, KEY_B),       /* S31 */
202         KEY(3, 6, KEY_N),       /* S32 */
203         KEY(3, 7, KEY_M),       /* S33 */
204         KEY(4, 0, KEY_TAB),     /* S34 */
205         KEY(4, 1, KEY_CAPSLOCK),        /* S35 */
206         KEY(4, 2, KEY_BACKSLASH),       /* S36 */
207         KEY(4, 3, KEY_APOSTROPHE),      /* S37 */
208         KEY(4, 4, KEY_COMMA),   /* S38 */
209         KEY(4, 5, KEY_DOT),     /* S39 */
210         KEY(4, 6, KEY_SLASH),   /* S40 */
211         KEY(4, 7, KEY_UP),      /* S41 */
212         KEY(5, 0, KEY_O),       /* S42 */
213         KEY(5, 1, KEY_L),       /* S43 */
214         KEY(5, 2, KEY_EQUAL),   /* S44 */
215         KEY(5, 3, KEY_QI_UPRED),        /* S45 */
216         KEY(5, 4, KEY_SPACE),   /* S46 */
217         KEY(5, 5, KEY_QI_QI),   /* S47 */
218         KEY(5, 6, KEY_RIGHTCTRL),       /* S48 */
219         KEY(5, 7, KEY_LEFT),    /* S49 */
220         KEY(6, 0, KEY_F8),      /* S50 */
221         KEY(6, 1, KEY_P),       /* S51 */
222         KEY(6, 2, KEY_BACKSPACE),/* S52 */
223         KEY(6, 3, KEY_ENTER),   /* S53 */
224         KEY(6, 4, KEY_QI_VOLUP),        /* S54 */
225         KEY(6, 5, KEY_QI_VOLDOWN),      /* S55 */
226         KEY(6, 6, KEY_DOWN),    /* S56 */
227         KEY(6, 7, KEY_RIGHT),   /* S57 */
228
229         KEY(7, 0, KEY_LEFTSHIFT),       /* S58 */
230         KEY(7, 1, KEY_LEFTALT), /* S59 */
231         KEY(7, 2, KEY_QI_FN),   /* S60 */
232 };
233
234 static const struct matrix_keymap_data qi_lb60_keymap_data = {
235         .keymap         = qi_lb60_keymap,
236         .keymap_size    = ARRAY_SIZE(qi_lb60_keymap),
237 };
238
239 static const unsigned int qi_lb60_keypad_cols[] = {
240         QI_LB60_GPIO_KEYOUT(0),
241         QI_LB60_GPIO_KEYOUT(1),
242         QI_LB60_GPIO_KEYOUT(2),
243         QI_LB60_GPIO_KEYOUT(3),
244         QI_LB60_GPIO_KEYOUT(4),
245         QI_LB60_GPIO_KEYOUT(5),
246         QI_LB60_GPIO_KEYOUT(6),
247         QI_LB60_GPIO_KEYOUT(7),
248 };
249
250 static const unsigned int qi_lb60_keypad_rows[] = {
251         QI_LB60_GPIO_KEYIN(0),
252         QI_LB60_GPIO_KEYIN(1),
253         QI_LB60_GPIO_KEYIN(2),
254         QI_LB60_GPIO_KEYIN(3),
255         QI_LB60_GPIO_KEYIN(4),
256         QI_LB60_GPIO_KEYIN(5),
257         QI_LB60_GPIO_KEYIN(6),
258         QI_LB60_GPIO_KEYIN8,
259 };
260
261 static struct matrix_keypad_platform_data qi_lb60_pdata = {
262         .keymap_data = &qi_lb60_keymap_data,
263         .col_gpios      = qi_lb60_keypad_cols,
264         .row_gpios      = qi_lb60_keypad_rows,
265         .num_col_gpios  = ARRAY_SIZE(qi_lb60_keypad_cols),
266         .num_row_gpios  = ARRAY_SIZE(qi_lb60_keypad_rows),
267         .col_scan_delay_us      = 10,
268         .debounce_ms            = 10,
269         .wakeup                 = 1,
270         .active_low             = 1,
271 };
272
273 static struct platform_device qi_lb60_keypad = {
274         .name           = "matrix-keypad",
275         .id             = -1,
276         .dev            = {
277                 .platform_data = &qi_lb60_pdata,
278         },
279 };
280
281 /* Display */
282 static struct fb_videomode qi_lb60_video_modes[] = {
283         {
284                 .name = "320x240",
285                 .xres = 320,
286                 .yres = 240,
287                 .refresh = 30,
288                 .left_margin = 140,
289                 .right_margin = 273,
290                 .upper_margin = 20,
291                 .lower_margin = 2,
292                 .hsync_len = 1,
293                 .vsync_len = 1,
294                 .sync = 0,
295                 .vmode = FB_VMODE_NONINTERLACED,
296         },
297 };
298
299 static struct jz4740_fb_platform_data qi_lb60_fb_pdata = {
300         .width          = 60,
301         .height         = 45,
302         .num_modes      = ARRAY_SIZE(qi_lb60_video_modes),
303         .modes          = qi_lb60_video_modes,
304         .bpp            = 24,
305         .lcd_type       = JZ_LCD_TYPE_8BIT_SERIAL,
306         .pixclk_falling_edge = 1,
307 };
308
309 struct spi_gpio_platform_data qi_lb60_spigpio_platform_data = {
310         .num_chipselect = 1,
311 };
312
313 static struct platform_device qi_lb60_spigpio_device = {
314         .name = "spi_gpio",
315         .id   = 1,
316         .dev = {
317                 .platform_data = &qi_lb60_spigpio_platform_data,
318         },
319 };
320
321 static struct gpiod_lookup_table qi_lb60_spigpio_gpio_table = {
322         .dev_id         = "spi_gpio",
323         .table          = {
324                 GPIO_LOOKUP("GPIOC", 23,
325                             "sck", GPIO_ACTIVE_HIGH),
326                 GPIO_LOOKUP("GPIOC", 22,
327                             "mosi", GPIO_ACTIVE_HIGH),
328                 GPIO_LOOKUP("GPIOC", 21,
329                             "cs", GPIO_ACTIVE_HIGH),
330                 { },
331         },
332 };
333
334 static struct spi_board_info qi_lb60_spi_board_info[] = {
335         {
336                 .modalias = "ili8960",
337                 .chip_select = 0,
338                 .bus_num = 1,
339                 .max_speed_hz = 30 * 1000,
340                 .mode = SPI_3WIRE,
341         },
342 };
343
344 /* Battery */
345 static struct jz_battery_platform_data qi_lb60_battery_pdata = {
346         .gpio_charge =  JZ_GPIO_PORTC(27),
347         .gpio_charge_active_low = 1,
348         .info = {
349                 .name = "battery",
350                 .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
351                 .voltage_max_design = 4200000,
352                 .voltage_min_design = 3600000,
353         },
354 };
355
356 /* GPIO Key: power */
357 static struct gpio_keys_button qi_lb60_gpio_keys_buttons[] = {
358         [0] = {
359                 .code           = KEY_POWER,
360                 .gpio           = JZ_GPIO_PORTD(29),
361                 .active_low     = 1,
362                 .desc           = "Power",
363                 .wakeup         = 1,
364         },
365 };
366
367 static struct gpio_keys_platform_data qi_lb60_gpio_keys_data = {
368         .nbuttons = ARRAY_SIZE(qi_lb60_gpio_keys_buttons),
369         .buttons = qi_lb60_gpio_keys_buttons,
370 };
371
372 static struct platform_device qi_lb60_gpio_keys = {
373         .name = "gpio-keys",
374         .id =   -1,
375         .dev = {
376                 .platform_data = &qi_lb60_gpio_keys_data,
377         }
378 };
379
380 static struct jz4740_mmc_platform_data qi_lb60_mmc_pdata = {
381         /* Intentionally left blank */
382 };
383
384 static struct gpiod_lookup_table qi_lb60_mmc_gpio_table = {
385         .dev_id = "jz4740-mmc.0",
386         .table = {
387                 GPIO_LOOKUP("GPIOD", 0, "cd", GPIO_ACTIVE_HIGH),
388                 GPIO_LOOKUP("GPIOD", 2, "power", GPIO_ACTIVE_LOW),
389                 { },
390         },
391 };
392
393 /* beeper */
394 static struct pwm_lookup qi_lb60_pwm_lookup[] = {
395         PWM_LOOKUP("jz4740-pwm", 4, "pwm-beeper", NULL, 0,
396                    PWM_POLARITY_NORMAL),
397 };
398
399 static struct platform_device qi_lb60_pwm_beeper = {
400         .name = "pwm-beeper",
401         .id = -1,
402 };
403
404 /* charger */
405 static char *qi_lb60_batteries[] = {
406         "battery",
407 };
408
409 static struct gpio_charger_platform_data qi_lb60_charger_pdata = {
410         .name = "usb",
411         .type = POWER_SUPPLY_TYPE_USB,
412         .gpio = JZ_GPIO_PORTD(28),
413         .gpio_active_low = 1,
414         .supplied_to = qi_lb60_batteries,
415         .num_supplicants = ARRAY_SIZE(qi_lb60_batteries),
416 };
417
418 static struct platform_device qi_lb60_charger_device = {
419         .name = "gpio-charger",
420         .dev = {
421                 .platform_data = &qi_lb60_charger_pdata,
422         },
423 };
424
425 /* audio */
426 static struct platform_device qi_lb60_audio_device = {
427         .name = "qi-lb60-audio",
428         .id = -1,
429 };
430
431 static struct gpiod_lookup_table qi_lb60_audio_gpio_table = {
432         .dev_id = "qi-lb60-audio",
433         .table = {
434                 GPIO_LOOKUP("GPIOB", 29, "snd", 0),
435                 GPIO_LOOKUP("GPIOD", 4, "amp", 0),
436                 { },
437         },
438 };
439
440 static struct platform_device *jz_platform_devices[] __initdata = {
441         &jz4740_udc_device,
442         &jz4740_udc_xceiv_device,
443         &jz4740_mmc_device,
444         &jz4740_nand_device,
445         &qi_lb60_keypad,
446         &qi_lb60_spigpio_device,
447         &jz4740_framebuffer_device,
448         &jz4740_pcm_device,
449         &jz4740_i2s_device,
450         &jz4740_codec_device,
451         &jz4740_adc_device,
452         &jz4740_pwm_device,
453         &jz4740_dma_device,
454         &qi_lb60_gpio_keys,
455         &qi_lb60_pwm_beeper,
456         &qi_lb60_charger_device,
457         &qi_lb60_audio_device,
458 };
459
460 static unsigned long pin_cfg_bias_disable[] = {
461             PIN_CONFIG_BIAS_DISABLE,
462 };
463
464 static struct pinctrl_map pin_map[] __initdata = {
465         /* NAND pin configuration */
466         PIN_MAP_MUX_GROUP_DEFAULT("jz4740-nand",
467                         "10010000.pin-controller", "nand-cs1", "nand"),
468
469         /* fbdev pin configuration */
470         PIN_MAP_MUX_GROUP("jz4740-fb", PINCTRL_STATE_DEFAULT,
471                         "10010000.pin-controller", "lcd-8bit", "lcd"),
472         PIN_MAP_MUX_GROUP("jz4740-fb", PINCTRL_STATE_SLEEP,
473                         "10010000.pin-controller", "lcd-no-pins", "lcd"),
474
475         /* MMC pin configuration */
476         PIN_MAP_MUX_GROUP_DEFAULT("jz4740-mmc.0",
477                         "10010000.pin-controller", "mmc-1bit", "mmc"),
478         PIN_MAP_MUX_GROUP_DEFAULT("jz4740-mmc.0",
479                         "10010000.pin-controller", "mmc-4bit", "mmc"),
480         PIN_MAP_CONFIGS_PIN_DEFAULT("jz4740-mmc.0",
481                         "10010000.pin-controller", "PD0", pin_cfg_bias_disable),
482         PIN_MAP_CONFIGS_PIN_DEFAULT("jz4740-mmc.0",
483                         "10010000.pin-controller", "PD2", pin_cfg_bias_disable),
484
485         /* PWM pin configuration */
486         PIN_MAP_MUX_GROUP_DEFAULT("jz4740-pwm",
487                         "10010000.pin-controller", "pwm4", "pwm4"),
488 };
489
490
491 static int __init qi_lb60_init_platform_devices(void)
492 {
493         jz4740_framebuffer_device.dev.platform_data = &qi_lb60_fb_pdata;
494         jz4740_nand_device.dev.platform_data = &qi_lb60_nand_pdata;
495         jz4740_adc_device.dev.platform_data = &qi_lb60_battery_pdata;
496         jz4740_mmc_device.dev.platform_data = &qi_lb60_mmc_pdata;
497
498         gpiod_add_lookup_table(&qi_lb60_audio_gpio_table);
499         gpiod_add_lookup_table(&qi_lb60_nand_gpio_table);
500         gpiod_add_lookup_table(&qi_lb60_spigpio_gpio_table);
501         gpiod_add_lookup_table(&qi_lb60_mmc_gpio_table);
502
503         spi_register_board_info(qi_lb60_spi_board_info,
504                                 ARRAY_SIZE(qi_lb60_spi_board_info));
505
506         pwm_add_table(qi_lb60_pwm_lookup, ARRAY_SIZE(qi_lb60_pwm_lookup));
507         pinctrl_register_mappings(pin_map, ARRAY_SIZE(pin_map));
508
509         return platform_add_devices(jz_platform_devices,
510                                         ARRAY_SIZE(jz_platform_devices));
511
512 }
513
514 static int __init qi_lb60_board_setup(void)
515 {
516         printk(KERN_INFO "Qi Hardware JZ4740 QI LB60 setup\n");
517
518         if (qi_lb60_init_platform_devices())
519                 panic("Failed to initialize platform devices");
520
521         return 0;
522 }
523 arch_initcall(qi_lb60_board_setup);