summaryrefslogtreecommitdiff
path: root/arch/arm/mach-omap2/board-omap3h1.c
blob: b3e002c4048ef1da92253f61162fa105fe02debf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/*
 * Copyright (C) 2014 Olio Devices, Inc.
 * Authors: Evan Wilson <evan@oliodevices.com>
 *			Mattis Fjallstrom <mattis@oliodevices.com>
 *
 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
 *
 * Modified from the original mach-omap/omap2/board-generic.c did by Paul
 * to support the OMAP2+ device tree boards with an unique board file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/opp.h>
#include <linux/cpu.h>
#include <linux/mpu.h>
#include <linux/spi/spi.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/nand.h>

#include <linux/usb/musb.h>
#include <linux/usb/phy.h>
#include <linux/usb/nop-usb-xceiv.h>

#include <linux/i2c/atmel_mxt_ts.h>
#include <linux/gpio.h>
#include <linux/gpio_keys.h>

#include <linux/regulator/machine.h>
#include <linux/regulator/fixed.h>
#include <linux/mfd/tps65910.h>

#include <linux/kmod.h> /* for call_usermodehelper - not used, remove? */
#include <linux/wakelock.h> /* used in interrupt, waking up.  */
#include <linux/debugfs.h>

#include <linux/led-lm3530.h>

#include <asm/mach-types.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h>
#include <asm/mach/flash.h>

#include <video/omapdss.h>
#include <video/omap-panel-data.h>
#include <linux/platform_data/mtd-nand-omap2.h>

#include "common.h"
#include "omap_device.h"
#include "gpmc.h"
#include "soc.h"
#include "mux.h"
#include "pm.h"
#include "board-flash.h"
#include "common-board-devices.h"
#include "board-omap3h1.h"

#define NAND_CS 0

#define MPUIRQ_GPIO 31
#define ON_OFF_BUTTON_GPIO 30
#define ATMEL_MXT_GPIO 105
#define USB_IRQ 124
#define USB_ON_ETK_D7 21

#if defined(CONFIG_MACH_OMAP3_H1_DVT1) || defined(CONFIG_MACH_OMAP3_H1_DVT2)
#define LCD_RESET_GPIO 94
#else 
#define LCD_RESET_GPIO 122
#endif

static struct accelerometer_wake {
  int awake;
  struct wake_lock lock;
  char lock_name[100];
  int user_pid;
} acc_wake;

static struct mtd_partition omap3h1_nand_partitions[] = {
	/* All the partition sizes are listed in terms of NAND block size */
	{
		.name		= "X-Loader",
		.offset		= 0,
		.size		= 4 * NAND_BLOCK_SIZE,
		.mask_flags	= MTD_WRITEABLE,	/* force read-only */
	},
	{
		.name		= "U-Boot",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x80000 */
		.size		= 15 * NAND_BLOCK_SIZE,
		.mask_flags	= MTD_WRITEABLE,	/* force read-only */
	},
	{
		.name		= "U-Boot Env",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x260000 */
		.size		= 1 * NAND_BLOCK_SIZE,
	},
	{
		.name		= "kernel",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x280000 */
		.size		= 40 * NAND_BLOCK_SIZE,
	},
	{
		.name		= "initramfs",
		.offset		= MTDPART_OFS_APPEND, 	/* Offset = 0xC80000 */
		.size		= 80 * NAND_BLOCK_SIZE,
	},
	{
		.name		= "ramdisk",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x1180000 */
		.size		= 40 * NAND_BLOCK_SIZE,
	},
	{
		.name		= "system",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x1680000 */
		.size		= 2000 * NAND_BLOCK_SIZE,
	},
	{
		.name		= "userdata",
		.offset		= MTDPART_OFS_APPEND,	/* Offset = 0x11180000 */
		.size		= MTDPART_SIZ_FULL,
	},
};

