Linux 6.12-rc1
[linux-2.6-block.git] / drivers / platform / chrome / chromeos_laptop.c
CommitLineData
203e0bae
DT
1// SPDX-License-Identifier: GPL-2.0+
2// Driver to instantiate Chromebook i2c/smbus devices.
3//
4// Copyright (C) 2012 Google, Inc.
5// Author: Benson Leung <bleung@chromium.org>
d1381f45 6
4f27f677
DT
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
5020cd29 9#include <linux/acpi.h>
d1381f45
BL
10#include <linux/dmi.h>
11#include <linux/i2c.h>
2ef39204
BL
12#include <linux/input.h>
13#include <linux/interrupt.h>
e6215eea 14#include <linux/ioport.h>
d1381f45 15#include <linux/module.h>
8d88cb03 16#include <linux/pci.h>
9ad36924 17#include <linux/platform_device.h>
f00c1d19 18#include <linux/property.h>
d1381f45 19
8e1ad4c4
BL
20#define ATMEL_TP_I2C_ADDR 0x4b
21#define ATMEL_TP_I2C_BL_ADDR 0x25
33a84f8a
YS
22#define ATMEL_TS_I2C_ADDR 0x4a
23#define ATMEL_TS_I2C_BL_ADDR 0x26
d1381f45 24#define CYAPA_TP_I2C_ADDR 0x67
9bd9a90b 25#define ELAN_TP_I2C_ADDR 0x15
d1381f45 26#define ISL_ALS_I2C_ADDR 0x44
aabf3f44 27#define TAOS_ALS_I2C_ADDR 0x29
d1381f45 28
6d3c1afe 29static const char *i2c_adapter_names[] = {
d1381f45 30 "SMBus I801 adapter",
741bf0c7
BL
31 "i915 gmbus vga",
32 "i915 gmbus panel",
ebaf31c4 33 "Synopsys DesignWare I2C adapter",
d1381f45
BL
34};
35
36/* Keep this enum consistent with i2c_adapter_names */
37enum i2c_adapter_type {
38 I2C_ADAPTER_SMBUS = 0,
741bf0c7
BL
39 I2C_ADAPTER_VGADDC,
40 I2C_ADAPTER_PANEL,
8d88cb03 41 I2C_ADAPTER_DESIGNWARE,
d1381f45
BL
42};
43
ec199dd5 44struct i2c_peripheral {
28cd38f1
DT
45 struct i2c_board_info board_info;
46 unsigned short alt_addr;
e6215eea 47
28cd38f1 48 const char *dmi_name;
e6215eea
DT
49 unsigned long irqflags;
50 struct resource irq_resource;
51
ec199dd5 52 enum i2c_adapter_type type;
8d88cb03 53 u32 pci_devid;
28cd38f1 54
2c02f659
HK
55 const struct property_entry *properties;
56
28cd38f1 57 struct i2c_client *client;
ec199dd5
AD
58};
59
5020cd29
DT
60struct acpi_peripheral {
61 char hid[ACPI_ID_LEN];
2c02f659
HK
62 struct software_node swnode;
63 struct i2c_client *client;
5020cd29
DT
64};
65
ec199dd5 66struct chromeos_laptop {
c0bb0608
DT
67 /*
68 * Note that we can't mark this pointer as const because
c82ebf1b 69 * i2c_new_scanned_device() changes passed in I2C board info, so.
c0bb0608
DT
70 */
71 struct i2c_peripheral *i2c_peripherals;
72 unsigned int num_i2c_peripherals;
5020cd29 73
2c02f659 74 struct acpi_peripheral *acpi_peripherals;
5020cd29 75 unsigned int num_acpi_peripherals;
ec199dd5
AD
76};
77
c0bb0608 78static const struct chromeos_laptop *cros_laptop;
9ad36924 79
28cd38f1 80static struct i2c_client *
8d88cb03 81chromes_laptop_instantiate_i2c_device(struct i2c_adapter *adapter,
28cd38f1
DT
82 struct i2c_board_info *info,
83 unsigned short alt_addr)
d1381f45 84{
96cba9b0 85 const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
8d88cb03 86 struct i2c_client *client;
d1381f45 87
96cba9b0
DT
88 /*
89 * Add the i2c device. If we can't detect it at the primary
90 * address we scan secondary addresses. In any case the client
91 * structure gets assigned primary address.
92 */
c82ebf1b
WS
93 client = i2c_new_scanned_device(adapter, info, addr_list, NULL);
94 if (IS_ERR(client) && alt_addr) {
96cba9b0
DT
95 struct i2c_board_info dummy_info = {
96 I2C_BOARD_INFO("dummy", info->addr),
97 };
28cd38f1
DT
98 const unsigned short alt_addr_list[] = {
99 alt_addr, I2C_CLIENT_END
100 };
96cba9b0
DT
101 struct i2c_client *dummy;
102
c82ebf1b
WS
103 dummy = i2c_new_scanned_device(adapter, &dummy_info,
104 alt_addr_list, NULL);
105 if (!IS_ERR(dummy)) {
4f27f677 106 pr_debug("%d-%02x is probed at %02x\n",
8d88cb03 107 adapter->nr, info->addr, dummy->addr);
96cba9b0 108 i2c_unregister_device(dummy);
b2057c64 109 client = i2c_new_client_device(adapter, info);
96cba9b0
DT
110 }
111 }
112
c82ebf1b
WS
113 if (IS_ERR(client)) {
114 client = NULL;
8d88cb03
DT
115 pr_debug("failed to register device %d-%02x\n",
116 adapter->nr, info->addr);
c82ebf1b 117 } else {
8d88cb03
DT
118 pr_debug("added i2c device %d-%02x\n",
119 adapter->nr, info->addr);
c82ebf1b 120 }
d1381f45 121
d1381f45
BL
122 return client;
123}
124
8d88cb03 125static bool chromeos_laptop_match_adapter_devid(struct device *dev, u32 devid)
d1381f45 126{
8d88cb03 127 struct pci_dev *pdev;
49c68a21 128
8d88cb03
DT
129 if (!dev_is_pci(dev))
130 return false;
d1381f45 131
8d88cb03 132 pdev = to_pci_dev(dev);
3b9f900f 133 return devid == pci_dev_id(pdev);
d1381f45
BL
134}
135
8d88cb03 136static void chromeos_laptop_check_adapter(struct i2c_adapter *adapter)
bcaf089c 137{
8d88cb03
DT
138 struct i2c_peripheral *i2c_dev;
139 int i;
bcaf089c 140
c0bb0608 141 for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
8d88cb03 142 i2c_dev = &cros_laptop->i2c_peripherals[i];
cc5c3985 143
8d88cb03
DT
144 /* Skip devices already created */
145 if (i2c_dev->client)
146 continue;
147
148 if (strncmp(adapter->name, i2c_adapter_names[i2c_dev->type],
149 strlen(i2c_adapter_names[i2c_dev->type])))
150 continue;
151
152 if (i2c_dev->pci_devid &&
153 !chromeos_laptop_match_adapter_devid(adapter->dev.parent,
154 i2c_dev->pci_devid)) {
155 continue;
156 }
157
158 i2c_dev->client =
159 chromes_laptop_instantiate_i2c_device(adapter,
160 &i2c_dev->board_info,
161 i2c_dev->alt_addr);
162 }
8016bcbc
BL
163}
164
5020cd29
DT
165static bool chromeos_laptop_adjust_client(struct i2c_client *client)
166{
2c02f659 167 struct acpi_peripheral *acpi_dev;
5020cd29
DT
168 struct acpi_device_id acpi_ids[2] = { };
169 int i;
170 int error;
171
172 if (!has_acpi_companion(&client->dev))
173 return false;
174
175 for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
176 acpi_dev = &cros_laptop->acpi_peripherals[i];
177
178 memcpy(acpi_ids[0].id, acpi_dev->hid, ACPI_ID_LEN);
179
180 if (acpi_match_device(acpi_ids, &client->dev)) {
2c02f659 181 error = device_add_software_node(&client->dev, &acpi_dev->swnode);
5020cd29
DT
182 if (error) {
183 dev_err(&client->dev,
184 "failed to add properties: %d\n",
185 error);
186 break;
187 }
188
2c02f659
HK
189 acpi_dev->client = client;
190
5020cd29
DT
191 return true;
192 }
193 }
194
195 return false;
196}
197
8d88cb03 198static void chromeos_laptop_detach_i2c_client(struct i2c_client *client)
9ad36924 199{
2c02f659 200 struct acpi_peripheral *acpi_dev;
28cd38f1 201 struct i2c_peripheral *i2c_dev;
9ad36924 202 int i;
ec199dd5 203
2c02f659
HK
204 if (has_acpi_companion(&client->dev))
205 for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
206 acpi_dev = &cros_laptop->acpi_peripherals[i];
ec199dd5 207
2c02f659
HK
208 if (acpi_dev->client == client) {
209 acpi_dev->client = NULL;
210 return;
211 }
212 }
213 else
214 for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
215 i2c_dev = &cros_laptop->i2c_peripherals[i];
216
217 if (i2c_dev->client == client) {
218 i2c_dev->client = NULL;
219 return;
220 }
221 }
8d88cb03 222}
5502486a 223
8d88cb03
DT
224static int chromeos_laptop_i2c_notifier_call(struct notifier_block *nb,
225 unsigned long action, void *data)
226{
227 struct device *dev = data;
228
229 switch (action) {
230 case BUS_NOTIFY_ADD_DEVICE:
231 if (dev->type == &i2c_adapter_type)
232 chromeos_laptop_check_adapter(to_i2c_adapter(dev));
5020cd29
DT
233 else if (dev->type == &i2c_client_type)
234 chromeos_laptop_adjust_client(to_i2c_client(dev));
8d88cb03
DT
235 break;
236
237 case BUS_NOTIFY_REMOVED_DEVICE:
238 if (dev->type == &i2c_client_type)
239 chromeos_laptop_detach_i2c_client(to_i2c_client(dev));
240 break;
ec199dd5
AD
241 }
242
8d88cb03 243 return 0;
aabf3f44
BL
244}
245
8d88cb03
DT
246static struct notifier_block chromeos_laptop_i2c_notifier = {
247 .notifier_call = chromeos_laptop_i2c_notifier_call,
248};
249
c0bb0608
DT
250#define DECLARE_CROS_LAPTOP(_name) \
251static const struct chromeos_laptop _name __initconst = { \
252 .i2c_peripherals = _name##_peripherals, \
253 .num_i2c_peripherals = ARRAY_SIZE(_name##_peripherals), \
254}
255
5020cd29
DT
256#define DECLARE_ACPI_CROS_LAPTOP(_name) \
257static const struct chromeos_laptop _name __initconst = { \
258 .acpi_peripherals = _name##_peripherals, \
259 .num_acpi_peripherals = ARRAY_SIZE(_name##_peripherals), \
260}
261
c0bb0608
DT
262static struct i2c_peripheral samsung_series_5_550_peripherals[] __initdata = {
263 /* Touchpad. */
264 {
265 .board_info = {
266 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
267 .flags = I2C_CLIENT_WAKE,
28cd38f1 268 },
c0bb0608
DT
269 .dmi_name = "trackpad",
270 .type = I2C_ADAPTER_SMBUS,
271 },
272 /* Light Sensor. */
273 {
274 .board_info = {
275 I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
28cd38f1 276 },
c0bb0608
DT
277 .dmi_name = "lightsensor",
278 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
279 },
280};
c0bb0608 281DECLARE_CROS_LAPTOP(samsung_series_5_550);
ec199dd5 282
c0bb0608
DT
283static struct i2c_peripheral samsung_series_5_peripherals[] __initdata = {
284 /* Light Sensor. */
285 {
286 .board_info = {
287 I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
28cd38f1 288 },
c0bb0608 289 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
290 },
291};
c0bb0608 292DECLARE_CROS_LAPTOP(samsung_series_5);
ec199dd5 293
c0bb0608 294static const int chromebook_pixel_tp_keys[] __initconst = {
28cd38f1
DT
295 KEY_RESERVED,
296 KEY_RESERVED,
297 KEY_RESERVED,
298 KEY_RESERVED,
299 KEY_RESERVED,
300 BTN_LEFT
301};
302
c0bb0608
DT
303static const struct property_entry
304chromebook_pixel_trackpad_props[] __initconst = {
5020cd29 305 PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
f00c1d19
DT
306 PROPERTY_ENTRY_U32_ARRAY("linux,gpio-keymap", chromebook_pixel_tp_keys),
307 { }
28cd38f1
DT
308};
309
5020cd29
DT
310static const struct property_entry
311chromebook_atmel_touchscreen_props[] __initconst = {
312 PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
313 { }
314};
315
c0bb0608
DT
316static struct i2c_peripheral chromebook_pixel_peripherals[] __initdata = {
317 /* Touch Screen. */
318 {
319 .board_info = {
320 I2C_BOARD_INFO("atmel_mxt_ts",
321 ATMEL_TS_I2C_ADDR),
322 .flags = I2C_CLIENT_WAKE,
28cd38f1 323 },
c0bb0608
DT
324 .dmi_name = "touchscreen",
325 .irqflags = IRQF_TRIGGER_FALLING,
326 .type = I2C_ADAPTER_PANEL,
327 .alt_addr = ATMEL_TS_I2C_BL_ADDR,
2c02f659 328 .properties = chromebook_atmel_touchscreen_props,
c0bb0608
DT
329 },
330 /* Touchpad. */
331 {
332 .board_info = {
333 I2C_BOARD_INFO("atmel_mxt_tp",
334 ATMEL_TP_I2C_ADDR),
c0bb0608 335 .flags = I2C_CLIENT_WAKE,
28cd38f1 336 },
c0bb0608
DT
337 .dmi_name = "trackpad",
338 .irqflags = IRQF_TRIGGER_FALLING,
339 .type = I2C_ADAPTER_VGADDC,
340 .alt_addr = ATMEL_TP_I2C_BL_ADDR,
2c02f659 341 .properties = chromebook_pixel_trackpad_props,
c0bb0608
DT
342 },
343 /* Light Sensor. */
344 {
345 .board_info = {
346 I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
28cd38f1 347 },
c0bb0608
DT
348 .dmi_name = "lightsensor",
349 .type = I2C_ADAPTER_PANEL,
ec199dd5
AD
350 },
351};
c0bb0608 352DECLARE_CROS_LAPTOP(chromebook_pixel);
ec199dd5 353
c0bb0608
DT
354static struct i2c_peripheral hp_chromebook_14_peripherals[] __initdata = {
355 /* Touchpad. */
356 {
357 .board_info = {
358 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
359 .flags = I2C_CLIENT_WAKE,
28cd38f1 360 },
c0bb0608
DT
361 .dmi_name = "trackpad",
362 .type = I2C_ADAPTER_DESIGNWARE,
5ea9567f
BL
363 },
364};
c0bb0608 365DECLARE_CROS_LAPTOP(hp_chromebook_14);
5ea9567f 366
c0bb0608
DT
367static struct i2c_peripheral dell_chromebook_11_peripherals[] __initdata = {
368 /* Touchpad. */
369 {
370 .board_info = {
371 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
372 .flags = I2C_CLIENT_WAKE,
28cd38f1 373 },
c0bb0608
DT
374 .dmi_name = "trackpad",
375 .type = I2C_ADAPTER_DESIGNWARE,
376 },
377 /* Elan Touchpad option. */
378 {
379 .board_info = {
380 I2C_BOARD_INFO("elan_i2c", ELAN_TP_I2C_ADDR),
381 .flags = I2C_CLIENT_WAKE,
28cd38f1 382 },
c0bb0608
DT
383 .dmi_name = "trackpad",
384 .type = I2C_ADAPTER_DESIGNWARE,
0e1e5e59
MH
385 },
386};
c0bb0608 387DECLARE_CROS_LAPTOP(dell_chromebook_11);
0e1e5e59 388
c0bb0608
DT
389static struct i2c_peripheral toshiba_cb35_peripherals[] __initdata = {
390 /* Touchpad. */
391 {
392 .board_info = {
393 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
394 .flags = I2C_CLIENT_WAKE,
28cd38f1 395 },
c0bb0608
DT
396 .dmi_name = "trackpad",
397 .type = I2C_ADAPTER_DESIGNWARE,
963cb6fa
GC
398 },
399};
c0bb0608 400DECLARE_CROS_LAPTOP(toshiba_cb35);
963cb6fa 401
c0bb0608
DT
402static struct i2c_peripheral acer_c7_chromebook_peripherals[] __initdata = {
403 /* Touchpad. */
404 {
405 .board_info = {
406 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
407 .flags = I2C_CLIENT_WAKE,
28cd38f1 408 },
c0bb0608
DT
409 .dmi_name = "trackpad",
410 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
411 },
412};
c0bb0608 413DECLARE_CROS_LAPTOP(acer_c7_chromebook);
ec199dd5 414
c0bb0608
DT
415static struct i2c_peripheral acer_ac700_peripherals[] __initdata = {
416 /* Light Sensor. */
417 {
418 .board_info = {
419 I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
28cd38f1 420 },
c0bb0608 421 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
422 },
423};
c0bb0608 424DECLARE_CROS_LAPTOP(acer_ac700);
ec199dd5 425
c0bb0608
DT
426static struct i2c_peripheral acer_c720_peripherals[] __initdata = {
427 /* Touchscreen. */
428 {
429 .board_info = {
430 I2C_BOARD_INFO("atmel_mxt_ts",
431 ATMEL_TS_I2C_ADDR),
432 .flags = I2C_CLIENT_WAKE,
28cd38f1 433 },
c0bb0608
DT
434 .dmi_name = "touchscreen",
435 .irqflags = IRQF_TRIGGER_FALLING,
436 .type = I2C_ADAPTER_DESIGNWARE,
437 .pci_devid = PCI_DEVID(0, PCI_DEVFN(0x15, 0x2)),
438 .alt_addr = ATMEL_TS_I2C_BL_ADDR,
2c02f659 439 .properties = chromebook_atmel_touchscreen_props,
c0bb0608
DT
440 },
441 /* Touchpad. */
442 {
443 .board_info = {
444 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
445 .flags = I2C_CLIENT_WAKE,
28cd38f1 446 },
c0bb0608
DT
447 .dmi_name = "trackpad",
448 .type = I2C_ADAPTER_DESIGNWARE,
449 .pci_devid = PCI_DEVID(0, PCI_DEVFN(0x15, 0x1)),
450 },
451 /* Elan Touchpad option. */
452 {
453 .board_info = {
454 I2C_BOARD_INFO("elan_i2c", ELAN_TP_I2C_ADDR),
455 .flags = I2C_CLIENT_WAKE,
28cd38f1 456 },
c0bb0608
DT
457 .dmi_name = "trackpad",
458 .type = I2C_ADAPTER_DESIGNWARE,
459 .pci_devid = PCI_DEVID(0, PCI_DEVFN(0x15, 0x1)),
460 },
461 /* Light Sensor. */
462 {
463 .board_info = {
464 I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
28cd38f1 465 },
c0bb0608
DT
466 .dmi_name = "lightsensor",
467 .type = I2C_ADAPTER_DESIGNWARE,
468 .pci_devid = PCI_DEVID(0, PCI_DEVFN(0x15, 0x2)),
da3b0ab7
MW
469 },
470};
c0bb0608 471DECLARE_CROS_LAPTOP(acer_c720);
da3b0ab7 472
c0bb0608
DT
473static struct i2c_peripheral
474hp_pavilion_14_chromebook_peripherals[] __initdata = {
475 /* Touchpad. */
476 {
477 .board_info = {
478 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
479 .flags = I2C_CLIENT_WAKE,
28cd38f1 480 },
c0bb0608
DT
481 .dmi_name = "trackpad",
482 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
483 },
484};
c0bb0608 485DECLARE_CROS_LAPTOP(hp_pavilion_14_chromebook);
ec199dd5 486
c0bb0608
DT
487static struct i2c_peripheral cr48_peripherals[] __initdata = {
488 /* Light Sensor. */
489 {
490 .board_info = {
491 I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
28cd38f1 492 },
c0bb0608 493 .type = I2C_ADAPTER_SMBUS,
ec199dd5
AD
494 },
495};
c0bb0608 496DECLARE_CROS_LAPTOP(cr48);
ec199dd5 497
5020cd29
DT
498static const u32 samus_touchpad_buttons[] __initconst = {
499 KEY_RESERVED,
500 KEY_RESERVED,
501 KEY_RESERVED,
502 BTN_LEFT
503};
504
505static const struct property_entry samus_trackpad_props[] __initconst = {
506 PROPERTY_ENTRY_STRING("compatible", "atmel,maxtouch"),
507 PROPERTY_ENTRY_U32_ARRAY("linux,gpio-keymap", samus_touchpad_buttons),
508 { }
509};
510
511static struct acpi_peripheral samus_peripherals[] __initdata = {
512 /* Touchpad */
513 {
514 .hid = "ATML0000",
2c02f659
HK
515 .swnode = {
516 .properties = samus_trackpad_props,
517 },
5020cd29
DT
518 },
519 /* Touchsceen */
520 {
521 .hid = "ATML0001",
2c02f659
HK
522 .swnode = {
523 .properties = chromebook_atmel_touchscreen_props,
524 },
5020cd29
DT
525 },
526};
527DECLARE_ACPI_CROS_LAPTOP(samus);
528
529static struct acpi_peripheral generic_atmel_peripherals[] __initdata = {
530 /* Touchpad */
531 {
532 .hid = "ATML0000",
2c02f659
HK
533 .swnode = {
534 .properties = chromebook_pixel_trackpad_props,
535 },
5020cd29
DT
536 },
537 /* Touchsceen */
538 {
539 .hid = "ATML0001",
2c02f659
HK
540 .swnode = {
541 .properties = chromebook_atmel_touchscreen_props,
542 },
5020cd29
DT
543 },
544};
545DECLARE_ACPI_CROS_LAPTOP(generic_atmel);
546
6faadbbb 547static const struct dmi_system_id chromeos_laptop_dmi_table[] __initconst = {
d1381f45 548 {
ec199dd5 549 .ident = "Samsung Series 5 550",
d1381f45
BL
550 .matches = {
551 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
552 DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
553 },
65582920 554 .driver_data = (void *)&samsung_series_5_550,
8e1ad4c4 555 },
d1381f45 556 {
ec199dd5 557 .ident = "Samsung Series 5",
d1381f45 558 .matches = {
ec199dd5 559 DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
d1381f45 560 },
65582920 561 .driver_data = (void *)&samsung_series_5,
d1381f45 562 },
cc5c3985 563 {
ec199dd5 564 .ident = "Chromebook Pixel",
cc5c3985
BL
565 .matches = {
566 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
567 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
568 },
65582920 569 .driver_data = (void *)&chromebook_pixel,
cc5c3985 570 },
0e1e5e59
MH
571 {
572 .ident = "Wolf",
573 .matches = {
574 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
575 DMI_MATCH(DMI_PRODUCT_NAME, "Wolf"),
576 },
65582920 577 .driver_data = (void *)&dell_chromebook_11,
0e1e5e59 578 },
5ea9567f
BL
579 {
580 .ident = "HP Chromebook 14",
581 .matches = {
582 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
583 DMI_MATCH(DMI_PRODUCT_NAME, "Falco"),
584 },
65582920 585 .driver_data = (void *)&hp_chromebook_14,
5ea9567f 586 },
963cb6fa
GC
587 {
588 .ident = "Toshiba CB35",
589 .matches = {
590 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
591 DMI_MATCH(DMI_PRODUCT_NAME, "Leon"),
592 },
65582920 593 .driver_data = (void *)&toshiba_cb35,
963cb6fa 594 },
261f171f 595 {
ec199dd5 596 .ident = "Acer C7 Chromebook",
261f171f
BL
597 .matches = {
598 DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
599 },
65582920 600 .driver_data = (void *)&acer_c7_chromebook,
e65a624b
BL
601 },
602 {
ec199dd5 603 .ident = "Acer AC700",
e65a624b 604 .matches = {
ec199dd5 605 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
e65a624b 606 },
65582920 607 .driver_data = (void *)&acer_ac700,
261f171f 608 },
da3b0ab7
MW
609 {
610 .ident = "Acer C720",
611 .matches = {
612 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
613 },
65582920 614 .driver_data = (void *)&acer_c720,
da3b0ab7 615 },
8016bcbc 616 {
ec199dd5 617 .ident = "HP Pavilion 14 Chromebook",
8016bcbc 618 .matches = {
ec199dd5 619 DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
8016bcbc 620 },
65582920 621 .driver_data = (void *)&hp_pavilion_14_chromebook,
8016bcbc 622 },
aabf3f44 623 {
ec199dd5 624 .ident = "Cr-48",
aabf3f44
BL
625 .matches = {
626 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
627 },
65582920 628 .driver_data = (void *)&cr48,
aabf3f44 629 },
5020cd29
DT
630 /* Devices with peripherals incompletely described in ACPI */
631 {
632 .ident = "Chromebook Pro",
633 .matches = {
634 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
635 DMI_MATCH(DMI_PRODUCT_NAME, "Caroline"),
636 },
637 .driver_data = (void *)&samus,
638 },
639 {
640 .ident = "Google Pixel 2 (2015)",
641 .matches = {
642 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
643 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
644 },
645 .driver_data = (void *)&samus,
646 },
683b6473
DT
647 {
648 .ident = "Samsung Chromebook 3",
649 .matches = {
650 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
651 DMI_MATCH(DMI_PRODUCT_NAME, "Celes"),
652 },
653 .driver_data = (void *)&samus,
654 },
5020cd29
DT
655 {
656 /*
657 * Other Chromebooks with Atmel touch controllers:
683b6473 658 * - Winky (touchpad)
5020cd29
DT
659 * - Clapper, Expresso, Rambi, Glimmer (touchscreen)
660 */
661 .ident = "Other Chromebook",
662 .matches = {
663 /*
664 * This will match all Google devices, not only devices
665 * with Atmel, but we will validate that the device
666 * actually has matching peripherals.
667 */
668 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
669 },
670 .driver_data = (void *)&generic_atmel,
671 },
d1381f45
BL
672 { }
673};
674MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
675
5020cd29 676static int __init chromeos_laptop_scan_peripherals(struct device *dev, void *data)
8d88cb03 677{
5020cd29 678 int error;
9ad36924 679
5020cd29
DT
680 if (dev->type == &i2c_adapter_type) {
681 chromeos_laptop_check_adapter(to_i2c_adapter(dev));
682 } else if (dev->type == &i2c_client_type) {
683 if (chromeos_laptop_adjust_client(to_i2c_client(dev))) {
684 /*
685 * Now that we have needed properties re-trigger
686 * driver probe in case driver was initialized
687 * earlier and probe failed.
688 */
689 error = device_attach(dev);
690 if (error < 0)
691 dev_warn(dev,
692 "%s: device_attach() failed: %d\n",
693 __func__, error);
694 }
695 }
8d88cb03
DT
696
697 return 0;
698}
9ad36924 699
65582920
DT
700static int __init chromeos_laptop_get_irq_from_dmi(const char *dmi_name)
701{
702 const struct dmi_device *dmi_dev;
703 const struct dmi_dev_onboard *dev_data;
704
705 dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, dmi_name, NULL);
706 if (!dmi_dev) {
707 pr_err("failed to find DMI device '%s'\n", dmi_name);
708 return -ENOENT;
709 }
710
711 dev_data = dmi_dev->device_data;
712 if (!dev_data) {
713 pr_err("failed to get data from DMI for '%s'\n", dmi_name);
714 return -EINVAL;
715 }
716
717 return dev_data->instance;
718}
719
c0bb0608 720static int __init chromeos_laptop_setup_irq(struct i2c_peripheral *i2c_dev)
65582920 721{
65582920 722 int irq;
65582920 723
c0bb0608 724 if (i2c_dev->dmi_name) {
65582920
DT
725 irq = chromeos_laptop_get_irq_from_dmi(i2c_dev->dmi_name);
726 if (irq < 0)
c0bb0608 727 return irq;
e6215eea
DT
728
729 i2c_dev->irq_resource = (struct resource)
730 DEFINE_RES_NAMED(irq, 1, NULL,
731 IORESOURCE_IRQ | i2c_dev->irqflags);
732 i2c_dev->board_info.resources = &i2c_dev->irq_resource;
733 i2c_dev->board_info.num_resources = 1;
65582920
DT
734 }
735
c0bb0608
DT
736 return 0;
737}
738
5020cd29
DT
739static int __init
740chromeos_laptop_prepare_i2c_peripherals(struct chromeos_laptop *cros_laptop,
741 const struct chromeos_laptop *src)
c0bb0608 742{
6ad4194d 743 struct i2c_peripheral *i2c_peripherals;
c0bb0608
DT
744 struct i2c_peripheral *i2c_dev;
745 struct i2c_board_info *info;
c0bb0608 746 int i;
5020cd29 747 int error;
c0bb0608 748
5020cd29
DT
749 if (!src->num_i2c_peripherals)
750 return 0;
c0bb0608 751
d1b35e6d
YJ
752 i2c_peripherals = kmemdup_array(src->i2c_peripherals,
753 src->num_i2c_peripherals,
754 sizeof(*i2c_peripherals), GFP_KERNEL);
6ad4194d 755 if (!i2c_peripherals)
5020cd29 756 return -ENOMEM;
c0bb0608 757
6ad4194d
RS
758 for (i = 0; i < src->num_i2c_peripherals; i++) {
759 i2c_dev = &i2c_peripherals[i];
c0bb0608
DT
760 info = &i2c_dev->board_info;
761
762 error = chromeos_laptop_setup_irq(i2c_dev);
763 if (error)
5020cd29 764 goto err_out;
c0bb0608 765
2c02f659
HK
766 /* Create primary fwnode for the device - copies everything */
767 if (i2c_dev->properties) {
768 info->fwnode = fwnode_create_software_node(i2c_dev->properties, NULL);
769 if (IS_ERR(info->fwnode)) {
770 error = PTR_ERR(info->fwnode);
5020cd29 771 goto err_out;
c0bb0608
DT
772 }
773 }
774 }
775
6ad4194d
RS
776 cros_laptop->i2c_peripherals = i2c_peripherals;
777 cros_laptop->num_i2c_peripherals = src->num_i2c_peripherals;
778
5020cd29 779 return 0;
c0bb0608 780
5020cd29 781err_out:
c0bb0608 782 while (--i >= 0) {
6ad4194d 783 i2c_dev = &i2c_peripherals[i];
c0bb0608 784 info = &i2c_dev->board_info;
2c02f659
HK
785 if (!IS_ERR_OR_NULL(info->fwnode))
786 fwnode_remove_software_node(info->fwnode);
c0bb0608 787 }
6ad4194d 788 kfree(i2c_peripherals);
5020cd29
DT
789 return error;
790}
791
792static int __init
793chromeos_laptop_prepare_acpi_peripherals(struct chromeos_laptop *cros_laptop,
794 const struct chromeos_laptop *src)
795{
796 struct acpi_peripheral *acpi_peripherals;
797 struct acpi_peripheral *acpi_dev;
798 const struct acpi_peripheral *src_dev;
799 int n_peripherals = 0;
800 int i;
801 int error;
802
803 for (i = 0; i < src->num_acpi_peripherals; i++) {
804 if (acpi_dev_present(src->acpi_peripherals[i].hid, NULL, -1))
805 n_peripherals++;
806 }
807
808 if (!n_peripherals)
809 return 0;
810
811 acpi_peripherals = kcalloc(n_peripherals,
812 sizeof(*src->acpi_peripherals),
813 GFP_KERNEL);
814 if (!acpi_peripherals)
815 return -ENOMEM;
816
817 acpi_dev = acpi_peripherals;
818 for (i = 0; i < src->num_acpi_peripherals; i++) {
819 src_dev = &src->acpi_peripherals[i];
820 if (!acpi_dev_present(src_dev->hid, NULL, -1))
821 continue;
822
823 *acpi_dev = *src_dev;
824
825 /* We need to deep-copy properties */
2c02f659
HK
826 if (src_dev->swnode.properties) {
827 acpi_dev->swnode.properties =
828 property_entries_dup(src_dev->swnode.properties);
829 if (IS_ERR(acpi_dev->swnode.properties)) {
830 error = PTR_ERR(acpi_dev->swnode.properties);
5020cd29
DT
831 goto err_out;
832 }
833 }
834
835 acpi_dev++;
836 }
837
838 cros_laptop->acpi_peripherals = acpi_peripherals;
839 cros_laptop->num_acpi_peripherals = n_peripherals;
840
841 return 0;
842
843err_out:
844 while (--i >= 0) {
845 acpi_dev = &acpi_peripherals[i];
2c02f659
HK
846 if (!IS_ERR_OR_NULL(acpi_dev->swnode.properties))
847 property_entries_free(acpi_dev->swnode.properties);
5020cd29
DT
848 }
849
850 kfree(acpi_peripherals);
851 return error;
65582920
DT
852}
853
c0bb0608
DT
854static void chromeos_laptop_destroy(const struct chromeos_laptop *cros_laptop)
855{
5020cd29 856 const struct acpi_peripheral *acpi_dev;
c0bb0608 857 struct i2c_peripheral *i2c_dev;
c0bb0608
DT
858 int i;
859
860 for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
861 i2c_dev = &cros_laptop->i2c_peripherals[i];
38d3cfbc 862 i2c_unregister_device(i2c_dev->client);
c0bb0608
DT
863 }
864
5020cd29
DT
865 for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
866 acpi_dev = &cros_laptop->acpi_peripherals[i];
867
2c02f659
HK
868 if (acpi_dev->client)
869 device_remove_software_node(&acpi_dev->client->dev);
870
871 property_entries_free(acpi_dev->swnode.properties);
5020cd29
DT
872 }
873
c0bb0608 874 kfree(cros_laptop->i2c_peripherals);
5020cd29 875 kfree(cros_laptop->acpi_peripherals);
c0bb0608
DT
876 kfree(cros_laptop);
877}
65582920 878
5020cd29
DT
879static struct chromeos_laptop * __init
880chromeos_laptop_prepare(const struct chromeos_laptop *src)
881{
882 struct chromeos_laptop *cros_laptop;
883 int error;
884
885 cros_laptop = kzalloc(sizeof(*cros_laptop), GFP_KERNEL);
886 if (!cros_laptop)
887 return ERR_PTR(-ENOMEM);
888
889 error = chromeos_laptop_prepare_i2c_peripherals(cros_laptop, src);
890 if (!error)
891 error = chromeos_laptop_prepare_acpi_peripherals(cros_laptop,
892 src);
893
894 if (error) {
895 chromeos_laptop_destroy(cros_laptop);
896 return ERR_PTR(error);
897 }
898
899 return cros_laptop;
900}
901
d1381f45
BL
902static int __init chromeos_laptop_init(void)
903{
65582920 904 const struct dmi_system_id *dmi_id;
8d88cb03 905 int error;
49c68a21 906
65582920
DT
907 dmi_id = dmi_first_match(chromeos_laptop_dmi_table);
908 if (!dmi_id) {
4f27f677 909 pr_debug("unsupported system\n");
d1381f45
BL
910 return -ENODEV;
911 }
9ad36924 912
65582920
DT
913 pr_debug("DMI Matched %s\n", dmi_id->ident);
914
c0bb0608 915 cros_laptop = chromeos_laptop_prepare((void *)dmi_id->driver_data);
65582920
DT
916 if (IS_ERR(cros_laptop))
917 return PTR_ERR(cros_laptop);
918
5020cd29
DT
919 if (!cros_laptop->num_i2c_peripherals &&
920 !cros_laptop->num_acpi_peripherals) {
921 pr_debug("no relevant devices detected\n");
922 error = -ENODEV;
923 goto err_destroy_cros_laptop;
924 }
925
8d88cb03
DT
926 error = bus_register_notifier(&i2c_bus_type,
927 &chromeos_laptop_i2c_notifier);
928 if (error) {
5020cd29
DT
929 pr_err("failed to register i2c bus notifier: %d\n",
930 error);
931 goto err_destroy_cros_laptop;
9ad36924
BL
932 }
933
8d88cb03 934 /*
5020cd29
DT
935 * Scan adapters that have been registered and clients that have
936 * been created before we installed the notifier to make sure
937 * we do not miss any devices.
8d88cb03 938 */
5020cd29 939 i2c_for_each_dev(NULL, chromeos_laptop_scan_peripherals);
9ad36924 940
d1381f45 941 return 0;
5020cd29
DT
942
943err_destroy_cros_laptop:
944 chromeos_laptop_destroy(cros_laptop);
945 return error;
d1381f45
BL
946}
947
948static void __exit chromeos_laptop_exit(void)
949{
8d88cb03 950 bus_unregister_notifier(&i2c_bus_type, &chromeos_laptop_i2c_notifier);
c0bb0608 951 chromeos_laptop_destroy(cros_laptop);
d1381f45
BL
952}
953
954module_init(chromeos_laptop_init);
955module_exit(chromeos_laptop_exit);
956
957MODULE_DESCRIPTION("Chrome OS Laptop driver");
958MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
959MODULE_LICENSE("GPL");