summaryrefslogtreecommitdiffstats
path: root/drivers/platform/chrome/chromeos_laptop.c
blob: 7abbb6167766eec972cbaee3f22e38ba8646ce9a (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
// SPDX-License-Identifier: GPL-2.0+
// Driver to instantiate Chromebook i2c/smbus devices.
//
// Copyright (C) 2012 Google, Inc.
// Author: Benson Leung <bleung@chromium.org>

#define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt

#include <linux/acpi.h>
#include <linux/dmi.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/property.h>

#define ATMEL_TP_I2C_ADDR	0x4b
#define ATMEL_TP_I2C_BL_ADDR	0x25
#define ATMEL_TS_I2C_ADDR	0x4a
#define ATMEL_TS_I2C_BL_ADDR	0x26
#define CYAPA_TP_I2C_ADDR	0x67
#define ELAN_TP_I2C_ADDR	0x15
#define ISL_ALS_I2C_ADDR	0x44
#define TAOS_ALS_I2C_ADDR	0x29

static const char *i2c_adapter_names[] = {
	"SMBus I801 adapter",
	"i915 gmbus vga",
	"i915 gmbus panel",
	"Synopsys DesignWare I2C adapter",
};

/* Keep this enum consistent with i2c_adapter_names */
enum i2c_adapter_type {
	I2C_ADAPTER_SMBUS = 0,
	I2C_ADAPTER_VGADDC,
	I2C_ADAPTER_PANEL,
	I2C_ADAPTER_DESIGNWARE,
};

struct i2c_peripheral {
	struct i2c_board_info board_info;
	unsigned short alt_addr;

	const char *dmi_name;
	unsigned long irqflags;
	struct resource irq_resource;

	enum i2c_adapter_type type;
	u32 pci_devid;

	struct i2c_client *client;
};

struct acpi_peripheral {
	char hid[ACPI_ID_LEN];
	const struct property_entry *properties;
};

struct chromeos_laptop {
	/*
	 * Note that we can't mark this pointer as const because
	 * i2c_new_probed_device() changes passed in I2C board info, so.
	 */
	struct i2c_peripheral *i2c_peripherals;
	unsigned int num_i2c_peripherals;

	const struct acpi_peripheral *acpi_peripherals;
	unsigned int num_acpi_peripherals;
};

static const struct chromeos_laptop *cros_laptop;

static struct i2c_client *
chromes_laptop_instantiate_i2c_device(struct i2c_adapter *adapter,
				      struct i2c_board_info *info,
				      unsigned short alt_addr)
{
	const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
	struct i2c_client *client;

	/*
	 * Add the i2c device. If we can't detect it at the primary
	 * address we scan secondary addresses. In any case the client
	 * structure gets assigned primary address.
	 */
	client = i2c_new_probed_device(adapter, info, addr_list, NULL);
	if (!client && alt_addr) {
		struct i2c_board_info dummy_info = {
			I2C_BOARD_INFO("dummy", info->addr),
		};
		const unsigned short alt_addr_list[] = {
			alt_addr, I2C_CLIENT_END
		};
		struct i2c_client *dummy;

		dummy = i2c_new_probed_device(adapter, &dummy_info,
					      alt_addr_list, NULL);
		if (dummy) {
			pr_debug("%d-%02x is probed at %02x\n",
				 adapter->nr, info->addr, dummy->addr);
			i2c_unregister_device(dummy);
			client = i2c_new_device(adapter, info);
		}
	}

	if (!client)
		pr_debug("failed to register device %d-%02x\n",
			 adapter->nr, info->addr);
	else
		pr_debug("added i2c device %d-%02x\n",
			 adapter->nr, info->addr);

	return client;
}

static bool chromeos_laptop_match_adapter_devid(struct device *dev, u32 devid)
{
	struct pci_dev *pdev;

	if (!dev_is_pci(dev))
		return false;

	pdev = to_pci_dev(dev);
	return devid == pci_dev_id(pdev);
}

static void chromeos_laptop_check_adapter(struct i2c_adapter *adapter)
{
	struct i2c_peripheral *i2c_dev;
	int i;

	for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
		i2c_dev = &cros_laptop->i2c_peripherals[i];

		/* Skip devices already created */
		if (i2c_dev->client)
			continue;

		if (strncmp(adapter->name, i2c_adapter_names[i2c_dev->type],
			    strlen(i2c_adapter_names[i2c_dev->type])))
			continue;

		if (i2c_dev->pci_devid &&
		    !chromeos_laptop_match_adapter_devid(adapter->dev.parent,
							 i2c_dev->pci_devid)) {
			continue;
		}

		i2c_dev->client =
			chromes_laptop_instantiate_i2c_device(adapter,
							&i2c_dev->board_info,
							i2c_dev->alt_addr);
	}
}