static struct omap_dss_device omap3h1_lcd_device = {
	.type				= OMAP_DISPLAY_TYPE_DPI,
	.name				= "olio_h1_panel",
	.driver_name 		= "ili9342_panel",
	.phy.dpi.data_lines = 18,
	.reset_gpio			= LCD_RESET_GPIO,
};

static struct omap_dss_device *omap3h1_dss_devices[] = {
		&omap3h1_lcd_device,
};

static struct omap_dss_board_info omap3h1_dss_data = {
	.num_devices 	= 1,
	.devices = omap3h1_dss_devices,
	.default_device = &omap3h1_lcd_device,
};

static struct spi_board_info omap3h1_spi_board_info[] __initdata = {
	{
		.modalias		= "ili9342-spi",
		.bus_num		= 1,
		.chip_select 	= 1,
		.max_speed_hz 	= 375000,
		.platform_data	= &omap3h1_lcd_device,
		.mode			= SPI_MODE_0,
	}
};

static int __init omap3h1_spi_init(void) {
	spi_register_board_info(omap3h1_spi_board_info,
			ARRAY_SIZE(omap3h1_spi_board_info));
	return 0;
}


/* 
 * int_config is interrupt config, INT_ENABLE register. To enable _all_ 
 * interrupts, set to 0x59. I think. --mfj
 * 
 * Level shifter seems to set power rail between two options - 1 means VDD.
 * (0 means VLogic, which I'm not sure what it is).
 */

static struct mpu_platform_data mpu_data = {
	.int_config  = 0x00,
	.level_shifter = 1,
	.orientation = {  0,  -1,  0,
					  -1,  0,  0,
					  0,  0,  1 },
};

static struct lm3530_platform_data omap3h1_backlight_platform_data = {
	.mode = LM3530_BL_MODE_MANUAL,
	//.als_input_mode = LM3530_INPUT_ALS1,
	.max_current = LM3530_FS_CURR_29mA,
	//.pwm_pol_hi = true,
	//.als_avrg_time = LM3530_ALS_AVRG_TIME_512ms,
	.brt_ramp_law = 0,
	.brt_ramp_fall = LM3530_RAMP_TIME_1s, /* LM3530_RAMP_TIME_1s, */
	.brt_ramp_rise = LM3530_RAMP_TIME_1s, /* LM3530_RAMP_TIME_1s, */
	//.als1_resistor_sel = LM3530_ALS_IMPD_13_53kOhm,
	//.als2_resistor_sel = LM3530_ALS_IMPD_Z,
	//.als_vmin = 730,	    /* mV */
	//.als_vmax = 10a20,	/* mV */
	.brt_val = 0x64,	/* initial brightness */
};



static struct mxt_platform_data mxt_data = {
    .irqflags	= IRQF_TRIGGER_FALLING,
}; 

static struct platform_device bcm20702_bluetooth_device = {
	.name = "bcm20702_bluetooth",
	.id   = -1,
 };

static struct platform_device omap3h1_dmic_codec = {
		.name = "dmic-codec",
		.id = -1,
};

/* --------------------------------------------------------------------------- */
/* Keys / buttons settings 
 */

static struct gpio_keys_button gpio_buttons[] = {
	{
        .code			= KEY_POWER, /* 26 */
		.gpio			= ON_OFF_BUTTON_GPIO,
		.desc			= "accelerometer",
		.wakeup			= 1,
	},
};

static struct gpio_keys_platform_data gpio_key_info = {
	.buttons	= gpio_buttons,
	.nbuttons	= ARRAY_SIZE(gpio_buttons),
};

static struct platform_device omap3h1_keys = {
	.name	= "gpio-keys",
	.id	= -1,
	.dev	= {
		.platform_data	= &gpio_key_info,
	},
};


/* --------------------------------------------------------------------------- */
/* USB settings 
 */

static struct omap_musb_board_data musb_board_data = {
	.interface_type		= MUSB_INTERFACE_ULPI,
	.mode   			= MUSB_OTG,
	.power	    		= 100,
};

