Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[pandora-kernel.git] / arch / x86 / platform / mrst / mrst.c
1 /*
2  * mrst.c: Intel Moorestown platform specific setup code
3  *
4  * (C) Copyright 2008 Intel Corporation
5  * Author: Jacob Pan (jacob.jun.pan@intel.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2
10  * of the License.
11  */
12
13 #define pr_fmt(fmt) "mrst: " fmt
14
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/interrupt.h>
18 #include <linux/scatterlist.h>
19 #include <linux/sfi.h>
20 #include <linux/intel_pmic_gpio.h>
21 #include <linux/spi/spi.h>
22 #include <linux/i2c.h>
23 #include <linux/i2c/pca953x.h>
24 #include <linux/gpio_keys.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/irq.h>
28 #include <linux/module.h>
29
30 #include <asm/setup.h>
31 #include <asm/mpspec_def.h>
32 #include <asm/hw_irq.h>
33 #include <asm/apic.h>
34 #include <asm/io_apic.h>
35 #include <asm/mrst.h>
36 #include <asm/mrst-vrtc.h>
37 #include <asm/io.h>
38 #include <asm/i8259.h>
39 #include <asm/intel_scu_ipc.h>
40 #include <asm/apb_timer.h>
41 #include <asm/reboot.h>
42
43 /*
44  * the clockevent devices on Moorestown/Medfield can be APBT or LAPIC clock,
45  * cmdline option x86_mrst_timer can be used to override the configuration
46  * to prefer one or the other.
47  * at runtime, there are basically three timer configurations:
48  * 1. per cpu apbt clock only
49  * 2. per cpu always-on lapic clocks only, this is Penwell/Medfield only
50  * 3. per cpu lapic clock (C3STOP) and one apbt clock, with broadcast.
51  *
52  * by default (without cmdline option), platform code first detects cpu type
53  * to see if we are on lincroft or penwell, then set up both lapic or apbt
54  * clocks accordingly.
55  * i.e. by default, medfield uses configuration #2, moorestown uses #1.
56  * config #3 is supported but not recommended on medfield.
57  *
58  * rating and feature summary:
59  * lapic (with C3STOP) --------- 100
60  * apbt (always-on) ------------ 110
61  * lapic (always-on,ARAT) ------ 150
62  */
63
64 __cpuinitdata enum mrst_timer_options mrst_timer_options;
65
66 static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
67 static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
68 enum mrst_cpu_type __mrst_cpu_chip;
69 EXPORT_SYMBOL_GPL(__mrst_cpu_chip);
70
71 int sfi_mtimer_num;
72
73 struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
74 EXPORT_SYMBOL_GPL(sfi_mrtc_array);
75 int sfi_mrtc_num;
76
77 /* parse all the mtimer info to a static mtimer array */
78 static int __init sfi_parse_mtmr(struct sfi_table_header *table)
79 {
80         struct sfi_table_simple *sb;
81         struct sfi_timer_table_entry *pentry;
82         struct mpc_intsrc mp_irq;
83         int totallen;
84
85         sb = (struct sfi_table_simple *)table;
86         if (!sfi_mtimer_num) {
87                 sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
88                                         struct sfi_timer_table_entry);
89                 pentry = (struct sfi_timer_table_entry *) sb->pentry;
90                 totallen = sfi_mtimer_num * sizeof(*pentry);
91                 memcpy(sfi_mtimer_array, pentry, totallen);
92         }
93
94         pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
95         pentry = sfi_mtimer_array;
96         for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
97                 pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz,"
98                         " irq = %d\n", totallen, (u32)pentry->phys_addr,
99                         pentry->freq_hz, pentry->irq);
100                         if (!pentry->irq)
101                                 continue;
102                         mp_irq.type = MP_INTSRC;
103                         mp_irq.irqtype = mp_INT;
104 /* triggering mode edge bit 2-3, active high polarity bit 0-1 */
105                         mp_irq.irqflag = 5;
106                         mp_irq.srcbus = MP_BUS_ISA;
107                         mp_irq.srcbusirq = pentry->irq; /* IRQ */
108                         mp_irq.dstapic = MP_APIC_ALL;
109                         mp_irq.dstirq = pentry->irq;
110                         mp_save_irq(&mp_irq);
111         }
112
113         return 0;
114 }
115
116 struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
117 {
118         int i;
119         if (hint < sfi_mtimer_num) {
120                 if (!sfi_mtimer_usage[hint]) {
121                         pr_debug("hint taken for timer %d irq %d\n",\
122                                 hint, sfi_mtimer_array[hint].irq);
123                         sfi_mtimer_usage[hint] = 1;
124                         return &sfi_mtimer_array[hint];
125                 }
126         }
127         /* take the first timer available */
128         for (i = 0; i < sfi_mtimer_num;) {
129                 if (!sfi_mtimer_usage[i]) {
130                         sfi_mtimer_usage[i] = 1;
131                         return &sfi_mtimer_array[i];
132                 }
133                 i++;
134         }
135         return NULL;
136 }
137
138 void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
139 {
140         int i;
141         for (i = 0; i < sfi_mtimer_num;) {
142                 if (mtmr->irq == sfi_mtimer_array[i].irq) {
143                         sfi_mtimer_usage[i] = 0;
144                         return;
145                 }
146                 i++;
147         }
148 }
149
150 /* parse all the mrtc info to a global mrtc array */
151 int __init sfi_parse_mrtc(struct sfi_table_header *table)
152 {
153         struct sfi_table_simple *sb;
154         struct sfi_rtc_table_entry *pentry;
155         struct mpc_intsrc mp_irq;
156
157         int totallen;
158
159         sb = (struct sfi_table_simple *)table;
160         if (!sfi_mrtc_num) {
161                 sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
162                                                 struct sfi_rtc_table_entry);
163                 pentry = (struct sfi_rtc_table_entry *)sb->pentry;
164                 totallen = sfi_mrtc_num * sizeof(*pentry);
165                 memcpy(sfi_mrtc_array, pentry, totallen);
166         }
167
168         pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
169         pentry = sfi_mrtc_array;
170         for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
171                 pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
172                         totallen, (u32)pentry->phys_addr, pentry->irq);
173                 mp_irq.type = MP_INTSRC;
174                 mp_irq.irqtype = mp_INT;
175                 mp_irq.irqflag = 0xf;   /* level trigger and active low */
176                 mp_irq.srcbus = MP_BUS_ISA;
177                 mp_irq.srcbusirq = pentry->irq; /* IRQ */
178                 mp_irq.dstapic = MP_APIC_ALL;
179                 mp_irq.dstirq = pentry->irq;
180                 mp_save_irq(&mp_irq);
181         }
182         return 0;
183 }
184
185 static unsigned long __init mrst_calibrate_tsc(void)
186 {
187         unsigned long flags, fast_calibrate;
188
189         local_irq_save(flags);
190         fast_calibrate = apbt_quick_calibrate();
191         local_irq_restore(flags);
192
193         if (fast_calibrate)
194                 return fast_calibrate;
195
196         return 0;
197 }
198
199 static void __init mrst_time_init(void)
200 {
201         sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
202         switch (mrst_timer_options) {
203         case MRST_TIMER_APBT_ONLY:
204                 break;
205         case MRST_TIMER_LAPIC_APBT:
206                 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
207                 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
208                 break;
209         default:
210                 if (!boot_cpu_has(X86_FEATURE_ARAT))
211                         break;
212                 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
213                 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
214                 return;
215         }
216         /* we need at least one APB timer */
217         pre_init_apic_IRQ0();
218         apbt_time_init();
219 }
220
221 static void __cpuinit mrst_arch_setup(void)
222 {
223         if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
224                 __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
225         else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
226                 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
227         else {
228                 pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
229                         boot_cpu_data.x86, boot_cpu_data.x86_model);
230                 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
231         }
232         pr_debug("Moorestown CPU %s identified\n",
233                 (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
234                 "Lincroft" : "Penwell");
235 }
236
237 /* MID systems don't have i8042 controller */
238 static int mrst_i8042_detect(void)
239 {
240         return 0;
241 }
242
243 /* Reboot and power off are handled by the SCU on a MID device */
244 static void mrst_power_off(void)
245 {
246         intel_scu_ipc_simple_command(0xf1, 1);
247 }
248
249 static void mrst_reboot(void)
250 {
251         intel_scu_ipc_simple_command(0xf1, 0);
252 }
253
254 /*
255  * Moorestown specific x86_init function overrides and early setup
256  * calls.
257  */
258 void __init x86_mrst_early_setup(void)
259 {
260         x86_init.resources.probe_roms = x86_init_noop;
261         x86_init.resources.reserve_resources = x86_init_noop;
262
263         x86_init.timers.timer_init = mrst_time_init;
264         x86_init.timers.setup_percpu_clockev = x86_init_noop;
265
266         x86_init.irqs.pre_vector_init = x86_init_noop;
267
268         x86_init.oem.arch_setup = mrst_arch_setup;
269
270         x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
271
272         x86_platform.calibrate_tsc = mrst_calibrate_tsc;
273         x86_platform.i8042_detect = mrst_i8042_detect;
274         x86_init.timers.wallclock_init = mrst_rtc_init;
275         x86_init.pci.init = pci_mrst_init;
276         x86_init.pci.fixup_irqs = x86_init_noop;
277
278         legacy_pic = &null_legacy_pic;
279
280         /* Moorestown specific power_off/restart method */
281         pm_power_off = mrst_power_off;
282         machine_ops.emergency_restart  = mrst_reboot;
283
284         /* Avoid searching for BIOS MP tables */
285         x86_init.mpparse.find_smp_config = x86_init_noop;
286         x86_init.mpparse.get_smp_config = x86_init_uint_noop;
287         set_bit(MP_BUS_ISA, mp_bus_not_pci);
288 }
289
290 /*
291  * if user does not want to use per CPU apb timer, just give it a lower rating
292  * than local apic timer and skip the late per cpu timer init.
293  */
294 static inline int __init setup_x86_mrst_timer(char *arg)
295 {
296         if (!arg)
297                 return -EINVAL;
298
299         if (strcmp("apbt_only", arg) == 0)
300                 mrst_timer_options = MRST_TIMER_APBT_ONLY;
301         else if (strcmp("lapic_and_apbt", arg) == 0)
302                 mrst_timer_options = MRST_TIMER_LAPIC_APBT;
303         else {
304                 pr_warning("X86 MRST timer option %s not recognised"
305                            " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
306                            arg);
307                 return -EINVAL;
308         }
309         return 0;
310 }
311 __setup("x86_mrst_timer=", setup_x86_mrst_timer);
312
313 /*
314  * Parsing GPIO table first, since the DEVS table will need this table
315  * to map the pin name to the actual pin.
316  */
317 static struct sfi_gpio_table_entry *gpio_table;
318 static int gpio_num_entry;
319
320 static int __init sfi_parse_gpio(struct sfi_table_header *table)
321 {
322         struct sfi_table_simple *sb;
323         struct sfi_gpio_table_entry *pentry;
324         int num, i;
325
326         if (gpio_table)
327                 return 0;
328         sb = (struct sfi_table_simple *)table;
329         num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
330         pentry = (struct sfi_gpio_table_entry *)sb->pentry;
331
332         gpio_table = (struct sfi_gpio_table_entry *)
333                                 kmalloc(num * sizeof(*pentry), GFP_KERNEL);
334         if (!gpio_table)
335                 return -1;
336         memcpy(gpio_table, pentry, num * sizeof(*pentry));
337         gpio_num_entry = num;
338
339         pr_debug("GPIO pin info:\n");
340         for (i = 0; i < num; i++, pentry++)
341                 pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
342                 " pin = %d\n", i,
343                         pentry->controller_name,
344                         pentry->pin_name,
345                         pentry->pin_no);
346         return 0;
347 }
348
349 static int get_gpio_by_name(const char *name)
350 {
351         struct sfi_gpio_table_entry *pentry = gpio_table;
352         int i;
353
354         if (!pentry)
355                 return -1;
356         for (i = 0; i < gpio_num_entry; i++, pentry++) {
357                 if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
358                         return pentry->pin_no;
359         }
360         return -1;
361 }
362
363 /*
364  * Here defines the array of devices platform data that IAFW would export
365  * through SFI "DEVS" table, we use name and type to match the device and
366  * its platform data.
367  */
368 struct devs_id {
369         char name[SFI_NAME_LEN + 1];
370         u8 type;
371         u8 delay;
372         void *(*get_platform_data)(void *info);
373 };
374
375 /* the offset for the mapping of global gpio pin to irq */
376 #define MRST_IRQ_OFFSET 0x100
377
378 static void __init *pmic_gpio_platform_data(void *info)
379 {
380         static struct intel_pmic_gpio_platform_data pmic_gpio_pdata;
381         int gpio_base = get_gpio_by_name("pmic_gpio_base");
382
383         if (gpio_base == -1)
384                 gpio_base = 64;
385         pmic_gpio_pdata.gpio_base = gpio_base;
386         pmic_gpio_pdata.irq_base = gpio_base + MRST_IRQ_OFFSET;
387         pmic_gpio_pdata.gpiointr = 0xffffeff8;
388
389         return &pmic_gpio_pdata;
390 }
391
392 static void __init *max3111_platform_data(void *info)
393 {
394         struct spi_board_info *spi_info = info;
395         int intr = get_gpio_by_name("max3111_int");
396
397         spi_info->mode = SPI_MODE_0;
398         if (intr == -1)
399                 return NULL;
400         spi_info->irq = intr + MRST_IRQ_OFFSET;
401         return NULL;
402 }
403
404 /* we have multiple max7315 on the board ... */
405 #define MAX7315_NUM 2
406 static void __init *max7315_platform_data(void *info)
407 {
408         static struct pca953x_platform_data max7315_pdata[MAX7315_NUM];
409         static int nr;
410         struct pca953x_platform_data *max7315 = &max7315_pdata[nr];
411         struct i2c_board_info *i2c_info = info;
412         int gpio_base, intr;
413         char base_pin_name[SFI_NAME_LEN + 1];
414         char intr_pin_name[SFI_NAME_LEN + 1];
415
416         if (nr == MAX7315_NUM) {
417                 pr_err("too many max7315s, we only support %d\n",
418                                 MAX7315_NUM);
419                 return NULL;
420         }
421         /* we have several max7315 on the board, we only need load several
422          * instances of the same pca953x driver to cover them
423          */
424         strcpy(i2c_info->type, "max7315");
425         if (nr++) {
426                 sprintf(base_pin_name, "max7315_%d_base", nr);
427                 sprintf(intr_pin_name, "max7315_%d_int", nr);
428         } else {
429                 strcpy(base_pin_name, "max7315_base");
430                 strcpy(intr_pin_name, "max7315_int");
431         }
432
433         gpio_base = get_gpio_by_name(base_pin_name);
434         intr = get_gpio_by_name(intr_pin_name);
435
436         if (gpio_base == -1)
437                 return NULL;
438         max7315->gpio_base = gpio_base;
439         if (intr != -1) {
440                 i2c_info->irq = intr + MRST_IRQ_OFFSET;
441                 max7315->irq_base = gpio_base + MRST_IRQ_OFFSET;
442         } else {
443                 i2c_info->irq = -1;
444                 max7315->irq_base = -1;
445         }
446         return max7315;
447 }
448
449 static void __init *emc1403_platform_data(void *info)
450 {
451         static short intr2nd_pdata;
452         struct i2c_board_info *i2c_info = info;
453         int intr = get_gpio_by_name("thermal_int");
454         int intr2nd = get_gpio_by_name("thermal_alert");
455
456         if (intr == -1 || intr2nd == -1)
457                 return NULL;
458
459         i2c_info->irq = intr + MRST_IRQ_OFFSET;
460         intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
461
462         return &intr2nd_pdata;
463 }
464
465 static void __init *lis331dl_platform_data(void *info)
466 {
467         static short intr2nd_pdata;
468         struct i2c_board_info *i2c_info = info;
469         int intr = get_gpio_by_name("accel_int");
470         int intr2nd = get_gpio_by_name("accel_2");
471
472         if (intr == -1 || intr2nd == -1)
473                 return NULL;
474
475         i2c_info->irq = intr + MRST_IRQ_OFFSET;
476         intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
477
478         return &intr2nd_pdata;
479 }
480
481 static void __init *no_platform_data(void *info)
482 {
483         return NULL;
484 }
485
486 static const struct devs_id __initconst device_ids[] = {
487         {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
488         {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
489         {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
490         {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
491         {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
492         {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
493         {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
494         {"msic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
495         {},
496 };
497
498 #define MAX_IPCDEVS     24
499 static struct platform_device *ipc_devs[MAX_IPCDEVS];
500 static int ipc_next_dev;
501
502 #define MAX_SCU_SPI     24
503 static struct spi_board_info *spi_devs[MAX_SCU_SPI];
504 static int spi_next_dev;
505
506 #define MAX_SCU_I2C     24
507 static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
508 static int i2c_bus[MAX_SCU_I2C];
509 static int i2c_next_dev;
510
511 static void __init intel_scu_device_register(struct platform_device *pdev)
512 {
513         if(ipc_next_dev == MAX_IPCDEVS)
514                 pr_err("too many SCU IPC devices");
515         else
516                 ipc_devs[ipc_next_dev++] = pdev;
517 }
518
519 static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
520 {
521         struct spi_board_info *new_dev;
522
523         if (spi_next_dev == MAX_SCU_SPI) {
524                 pr_err("too many SCU SPI devices");
525                 return;
526         }
527
528         new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
529         if (!new_dev) {
530                 pr_err("failed to alloc mem for delayed spi dev %s\n",
531                         sdev->modalias);
532                 return;
533         }
534         memcpy(new_dev, sdev, sizeof(*sdev));
535
536         spi_devs[spi_next_dev++] = new_dev;
537 }
538
539 static void __init intel_scu_i2c_device_register(int bus,
540                                                 struct i2c_board_info *idev)
541 {
542         struct i2c_board_info *new_dev;
543
544         if (i2c_next_dev == MAX_SCU_I2C) {
545                 pr_err("too many SCU I2C devices");
546                 return;
547         }
548
549         new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
550         if (!new_dev) {
551                 pr_err("failed to alloc mem for delayed i2c dev %s\n",
552                         idev->type);
553                 return;
554         }
555         memcpy(new_dev, idev, sizeof(*idev));
556
557         i2c_bus[i2c_next_dev] = bus;
558         i2c_devs[i2c_next_dev++] = new_dev;
559 }
560
561 /* Called by IPC driver */
562 void intel_scu_devices_create(void)
563 {
564         int i;
565
566         for (i = 0; i < ipc_next_dev; i++)
567                 platform_device_add(ipc_devs[i]);
568
569         for (i = 0; i < spi_next_dev; i++)
570                 spi_register_board_info(spi_devs[i], 1);
571
572         for (i = 0; i < i2c_next_dev; i++) {
573                 struct i2c_adapter *adapter;
574                 struct i2c_client *client;
575
576                 adapter = i2c_get_adapter(i2c_bus[i]);
577                 if (adapter) {
578                         client = i2c_new_device(adapter, i2c_devs[i]);
579                         if (!client)
580                                 pr_err("can't create i2c device %s\n",
581                                         i2c_devs[i]->type);
582                 } else
583                         i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
584         }
585 }
586 EXPORT_SYMBOL_GPL(intel_scu_devices_create);
587
588 /* Called by IPC driver */
589 void intel_scu_devices_destroy(void)
590 {
591         int i;
592
593         for (i = 0; i < ipc_next_dev; i++)
594                 platform_device_del(ipc_devs[i]);
595 }
596 EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
597
598 static void __init install_irq_resource(struct platform_device *pdev, int irq)
599 {
600         /* Single threaded */
601         static struct resource __initdata res = {
602                 .name = "IRQ",
603                 .flags = IORESOURCE_IRQ,
604         };
605         res.start = irq;
606         platform_device_add_resources(pdev, &res, 1);
607 }
608
609 static void __init sfi_handle_ipc_dev(struct platform_device *pdev)
610 {
611         const struct devs_id *dev = device_ids;
612         void *pdata = NULL;
613
614         while (dev->name[0]) {
615                 if (dev->type == SFI_DEV_TYPE_IPC &&
616                         !strncmp(dev->name, pdev->name, SFI_NAME_LEN)) {
617                         pdata = dev->get_platform_data(pdev);
618                         break;
619                 }
620                 dev++;
621         }
622         pdev->dev.platform_data = pdata;
623         intel_scu_device_register(pdev);
624 }
625
626 static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
627 {
628         const struct devs_id *dev = device_ids;
629         void *pdata = NULL;
630
631         while (dev->name[0]) {
632                 if (dev->type == SFI_DEV_TYPE_SPI &&
633                                 !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
634                         pdata = dev->get_platform_data(spi_info);
635                         break;
636                 }
637                 dev++;
638         }
639         spi_info->platform_data = pdata;
640         if (dev->delay)
641                 intel_scu_spi_device_register(spi_info);
642         else
643                 spi_register_board_info(spi_info, 1);
644 }
645
646 static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
647 {
648         const struct devs_id *dev = device_ids;
649         void *pdata = NULL;
650
651         while (dev->name[0]) {
652                 if (dev->type == SFI_DEV_TYPE_I2C &&
653                         !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
654                         pdata = dev->get_platform_data(i2c_info);
655                         break;
656                 }
657                 dev++;
658         }
659         i2c_info->platform_data = pdata;
660
661         if (dev->delay)
662                 intel_scu_i2c_device_register(bus, i2c_info);
663         else
664                 i2c_register_board_info(bus, i2c_info, 1);
665  }
666
667
668 static int __init sfi_parse_devs(struct sfi_table_header *table)
669 {
670         struct sfi_table_simple *sb;
671         struct sfi_device_table_entry *pentry;
672         struct spi_board_info spi_info;
673         struct i2c_board_info i2c_info;
674         struct platform_device *pdev;
675         int num, i, bus;
676         int ioapic;
677         struct io_apic_irq_attr irq_attr;
678
679         sb = (struct sfi_table_simple *)table;
680         num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
681         pentry = (struct sfi_device_table_entry *)sb->pentry;
682
683         for (i = 0; i < num; i++, pentry++) {
684                 int irq = pentry->irq;
685
686                 if (irq != (u8)0xff) { /* native RTE case */
687                         /* these SPI2 devices are not exposed to system as PCI
688                          * devices, but they have separate RTE entry in IOAPIC
689                          * so we have to enable them one by one here
690                          */
691                         ioapic = mp_find_ioapic(irq);
692                         irq_attr.ioapic = ioapic;
693                         irq_attr.ioapic_pin = irq;
694                         irq_attr.trigger = 1;
695                         irq_attr.polarity = 1;
696                         io_apic_set_pci_routing(NULL, irq, &irq_attr);
697                 } else
698                         irq = 0; /* No irq */
699
700                 switch (pentry->type) {
701                 case SFI_DEV_TYPE_IPC:
702                         /* ID as IRQ is a hack that will go away */
703                         pdev = platform_device_alloc(pentry->name, irq);
704                         if (pdev == NULL) {
705                                 pr_err("out of memory for SFI platform device '%s'.\n",
706                                                         pentry->name);
707                                 continue;
708                         }
709                         install_irq_resource(pdev, irq);
710                         pr_debug("info[%2d]: IPC bus, name = %16.16s, "
711                                 "irq = 0x%2x\n", i, pentry->name, irq);
712                         sfi_handle_ipc_dev(pdev);
713                         break;
714                 case SFI_DEV_TYPE_SPI:
715                         memset(&spi_info, 0, sizeof(spi_info));
716                         strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
717                         spi_info.irq = irq;
718                         spi_info.bus_num = pentry->host_num;
719                         spi_info.chip_select = pentry->addr;
720                         spi_info.max_speed_hz = pentry->max_freq;
721                         pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
722                                 "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
723                                 spi_info.bus_num,
724                                 spi_info.modalias,
725                                 spi_info.irq,
726                                 spi_info.max_speed_hz,
727                                 spi_info.chip_select);
728                         sfi_handle_spi_dev(&spi_info);
729                         break;
730                 case SFI_DEV_TYPE_I2C:
731                         memset(&i2c_info, 0, sizeof(i2c_info));
732                         bus = pentry->host_num;
733                         strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
734                         i2c_info.irq = irq;
735                         i2c_info.addr = pentry->addr;
736                         pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
737                                 "irq = 0x%2x, addr = 0x%x\n", i, bus,
738                                 i2c_info.type,
739                                 i2c_info.irq,
740                                 i2c_info.addr);
741                         sfi_handle_i2c_dev(bus, &i2c_info);
742                         break;
743                 case SFI_DEV_TYPE_UART:
744                 case SFI_DEV_TYPE_HSI:
745                 default:
746                         ;
747                 }
748         }
749         return 0;
750 }
751
752 static int __init mrst_platform_init(void)
753 {
754         sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
755         sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
756         return 0;
757 }
758 arch_initcall(mrst_platform_init);
759
760 /*
761  * we will search these buttons in SFI GPIO table (by name)
762  * and register them dynamically. Please add all possible
763  * buttons here, we will shrink them if no GPIO found.
764  */
765 static struct gpio_keys_button gpio_button[] = {
766         {KEY_POWER,             -1, 1, "power_btn",     EV_KEY, 0, 3000},
767         {KEY_PROG1,             -1, 1, "prog_btn1",     EV_KEY, 0, 20},
768         {KEY_PROG2,             -1, 1, "prog_btn2",     EV_KEY, 0, 20},
769         {SW_LID,                -1, 1, "lid_switch",    EV_SW,  0, 20},
770         {KEY_VOLUMEUP,          -1, 1, "vol_up",        EV_KEY, 0, 20},
771         {KEY_VOLUMEDOWN,        -1, 1, "vol_down",      EV_KEY, 0, 20},
772         {KEY_CAMERA,            -1, 1, "camera_full",   EV_KEY, 0, 20},
773         {KEY_CAMERA_FOCUS,      -1, 1, "camera_half",   EV_KEY, 0, 20},
774         {SW_KEYPAD_SLIDE,       -1, 1, "MagSw1",        EV_SW,  0, 20},
775         {SW_KEYPAD_SLIDE,       -1, 1, "MagSw2",        EV_SW,  0, 20},
776 };
777
778 static struct gpio_keys_platform_data mrst_gpio_keys = {
779         .buttons        = gpio_button,
780         .rep            = 1,
781         .nbuttons       = -1, /* will fill it after search */
782 };
783
784 static struct platform_device pb_device = {
785         .name           = "gpio-keys",
786         .id             = -1,
787         .dev            = {
788                 .platform_data  = &mrst_gpio_keys,
789         },
790 };
791
792 /*
793  * Shrink the non-existent buttons, register the gpio button
794  * device if there is some
795  */
796 static int __init pb_keys_init(void)
797 {
798         struct gpio_keys_button *gb = gpio_button;
799         int i, num, good = 0;
800
801         num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
802         for (i = 0; i < num; i++) {
803                 gb[i].gpio = get_gpio_by_name(gb[i].desc);
804                 if (gb[i].gpio == -1)
805                         continue;
806
807                 if (i != good)
808                         gb[good] = gb[i];
809                 good++;
810         }
811
812         if (good) {
813                 mrst_gpio_keys.nbuttons = good;
814                 return platform_device_register(&pb_device);
815         }
816         return 0;
817 }
818 late_initcall(pb_keys_init);