static bool chromeos_laptop_adjust_client(struct i2c_client *client)
{
	const struct acpi_peripheral *acpi_dev;
	struct acpi_device_id acpi_ids[2] = { };
	int i;
	int error;

	if (!has_acpi_companion(&client->dev))
		return false;

	for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
		acpi_dev = &cros_laptop->acpi_peripherals[i];

		memcpy(acpi_ids[0].id, acpi_dev->hid, ACPI_ID_LEN);

		if (acpi_match_device(acpi_ids, &client->dev)) {
			error = device_add_properties(&client->dev,
						      acpi_dev->properties);
			if (error) {
				dev_err(&client->dev,
					"failed to add properties: %d\n",
					error);
				break;
			}

			return true;
		}
	}

	return false;
}

static void chromeos_laptop_detach_i2c_client(struct i2c_client *client)
{
	struct i2c_peripheral *i2c_dev;
	int i;

	for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
		i2c_dev = &cros_laptop->i2c_peripherals[i];

		if (i2c_dev->client == client)
			i2c_dev->client = NULL;
	}
}

static int chromeos_laptop_i2c_notifier_call(struct notifier_block *nb,
					     unsigned long action, void *data)
{
	struct device *dev = data;

	switch (action) {
	case BUS_NOTIFY_ADD_DEVICE:
		if (dev->type == &i2c_adapter_type)
			chromeos_laptop_check_adapter(to_i2c_adapter(dev));
		else if (dev->type == &i2c_client_type)
			chromeos_laptop_adjust_client(to_i2c_client(dev));
		break;

	case BUS_NOTIFY_REMOVED_DEVICE:
		if (dev->type == &i2c_client_type)
			chromeos_laptop_detach_i2c_client(to_i2c_client(dev));
		break;
	}

	return 0;
}

static struct notifier_block chromeos_laptop_i2c_notifier = {
	.notifier_call = chromeos_laptop_i2c_notifier_call,
};

#define DECLARE_CROS_LAPTOP(_name)					\
static const struct chromeos_laptop _name __initconst = {		\
	.i2c_peripherals	= _name##_peripherals,			\
	.num_i2c_peripherals	= ARRAY_SIZE(_name##_peripherals),	\
}

#define DECLARE_ACPI_CROS_LAPTOP(_name)					\
static const struct chromeos_laptop _name __initconst = {		\
	.acpi_peripherals	= _name##_peripherals,			\
	.num_acpi_peripherals	= ARRAY_SIZE(_name##_peripherals),	\
}

static struct i2c_peripheral samsung_series_5_550_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_SMBUS,
	},
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
		},
		.dmi_name	= "lightsensor",
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(samsung_series_5_550);

static struct i2c_peripheral samsung_series_5_peripherals[] __initdata = {
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
		},
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(samsung_series_5);

static const int chromebook_pixel_tp_keys[] __initconst = {
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	BTN_LEFT
};

static const struct property_entry
chromebook_pixel_trackpad_props[] __initconst = {
	PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
	PROPERTY_ENTRY_U32_ARRAY("linux,gpio-keymap", chromebook_pixel_tp_keys),
	{ }
};