static struct nop_usb_xceiv_platform_data nop_plat_data = {
   .type     = USB_PHY_TYPE_USB2,
   .clk_rate = 60000000, /* 60 MHz */
   .needs_vcc = 1,
   .needs_reset = 0,
};

static struct platform_device nop_phy_device = {
  .name = "nop_usb_xceiv",
  .id   = -1,
  .dev  = {
      .platform_data = &nop_plat_data,
  },
};


/* static struct usbhs_omap_platform_data usbhs_bdata __initdata = { */
/* 	.port_mode[0] = OMAP_EHCI_PORT_MODE_PHY, */
/* }; */


/* --------------------------------------------------------------------------- */
/* REGULATORS 
 */

static struct regulator_init_data omap3h1_vbat_data = {
    .constraints.always_on = true,
    .constraints.valid_modes_mask = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
    .constraints.valid_ops_mask = REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
};

static struct fixed_voltage_config omap3h1_vbat_pdata = {
	.supply_name	= "VBAT",
	.microvolts   	= 3800000,
	.init_data	    = &omap3h1_vbat_data,
	.gpio		    = -EINVAL,
};

static struct platform_device omap3h1_vbat = {
	.name	= "reg-fixed-voltage",
	.id		= -1,
	.dev = {
		.platform_data = &omap3h1_vbat_pdata,
	},
};


/* --------------------------------------------------------------------------- */

static struct regulator_init_data tps65910_dummy = {
	.supply_regulator = "VBAT",
    .constraints.always_on  = false,
    /* .constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS, */
};

/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_touch3_supply[] = {
    REGULATOR_SUPPLY("avdd", "2-004a"),
};

static struct regulator_init_data tps65910_touch3 = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 3300000,
        .max_uV            = 3300000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_STATUS 
                             | REGULATOR_CHANGE_MODE,
        .always_on         = true,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_touch3_supply),
    .consumer_supplies     = tps65910_touch3_supply,
};

/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_vibrator_supply[] = {
      REGULATOR_SUPPLY("vaux2", "drv2605"),
};

static struct regulator_init_data tps65910_vibrator = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 3300000,
        .max_uV            = 3300000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | 
                             REGULATOR_CHANGE_MODE |
                             REGULATOR_CHANGE_STATUS,
        .always_on         = true,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_vibrator_supply),
    .consumer_supplies     = tps65910_vibrator_supply,
};



/* --------------------------------------------------------------------------- */


static struct regulator_init_data tps65910_3v3 = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 3300000,
        .max_uV            = 3300000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | 
                             REGULATOR_CHANGE_MODE    | 
                             REGULATOR_CHANGE_STATUS,
        .always_on         = true,
        .apply_uV		   = true,
    },
};


/* --------------------------------------------------------------------------- */
/* This regulator must not be allowed to change status, doing so causes       
 * reboots to fail. Not sure why that is. --mfj
 */

static struct regulator_consumer_supply tps65910_1v8_supply[] = {
    REGULATOR_SUPPLY("vdds_dsi", "omapdss"),
    REGULATOR_SUPPLY("vin", "2-0038"),
};

static struct regulator_init_data tps65910_1v8 = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 1800000,
        .max_uV            = 1800000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
        .always_on         = false,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_1v8_supply),
    .consumer_supplies     = tps65910_1v8_supply,
};

/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_vpll_supply[] = {
  };

static struct regulator_init_data tps65910_vpll = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 1800000,
        .max_uV            = 1800000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY, 
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
        .always_on         = false,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_vpll_supply),
    .consumer_supplies     = tps65910_vpll_supply,
};


/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_touch_supply[] = {
    REGULATOR_SUPPLY("vdd", "2-004a" ),
};

static struct regulator_init_data tps65910_touch = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 1800000,
        .max_uV            = 1800000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
        .always_on         = false,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_touch_supply),
    .consumer_supplies     = tps65910_touch_supply,
};


/* --------------------------------------------------------------------------- */

/* This one only works if 'always_on' is set to true. Do I need to do something
 * special otherwise to turn it on? 
 */

static struct regulator_consumer_supply tps65910_accel_supply[] = {
     REGULATOR_SUPPLY("vdd", "mpu6515"),
};

static struct regulator_init_data tps65910_accel = {
	.supply_regulator = "VBAT",
    .constraints = {
        .min_uV            = 1800000,
        .max_uV            = 1800000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_STATUS,
        .always_on         = true,
        .apply_uV		   = true,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_accel_supply),
    .consumer_supplies     = tps65910_accel_supply,
};

/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_vdd1_supply[] = {
    REGULATOR_SUPPLY("vcc", "cpu0"),
};

static struct regulator_init_data tps65910_vdd1 = {
	.supply_regulator = "VBAT",
    .constraints = {
        .name              = "vdd_mpu",
        .min_uV            = 900000,
        .max_uV            = 1350000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
        .always_on         = false,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_vdd1_supply),
    .consumer_supplies     = tps65910_vdd1_supply,
};

/* --------------------------------------------------------------------------- */

static struct regulator_consumer_supply tps65910_vdd2_supply[] = {
    REGULATOR_SUPPLY("vcc", "l3_main.0"),
};

static struct regulator_init_data tps65910_vdd2 = {
	.supply_regulator = "VBAT",
    .constraints = {
        .name              = "vdd_core",
        .min_uV            = 900000,
        .max_uV            = 1200000,
        .valid_modes_mask  = REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
        .valid_ops_mask    = REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS,
        .always_on         = false,
    },
    .num_consumer_supplies = ARRAY_SIZE(tps65910_vdd2_supply),
    .consumer_supplies     = tps65910_vdd2_supply,
};

/* --------------------------------------------------------------------------- */

static struct tps65910_board omap3h1_tps65910_pdata = {
	.irq = 7 + OMAP_INTC_START,
	.en_ck32k_xtal = true,

    .tps65910_pmic_init_data[TPS65910_REG_VIO]      = &tps65910_1v8,
    .tps65910_pmic_init_data[TPS65910_REG_VDD1]     = &tps65910_vdd1,
    .tps65910_pmic_init_data[TPS65910_REG_VDD2]     = &tps65910_vdd2,
    .tps65910_pmic_init_data[TPS65910_REG_VDIG1]    = &tps65910_accel,
    .tps65910_pmic_init_data[TPS65910_REG_VDIG2]    = &tps65910_touch,
    .tps65910_pmic_init_data[TPS65910_REG_VPLL]     = &tps65910_vpll,

    .tps65910_pmic_init_data[TPS65910_REG_VMMC]     = &tps65910_touch3,
    .tps65910_pmic_init_data[TPS65910_REG_VAUX2]    = &tps65910_vibrator,
    
    /* This one is connected to BT */
    
    .tps65910_pmic_init_data[TPS65910_REG_VAUX33]   = &tps65910_3v3,

    /* not actually used */

    .tps65910_pmic_init_data[TPS65910_REG_VRTC]     = &tps65910_dummy,
    .tps65910_pmic_init_data[TPS65910_REG_VDAC]     = &tps65910_dummy,
    .tps65910_pmic_init_data[TPS65910_REG_VAUX1]    = &tps65910_dummy, /* 2v85 */
    .tps65910_pmic_init_data[TPS65910_REG_VDD3]     = &tps65910_dummy,
    
    .pm_off = true,
};


/* ====================================================================== */
/* Voltage domain settings  */


#define OMAP3_SRI2C_SLAVE_ADDR		0x12
#define OMAP3_VDD_MPU_SR_CONTROL_REG	0x00
#define OMAP3_VDD_CORE_SR_CONTROL_REG	0x01
#define OMAP3_VP_CONFIG_ERROROFFSET	0x00
#define OMAP3_VP_VSTEPMIN_VSTEPMIN	0x1
#define OMAP3_VP_VSTEPMAX_VSTEPMAX	0x04
#define OMAP3_VP_VLIMITTO_TIMEOUT_US	200