static const struct property_entry
chromebook_atmel_touchscreen_props[] __initconst = {
	PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
	{ }
};

static struct i2c_peripheral chromebook_pixel_peripherals[] __initdata = {
	/* Touch Screen. */
	{
		.board_info	= {
			I2C_BOARD_INFO("atmel_mxt_ts",
					ATMEL_TS_I2C_ADDR),
			.properties	=
				chromebook_atmel_touchscreen_props,
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "touchscreen",
		.irqflags	= IRQF_TRIGGER_FALLING,
		.type		= I2C_ADAPTER_PANEL,
		.alt_addr	= ATMEL_TS_I2C_BL_ADDR,
	},
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("atmel_mxt_tp",
					ATMEL_TP_I2C_ADDR),
			.properties	=
				chromebook_pixel_trackpad_props,
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.irqflags	= IRQF_TRIGGER_FALLING,
		.type		= I2C_ADAPTER_VGADDC,
		.alt_addr	= ATMEL_TP_I2C_BL_ADDR,
	},
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
		},
		.dmi_name	= "lightsensor",
		.type		= I2C_ADAPTER_PANEL,
	},
};
DECLARE_CROS_LAPTOP(chromebook_pixel);

static struct i2c_peripheral hp_chromebook_14_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
	},
};
DECLARE_CROS_LAPTOP(hp_chromebook_14);

static struct i2c_peripheral dell_chromebook_11_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
	},
	/* Elan Touchpad option. */
	{
		.board_info	= {
			I2C_BOARD_INFO("elan_i2c", ELAN_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
	},
};
DECLARE_CROS_LAPTOP(dell_chromebook_11);

static struct i2c_peripheral toshiba_cb35_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
	},
};
DECLARE_CROS_LAPTOP(toshiba_cb35);

static struct i2c_peripheral acer_c7_chromebook_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(acer_c7_chromebook);

static struct i2c_peripheral acer_ac700_peripherals[] __initdata = {
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
		},
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(acer_ac700);

static struct i2c_peripheral acer_c720_peripherals[] __initdata = {
	/* Touchscreen. */
	{
		.board_info	= {
			I2C_BOARD_INFO("atmel_mxt_ts",
					ATMEL_TS_I2C_ADDR),
			.properties	=
				chromebook_atmel_touchscreen_props,
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "touchscreen",
		.irqflags	= IRQF_TRIGGER_FALLING,
		.type		= I2C_ADAPTER_DESIGNWARE,
		.pci_devid	= PCI_DEVID(0, PCI_DEVFN(0x15, 0x2)),
		.alt_addr	= ATMEL_TS_I2C_BL_ADDR,
	},
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
		.pci_devid	= PCI_DEVID(0, PCI_DEVFN(0x15, 0x1)),
	},
	/* Elan Touchpad option. */
	{
		.board_info	= {
			I2C_BOARD_INFO("elan_i2c", ELAN_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_DESIGNWARE,
		.pci_devid	= PCI_DEVID(0, PCI_DEVFN(0x15, 0x1)),
	},
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
		},
		.dmi_name	= "lightsensor",
		.type		= I2C_ADAPTER_DESIGNWARE,
		.pci_devid	= PCI_DEVID(0, PCI_DEVFN(0x15, 0x2)),
	},
};
DECLARE_CROS_LAPTOP(acer_c720);