static unsigned long olio_vsel_to_uv(const u8 vsel)
{
	return (((vsel * 125) + 6000)) * 100;
}

static u8 olio_uv_to_vsel(unsigned long uv)
{
	return DIV_ROUND_UP(uv - 600000, 12500);
}


static struct omap_voltdm_pmic omap3_mpu_pmic = {
	.slew_rate		= 4000,
	.step_size		= 12500,
	.vp_erroroffset		= OMAP3_VP_CONFIG_ERROROFFSET,
	.vp_vstepmin		= OMAP3_VP_VSTEPMIN_VSTEPMIN,
	.vp_vstepmax		= OMAP3_VP_VSTEPMAX_VSTEPMAX,
	.vddmin			= 900000,
	.vddmax			= 1350000,
	.vp_timeout_us		= OMAP3_VP_VLIMITTO_TIMEOUT_US,
	.i2c_slave_addr		= OMAP3_SRI2C_SLAVE_ADDR,
	.volt_reg_addr		= OMAP3_VDD_MPU_SR_CONTROL_REG,
	.i2c_high_speed		= true,
	.vsel_to_uv		= olio_vsel_to_uv,
	.uv_to_vsel		= olio_uv_to_vsel,
};

static struct omap_voltdm_pmic omap3_core_pmic = {
	.slew_rate		= 4000,
	.step_size		= 12500,
	.vp_erroroffset		= OMAP3_VP_CONFIG_ERROROFFSET,
	.vp_vstepmin		= OMAP3_VP_VSTEPMIN_VSTEPMIN,
	.vp_vstepmax		= OMAP3_VP_VSTEPMAX_VSTEPMAX,
	.vddmin			= 900000,
	.vddmax			= 1200000,
	.vp_timeout_us		= OMAP3_VP_VLIMITTO_TIMEOUT_US,
	.i2c_slave_addr		= OMAP3_SRI2C_SLAVE_ADDR,
	.volt_reg_addr		= OMAP3_VDD_CORE_SR_CONTROL_REG,
	.i2c_high_speed		= true,
	.vsel_to_uv		= olio_vsel_to_uv,
	.uv_to_vsel		= olio_uv_to_vsel,
};

int __init omap3_voltdm_init(void)
{
	struct voltagedomain *voltdm;

	if (!cpu_is_omap34xx())
		return -ENODEV;

	voltdm = voltdm_lookup("mpu_iva");
	omap_voltage_register_pmic(voltdm, &omap3_mpu_pmic);

	voltdm = voltdm_lookup("core");
	omap_voltage_register_pmic(voltdm, &omap3_core_pmic);

	return 0;
}



/* ====================================================================== */


static struct i2c_board_info __initdata omap3h1_i2c1_board_info[] = {
         {
            I2C_BOARD_INFO("tps65910", 0x2d),
            .platform_data = &omap3h1_tps65910_pdata,
         },
#ifdef CONFIG_MACH_OMAP3_H1_DVT2
};
static struct i2c_board_info __initdata omap3h1_i2c2_board_info[] = {
#endif
         {
            I2C_BOARD_INFO("mpu6515", 0x68),
            // This is needed for the interrupt wake. IH_GPIO_BASE changed in 3.10 
            // .irq = (IH_GPIO_BASE + MPUIRQ_GPIO),
            .platform_data = &mpu_data,
         },
         { 
            /* Backlight */
            I2C_BOARD_INFO("lm3530-led", 0x38),
            .platform_data = &omap3h1_backlight_platform_data,
    	 },
         {
            I2C_BOARD_INFO("mXT224", 0x4a),
            .platform_data = &mxt_data,
         },
#ifdef CONFIG_MACH_OMAP3_H1_EVT1
};
static struct i2c_board_info __initdata omap3h1_i2c3_board_info[] = {
#endif
         {
            I2C_BOARD_INFO("bq274xx", 0x55),
         },
};

/* --------------------------------------------------------------------------- */
/* Create debugfs entry 
 * 
 * We need to have this so that our wake-up thread can tell us it's PID. 
 * That PID will then be used when waking the system up.
 */


struct dentry * pidfile;

#define SIG_OLIO_WAKE 44	// we choose 44 as our signal number (real-time signals are in the range of 33 to 64)

static ssize_t write_pid(struct file *file, 
                         const char __user *buf,
                         size_t count, 
                         loff_t *ppos)
{
	char mybuf[10];
	int pid = 0;

	/* read the value from user space */

	if(count > 10)
      return -EINVAL;
	if (copy_from_user(mybuf, buf, count) != 0) {
      /* nothing read?? */
      printk ("OLIO: write_pid: Error??\n");
      return 0;
    }

	sscanf(mybuf, "%d", &pid);
	
    printk("pid read = %d\n", pid);
    
    acc_wake.user_pid = pid;

	return count;
}

static ssize_t send_wake_signal (void) {
    int ret;
	struct siginfo info;
	struct task_struct *t;

	/* send the signal */

    printk ("Now sending signal!\n");

	memset(&info, 0, sizeof(struct siginfo));

    /* this is bit of a trickery: SI_QUEUE is normally used by sigqueue from user space,
     * and kernel space should use SI_KERNEL. But if SI_KERNEL is used the real_time data 
     * is not delivered to the user space signal handler function. 
     */

	info.si_signo = SIG_OLIO_WAKE;
	info.si_code  = SI_QUEUE;

  	/* real time signals may have 32 bits of data. */

	info.si_int = 1234;

	rcu_read_lock();

	t = pid_task(find_pid_ns(acc_wake.user_pid, &init_pid_ns), PIDTYPE_PID);	

	if(t == NULL){
        rcu_read_unlock();
		printk("no such pid\n");
		return -ENODEV;
	}

    rcu_read_unlock();

    /* send the signal */

	ret = send_sig_info(SIG_OLIO_WAKE, &info, t);

	if (ret < 0) {
		printk("error sending signal\n");
	}
    return ret;
}

static const struct file_operations h1_fops = {
    .write = write_pid,
};



/***************************************************************************
 * acc_irq_handler - handle interrupt from accelerometer
 * 
 * This routine gets called when an interrupt from the accelerometer 
 * happens. Time out is in milliseconds.
 * 
 * TODO: BREAK this all out, place it in it's own kernel driver together
 * with the configuration stuff for the accelerometer (the low power sleep 
 * mode stuff).
 */

#define OLIO_DOUBLE_TAP 500
#define OLIO_ONE_SECOND 1000
#define OLIO_DT_WAIT 3000

static unsigned long previous;
static unsigned long previous_double;

static irqreturn_t acc_irq_handler_thr(int irq, void * omap3h1_d) {
  /* struct platform_device** omap3h1_devs = (struct platform_device **) omap3h1_d; */

  int ret = 99;
  unsigned long now;

  printk (KERN_DEBUG "acc_irq_handler_thr: Entered!\n");

  now = jiffies;

  if (jiffies_to_msecs(now - previous) < OLIO_DOUBLE_TAP) {

    /* ignore double clicks that happens too soon from the previous one */
    if (jiffies_to_msecs (now - previous_double) < OLIO_DT_WAIT)
    {
      previous = 0;
      return IRQ_HANDLED;
    }

    printk (KERN_DEBUG "Double tap detected!\n");

    /* We want to wake up. Take wake lock (and send signal to user space?)
     * BT driver only takes wake lock, then lets higher levels handle 
     * everything ... but what if there's nothing there to take the lock?
     */

    send_wake_signal();    
    wake_lock_timeout (&acc_wake.lock, msecs_to_jiffies (2 * OLIO_ONE_SECOND));
    
    previous_double = now;
    previous = 0;
  } else {
    previous = now;
  }

  printk (KERN_DEBUG "acc_irq_handler_thr: Leaving! (ret = %d)\n", ret);

  return IRQ_HANDLED;
}