static struct i2c_peripheral
hp_pavilion_14_chromebook_peripherals[] __initdata = {
	/* Touchpad. */
	{
		.board_info	= {
			I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
			.flags		= I2C_CLIENT_WAKE,
		},
		.dmi_name	= "trackpad",
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(hp_pavilion_14_chromebook);

static struct i2c_peripheral cr48_peripherals[] __initdata = {
	/* Light Sensor. */
	{
		.board_info	= {
			I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
		},
		.type		= I2C_ADAPTER_SMBUS,
	},
};
DECLARE_CROS_LAPTOP(cr48);

static const u32 samus_touchpad_buttons[] __initconst = {
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	BTN_LEFT
};

static const struct property_entry samus_trackpad_props[] __initconst = {
	PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
	PROPERTY_ENTRY_U32_ARRAY("linux,gpio-keymap", samus_touchpad_buttons),
	{ }
};

static struct acpi_peripheral samus_peripherals[] __initdata = {
	/* Touchpad */
	{
		.hid		= "ATML0000",
		.properties	= samus_trackpad_props,
	},
	/* Touchsceen */
	{
		.hid		= "ATML0001",
		.properties	= chromebook_atmel_touchscreen_props,
	},
};
DECLARE_ACPI_CROS_LAPTOP(samus);

static struct acpi_peripheral generic_atmel_peripherals[] __initdata = {
	/* Touchpad */
	{
		.hid		= "ATML0000",
		.properties	= chromebook_pixel_trackpad_props,
	},
	/* Touchsceen */
	{
		.hid		= "ATML0001",
		.properties	= chromebook_atmel_touchscreen_props,
	},
};
DECLARE_ACPI_CROS_LAPTOP(generic_atmel);

static const struct dmi_system_id chromeos_laptop_dmi_table[] __initconst = {
	{
		.ident = "Samsung Series 5 550",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
		},
		.driver_data = (void *)&samsung_series_5_550,
	},
	{
		.ident = "Samsung Series 5",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
		},
		.driver_data = (void *)&samsung_series_5,
	},
	{
		.ident = "Chromebook Pixel",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
		},
		.driver_data = (void *)&chromebook_pixel,
	},
	{
		.ident = "Wolf",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Wolf"),
		},
		.driver_data = (void *)&dell_chromebook_11,
	},
	{
		.ident = "HP Chromebook 14",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Falco"),
		},
		.driver_data = (void *)&hp_chromebook_14,
	},
	{
		.ident = "Toshiba CB35",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Leon"),
		},
		.driver_data = (void *)&toshiba_cb35,
	},
	{
		.ident = "Acer C7 Chromebook",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
		},
		.driver_data = (void *)&acer_c7_chromebook,
	},
	{
		.ident = "Acer AC700",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
		},
		.driver_data = (void *)&acer_ac700,
	},
	{
		.ident = "Acer C720",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
		},
		.driver_data = (void *)&acer_c720,
	},
	{
		.ident = "HP Pavilion 14 Chromebook",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
		},
		.driver_data = (void *)&hp_pavilion_14_chromebook,
	},
	{
		.ident = "Cr-48",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
		},
		.driver_data = (void *)&cr48,
	},
	/* Devices with peripherals incompletely described in ACPI */
	{
		.ident = "Chromebook Pro",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Caroline"),
		},
		.driver_data = (void *)&samus,
	},
	{
		.ident = "Google Pixel 2 (2015)",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
		},
		.driver_data = (void *)&samus,
	},
	{
		.ident = "Samsung Chromebook 3",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Celes"),
		},
		.driver_data = (void *)&samus,
	},
	{
		/*
		 * Other Chromebooks with Atmel touch controllers:
		 * - Winky (touchpad)
		 * - Clapper, Expresso, Rambi, Glimmer (touchscreen)
		 */
		.ident = "Other Chromebook",
		.matches = {
			/*
			 * This will match all Google devices, not only devices
			 * with Atmel, but we will validate that the device
			 * actually has matching peripherals.
			 */
			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
		},
		.driver_data = (void *)&generic_atmel,
	},
	{ }
};
MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);

static int __init chromeos_laptop_scan_peripherals(struct device *dev, void *data)
{
	int error;

	if (dev->type == &i2c_adapter_type) {
		chromeos_laptop_check_adapter(to_i2c_adapter(dev));
	} else if (dev->type == &i2c_client_type) {
		if (chromeos_laptop_adjust_client(to_i2c_client(dev))) {
			/*
			 * Now that we have needed properties re-trigger
			 * driver probe in case driver was initialized
			 * earlier and probe failed.
			 */
			error = device_attach(dev);
			if (error < 0)
				dev_warn(dev,
					 "%s: device_attach() failed: %d\n",
					 __func__, error);
		}
	}

	return 0;
}