static irqreturn_t acc_irq_handler(int irq, void * omap3h1_d) {
  printk (KERN_DEBUG "acc_irq_handler: Entered!\n");
  
  /* nothing to do, other than wake the main thread */
  
  printk (KERN_DEBUG "acc_irq_handler: Leaving!\n");
  return IRQ_WAKE_THREAD;
}

static int __init omap3_acc_irq_init (int irq, void * dev_id) {
    irq_set_irq_wake(irq, 1);

    if (request_threaded_irq (irq, acc_irq_handler, 
                              acc_irq_handler_thr, 0x81, "acc_wake", 
                              dev_id)) {
      printk (KERN_DEBUG "Couldn't register handler for accelerometer IRQ\n");
    }

    /* Should check the return value ... but if it fails, what do we do? */
    
    return 0;
}



/***************************************************************************
 * omap3_h1_i2c_init - init the i2c buses 
 *
 */

static int __init omap3_h1_i2c_init(void)
{
    int acc_irq;

  /* In Linux 3.10 we need to request an IRQ through 
   * gpio_to_irq. This means it can't be set at compile time, and
   * must be done at runtime. In other words, here.
   */

	gpio_request_one(ATMEL_MXT_GPIO, GPIOF_IN, "atmel_mxt_ts CHG");
	gpio_request_one(MPUIRQ_GPIO, GPIOF_IN, "mpu6515 IRQ pin");

    acc_irq = gpio_to_irq(MPUIRQ_GPIO);

#ifdef CONFIG_MACH_OMAP3_H1_DVT2
	omap3h1_i2c2_board_info[2].irq = gpio_to_irq(ATMEL_MXT_GPIO);
    omap3h1_i2c2_board_info[0].irq = acc_irq;
#else
	omap3h1_i2c1_board_info[3].irq = gpio_to_irq(ATMEL_MXT_GPIO);
    omap3h1_i2c1_board_info[1].irq = acc_irq;
#endif

	gpio_request_one(USB_ON_ETK_D7, GPIOF_OUT_INIT_HIGH, "USB on");
    
    /* Register buses */

	omap_register_i2c_bus(1, 400, omap3h1_i2c1_board_info, ARRAY_SIZE(omap3h1_i2c1_board_info));
#ifdef CONFIG_MACH_OMAP3_H1_DVT2
	omap_register_i2c_bus(2, 400, omap3h1_i2c2_board_info, ARRAY_SIZE(omap3h1_i2c2_board_info));
#else
	omap_register_i2c_bus(2, 400, NULL, 0);
#endif
#ifdef CONFIG_MACH_OMAP3_H1_EVT1
	omap_register_i2c_bus(3, 400, omap3h1_i2c3_board_info, ARRAY_SIZE(omap3h1_i2c3_board_info));
#else
	omap_register_i2c_bus(3, 400, NULL, 0);
#endif
	return 0;
}

static struct platform_device *omap3h1_devices[] __initdata = {
		&omap3h1_vbat,
        &bcm20702_bluetooth_device,
        &nop_phy_device,
		&omap3h1_dmic_codec,
        &omap3h1_keys,
};

#ifdef CONFIG_OMAP_MUX
static struct omap_board_mux board_mux[] __initdata = {

#if defined(CONFIG_MACH_OMAP3_H1_DVT1) || defined(CONFIG_MACH_OMAP3_H1_DVT2)
	OMAP3_MUX(CAM_HS, OMAP_MUX_MODE4 | OMAP_PIN_OUTPUT),
#else
	OMAP3_MUX(HSUSB0_DIR, OMAP_MUX_MODE4 | OMAP_PIN_OUTPUT),
#endif
	OMAP3_MUX(DSS_PCLK, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_HSYNC, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_VSYNC, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA0, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA1, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA2, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA3, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA4, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA5, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA6, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA7, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA8, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA9, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA10, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA11, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA12, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA13, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA14, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA15, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA16, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(DSS_DATA17, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),

	OMAP3_MUX(MCSPI1_CLK, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(MCSPI1_SIMO, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),
	OMAP3_MUX(MCSPI1_SOMI, OMAP_MUX_MODE0 | OMAP_PIN_INPUT_PULLUP),
	OMAP3_MUX(MCSPI1_CS1, OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT),

	/* TOUCH_RESET */

	OMAP3_MUX(CAM_D6, OMAP_MUX_MODE4 | OMAP_PIN_INPUT_PULLUP),

#if defined(CONFIG_MACH_OMAP3_H1_DVT1) || defined(CONFIG_MACH_OMAP3_H1_DVT2)
    /* USB pin settings (mUSB) */

    OMAP3_MUX(HSUSB0_CLK,   (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA0, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA1, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA2, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA3, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA4, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA5, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA6, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DATA7, (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_DIR,   (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_NXT,   (OMAP_MUX_MODE0 | OMAP_PIN_INPUT)),
    OMAP3_MUX(HSUSB0_STP,   (OMAP_MUX_MODE0 | OMAP_PIN_OUTPUT)),
#endif /* CONFIG_MACH_OMAP3_H1_DVT */

    /* accelerometer */
     
    OMAP3_MUX(JTAG_EMU1, (OMAP_MUX_MODE4 | OMAP_PIN_INPUT 
                          | OMAP_PIN_OFF_WAKEUPENABLE)),

    /* USB on */
    
    OMAP3_MUX(ETK_D7, (OMAP_MUX_MODE4 | OMAP_PIN_OUTPUT)),

	{ .reg_offset = OMAP_MUX_TERMINATOR },
};
#endif

static int __init omap3_wake_init (void) {
  pidfile = debugfs_create_file("wake_signal_pid", 0200, NULL, NULL, &h1_fops);
  acc_wake.user_pid = 0; /* No user thread yet */

  snprintf(acc_wake.lock_name, sizeof(acc_wake.lock_name),
           "Accelerometer");
  wake_lock_init(&acc_wake.lock, WAKE_LOCK_SUSPEND,
                 acc_wake.lock_name);
  return 0;
}


static void __init omap3_h1_init(void)
{
    omap3_mux_init(board_mux, OMAP_PACKAGE_CBP);

	omap3_h1_i2c_init();
  
	omap_display_init(&omap3h1_dss_data);
	omap_serial_init();
	omap_sdrc_init(NULL, NULL);

	board_nand_init(omap3h1_nand_partitions,
                    ARRAY_SIZE(omap3h1_nand_partitions), NAND_CS,
                    NAND_BUSWIDTH_16, NULL);

	platform_add_devices(omap3h1_devices, ARRAY_SIZE(omap3h1_devices));

	omap3h1_spi_init();

	usb_bind_phy("musb-hdrc.0.auto", 0, "nop_usb_xceiv"); /* "tusb-usb-h1" */
	usb_musb_init(&musb_board_data);

    /* Set up the voltage domains */

    omap3_voltdm_init();

    /* For handling interrupts from the accelerometer */

    /* previous = jiffies; */
     
    /* Init the code that handles wake ups */

    /* omap3_wake_init(); */

    /* omap3_acc_irq_init(omap3h1_i2c2_board_info[0].irq, (void *) &omap3h1_devices); */
}

MACHINE_START(OMAP3_H1, "Olio OMAP3 H1 Board")
	.atag_offset	= 0x100,
	.reserve		= omap_reserve,
	.map_io			= omap3_map_io,
	.init_early		= omap3630_init_early,
	.init_irq		= omap3_init_irq,
	.handle_irq		= omap3_intc_handle_irq,
	.init_machine	= omap3_h1_init,
	.init_late		= omap3630_init_late,
	.init_time		= omap3_secure_sync32k_timer_init,
	/* .dt_compat		= omap3_h1_boards_compat, */
	.restart		= omap3xxx_restart,
MACHINE_END