static int __init chromeos_laptop_get_irq_from_dmi(const char *dmi_name)
{
	const struct dmi_device *dmi_dev;
	const struct dmi_dev_onboard *dev_data;

	dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, dmi_name, NULL);
	if (!dmi_dev) {
		pr_err("failed to find DMI device '%s'\n", dmi_name);
		return -ENOENT;
	}

	dev_data = dmi_dev->device_data;
	if (!dev_data) {
		pr_err("failed to get data from DMI for '%s'\n", dmi_name);
		return -EINVAL;
	}

	return dev_data->instance;
}

static int __init chromeos_laptop_setup_irq(struct i2c_peripheral *i2c_dev)
{
	int irq;

	if (i2c_dev->dmi_name) {
		irq = chromeos_laptop_get_irq_from_dmi(i2c_dev->dmi_name);
		if (irq < 0)
			return irq;

		i2c_dev->irq_resource  = (struct resource)
			DEFINE_RES_NAMED(irq, 1, NULL,
					 IORESOURCE_IRQ | i2c_dev->irqflags);
		i2c_dev->board_info.resources = &i2c_dev->irq_resource;
		i2c_dev->board_info.num_resources = 1;
	}

	return 0;
}

static int __init
chromeos_laptop_prepare_i2c_peripherals(struct chromeos_laptop *cros_laptop,
					const struct chromeos_laptop *src)
{
	struct i2c_peripheral *i2c_dev;
	struct i2c_board_info *info;
	int i;
	int error;

	if (!src->num_i2c_peripherals)
		return 0;

	cros_laptop->i2c_peripherals = kmemdup(src->i2c_peripherals,
					       src->num_i2c_peripherals *
						sizeof(*src->i2c_peripherals),
					       GFP_KERNEL);
	if (!cros_laptop->i2c_peripherals)
		return -ENOMEM;

	cros_laptop->num_i2c_peripherals = src->num_i2c_peripherals;

	for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
		i2c_dev = &cros_laptop->i2c_peripherals[i];
		info = &i2c_dev->board_info;

		error = chromeos_laptop_setup_irq(i2c_dev);
		if (error)
			goto err_out;

		/* We need to deep-copy properties */
		if (info->properties) {
			info->properties =
				property_entries_dup(info->properties);
			if (IS_ERR(info->properties)) {
				error = PTR_ERR(info->properties);
				goto err_out;
			}
		}
	}

	return 0;

err_out:
	while (--i >= 0) {
		i2c_dev = &cros_laptop->i2c_peripherals[i];
		info = &i2c_dev->board_info;
		if (info->properties)
			property_entries_free(info->properties);
	}
	kfree(cros_laptop->i2c_peripherals);
	return error;
}

static int __init
chromeos_laptop_prepare_acpi_peripherals(struct chromeos_laptop *cros_laptop,
					const struct chromeos_laptop *src)
{
	struct acpi_peripheral *acpi_peripherals;
	struct acpi_peripheral *acpi_dev;
	const struct acpi_peripheral *src_dev;
	int n_peripherals = 0;
	int i;
	int error;

	for (i = 0; i < src->num_acpi_peripherals; i++) {
		if (acpi_dev_present(src->acpi_peripherals[i].hid, NULL, -1))
			n_peripherals++;
	}

	if (!n_peripherals)
		return 0;

	acpi_peripherals = kcalloc(n_peripherals,
				   sizeof(*src->acpi_peripherals),
				   GFP_KERNEL);
	if (!acpi_peripherals)
		return -ENOMEM;

	acpi_dev = acpi_peripherals;
	for (i = 0; i < src->num_acpi_peripherals; i++) {
		src_dev = &src->acpi_peripherals[i];
		if (!acpi_dev_present(src_dev->hid, NULL, -1))
			continue;

		*acpi_dev = *src_dev;

		/* We need to deep-copy properties */
		if (src_dev->properties) {
			acpi_dev->properties =
				property_entries_dup(src_dev->properties);
			if (IS_ERR(acpi_dev->properties)) {
				error = PTR_ERR(acpi_dev->properties);
				goto err_out;
			}
		}

		acpi_dev++;
	}

	cros_laptop->acpi_peripherals = acpi_peripherals;
	cros_laptop->num_acpi_peripherals = n_peripherals;

	return 0;

err_out:
	while (--i >= 0) {
		acpi_dev = &acpi_peripherals[i];
		if (acpi_dev->properties)
			property_entries_free(acpi_dev->properties);
	}

	kfree(acpi_peripherals);
	return error;
}

static void chromeos_laptop_destroy(const struct chromeos_laptop *cros_laptop)
{
	const struct acpi_peripheral *acpi_dev;
	struct i2c_peripheral *i2c_dev;
	struct i2c_board_info *info;
	int i;

	for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
		i2c_dev = &cros_laptop->i2c_peripherals[i];
		info = &i2c_dev->board_info;

		if (i2c_dev->client)
			i2c_unregister_device(i2c_dev->client);

		if (info->properties)
			property_entries_free(info->properties);
	}

	for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
		acpi_dev = &cros_laptop->acpi_peripherals[i];

		if (acpi_dev->properties)
			property_entries_free(acpi_dev->properties);
	}

	kfree(cros_laptop->i2c_peripherals);
	kfree(cros_laptop->acpi_peripherals);
	kfree(cros_laptop);
}

static struct chromeos_laptop * __init
chromeos_laptop_prepare(const struct chromeos_laptop *src)
{
	struct chromeos_laptop *cros_laptop;
	int error;

	cros_laptop = kzalloc(sizeof(*cros_laptop), GFP_KERNEL);
	if (!cros_laptop)
		return ERR_PTR(-ENOMEM);

	error = chromeos_laptop_prepare_i2c_peripherals(cros_laptop, src);
	if (!error)
		error = chromeos_laptop_prepare_acpi_peripherals(cros_laptop,
								 src);

	if (error) {
		chromeos_laptop_destroy(cros_laptop);
		return ERR_PTR(error);
	}

	return cros_laptop;
}

static int __init chromeos_laptop_init(void)
{
	const struct dmi_system_id *dmi_id;
	int error;

	dmi_id = dmi_first_match(chromeos_laptop_dmi_table);
	if (!dmi_id) {
		pr_debug("unsupported system\n");
		return -ENODEV;
	}

	pr_debug("DMI Matched %s\n", dmi_id->ident);

	cros_laptop = chromeos_laptop_prepare((void *)dmi_id->driver_data);
	if (IS_ERR(cros_laptop))
		return PTR_ERR(cros_laptop);

	if (!cros_laptop->num_i2c_peripherals &&
	    !cros_laptop->num_acpi_peripherals) {
		pr_debug("no relevant devices detected\n");
		error = -ENODEV;
		goto err_destroy_cros_laptop;
	}

	error = bus_register_notifier(&i2c_bus_type,
				      &chromeos_laptop_i2c_notifier);
	if (error) {
		pr_err("failed to register i2c bus notifier: %d\n",
		       error);
		goto err_destroy_cros_laptop;
	}

	/*
	 * Scan adapters that have been registered and clients that have
	 * been created before we installed the notifier to make sure
	 * we do not miss any devices.
	 */
	i2c_for_each_dev(NULL, chromeos_laptop_scan_peripherals);

	return 0;

err_destroy_cros_laptop:
	chromeos_laptop_destroy(cros_laptop);
	return error;
}

static void __exit chromeos_laptop_exit(void)
{
	bus_unregister_notifier(&i2c_bus_type, &chromeos_laptop_i2c_notifier);
	chromeos_laptop_destroy(cros_laptop);
}

module_init(chromeos_laptop_init);
module_exit(chromeos_laptop_exit);

MODULE_DESCRIPTION("Chrome OS Laptop driver");
MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
MODULE_LICENSE("GPL");