platform/chrome: Add support for the acer c720p touchscreen.
[linux-2.6-block.git] / drivers / platform / chrome / chromeos_laptop.c
CommitLineData
d1381f45
BL
1/*
2 * chromeos_laptop.c - Driver to instantiate Chromebook i2c/smbus devices.
3 *
4 * Author : Benson Leung <bleung@chromium.org>
5 *
6 * Copyright (C) 2012 Google, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/dmi.h>
25#include <linux/i2c.h>
2ef39204
BL
26#include <linux/i2c/atmel_mxt_ts.h>
27#include <linux/input.h>
28#include <linux/interrupt.h>
d1381f45 29#include <linux/module.h>
9ad36924 30#include <linux/platform_device.h>
d1381f45 31
8e1ad4c4
BL
32#define ATMEL_TP_I2C_ADDR 0x4b
33#define ATMEL_TP_I2C_BL_ADDR 0x25
33a84f8a
YS
34#define ATMEL_TS_I2C_ADDR 0x4a
35#define ATMEL_TS_I2C_BL_ADDR 0x26
d1381f45
BL
36#define CYAPA_TP_I2C_ADDR 0x67
37#define ISL_ALS_I2C_ADDR 0x44
aabf3f44 38#define TAOS_ALS_I2C_ADDR 0x29
d1381f45
BL
39
40static struct i2c_client *als;
41static struct i2c_client *tp;
33a84f8a 42static struct i2c_client *ts;
d1381f45 43
6d3c1afe 44static const char *i2c_adapter_names[] = {
d1381f45 45 "SMBus I801 adapter",
741bf0c7
BL
46 "i915 gmbus vga",
47 "i915 gmbus panel",
da3b0ab7
MW
48 "i2c-designware-pci",
49 "i2c-designware-pci",
d1381f45
BL
50};
51
52/* Keep this enum consistent with i2c_adapter_names */
53enum i2c_adapter_type {
54 I2C_ADAPTER_SMBUS = 0,
741bf0c7
BL
55 I2C_ADAPTER_VGADDC,
56 I2C_ADAPTER_PANEL,
da3b0ab7
MW
57 I2C_ADAPTER_DESIGNWARE_0,
58 I2C_ADAPTER_DESIGNWARE_1,
d1381f45
BL
59};
60
ec199dd5 61struct i2c_peripheral {
9ad36924 62 int (*add)(enum i2c_adapter_type type);
ec199dd5
AD
63 enum i2c_adapter_type type;
64};
65
66#define MAX_I2C_PERIPHERALS 3
67
68struct chromeos_laptop {
69 struct i2c_peripheral i2c_peripherals[MAX_I2C_PERIPHERALS];
70};
71
9ad36924
BL
72static struct chromeos_laptop *cros_laptop;
73
74static struct i2c_board_info cyapa_device = {
d1381f45
BL
75 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
76 .flags = I2C_CLIENT_WAKE,
77};
78
9ad36924 79static struct i2c_board_info isl_als_device = {
d1381f45
BL
80 I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
81};
82
9ad36924 83static struct i2c_board_info tsl2583_als_device = {
8016bcbc
BL
84 I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
85};
86
9ad36924 87static struct i2c_board_info tsl2563_als_device = {
aabf3f44
BL
88 I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
89};
90
fb5e4c3e
ND
91static int mxt_t19_keys[] = {
92 KEY_RESERVED,
93 KEY_RESERVED,
94 KEY_RESERVED,
95 KEY_RESERVED,
96 KEY_RESERVED,
97 BTN_LEFT
98};
99
2ef39204 100static struct mxt_platform_data atmel_224s_tp_platform_data = {
2ef39204 101 .irqflags = IRQF_TRIGGER_FALLING,
fb5e4c3e
ND
102 .t19_num_keys = ARRAY_SIZE(mxt_t19_keys),
103 .t19_keymap = mxt_t19_keys,
2ef39204
BL
104 .config = NULL,
105 .config_length = 0,
106};
107
9ad36924 108static struct i2c_board_info atmel_224s_tp_device = {
8e1ad4c4 109 I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
2ef39204 110 .platform_data = &atmel_224s_tp_platform_data,
8e1ad4c4
BL
111 .flags = I2C_CLIENT_WAKE,
112};
113
2ef39204 114static struct mxt_platform_data atmel_1664s_platform_data = {
2ef39204 115 .irqflags = IRQF_TRIGGER_FALLING,
2ef39204
BL
116 .config = NULL,
117 .config_length = 0,
118};
119
9ad36924 120static struct i2c_board_info atmel_1664s_device = {
33a84f8a 121 I2C_BOARD_INFO("atmel_mxt_ts", ATMEL_TS_I2C_ADDR),
2ef39204 122 .platform_data = &atmel_1664s_platform_data,
33a84f8a
YS
123 .flags = I2C_CLIENT_WAKE,
124};
125
9ad36924 126static struct i2c_client *__add_probed_i2c_device(
d1381f45
BL
127 const char *name,
128 int bus,
129 struct i2c_board_info *info,
130 const unsigned short *addrs)
131{
132 const struct dmi_device *dmi_dev;
133 const struct dmi_dev_onboard *dev_data;
134 struct i2c_adapter *adapter;
135 struct i2c_client *client;
136
137 if (bus < 0)
138 return NULL;
139 /*
140 * If a name is specified, look for irq platform information stashed
141 * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
142 */
143 if (name) {
144 dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
145 if (!dmi_dev) {
146 pr_err("%s failed to dmi find device %s.\n",
147 __func__,
148 name);
149 return NULL;
150 }
151 dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
152 if (!dev_data) {
153 pr_err("%s failed to get data from dmi for %s.\n",
154 __func__, name);
155 return NULL;
156 }
157 info->irq = dev_data->instance;
158 }
159
160 adapter = i2c_get_adapter(bus);
161 if (!adapter) {
162 pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
163 return NULL;
164 }
165
166 /* add the i2c device */
167 client = i2c_new_probed_device(adapter, info, addrs, NULL);
168 if (!client)
169 pr_err("%s failed to register device %d-%02x\n",
170 __func__, bus, info->addr);
171 else
172 pr_debug("%s added i2c device %d-%02x\n",
173 __func__, bus, info->addr);
174
175 i2c_put_adapter(adapter);
176 return client;
177}
178
da3b0ab7
MW
179struct i2c_lookup {
180 const char *name;
181 int instance;
182 int n;
183};
184
9ad36924 185static int __find_i2c_adap(struct device *dev, void *data)
d1381f45 186{
da3b0ab7 187 struct i2c_lookup *lookup = data;
d1381f45
BL
188 static const char *prefix = "i2c-";
189 struct i2c_adapter *adapter;
49c68a21 190
d1381f45
BL
191 if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
192 return 0;
193 adapter = to_i2c_adapter(dev);
da3b0ab7
MW
194 if (strncmp(adapter->name, lookup->name, strlen(lookup->name)) == 0 &&
195 lookup->n++ == lookup->instance)
196 return 1;
197 return 0;
d1381f45
BL
198}
199
9ad36924 200static int find_i2c_adapter_num(enum i2c_adapter_type type)
d1381f45
BL
201{
202 struct device *dev = NULL;
203 struct i2c_adapter *adapter;
da3b0ab7
MW
204 struct i2c_lookup lookup;
205
206 memset(&lookup, 0, sizeof(lookup));
207 lookup.name = i2c_adapter_names[type];
208 lookup.instance = (type == I2C_ADAPTER_DESIGNWARE_1) ? 1 : 0;
209
d1381f45 210 /* find the adapter by name */
da3b0ab7 211 dev = bus_find_device(&i2c_bus_type, NULL, &lookup, __find_i2c_adap);
d1381f45 212 if (!dev) {
9ad36924
BL
213 /* Adapters may appear later. Deferred probing will retry */
214 pr_notice("%s: i2c adapter %s not found on system.\n", __func__,
da3b0ab7 215 lookup.name);
d1381f45
BL
216 return -ENODEV;
217 }
218 adapter = to_i2c_adapter(dev);
219 return adapter->nr;
220}
221
bcaf089c
BL
222/*
223 * Takes a list of addresses in addrs as such :
224 * { addr1, ... , addrn, I2C_CLIENT_END };
225 * add_probed_i2c_device will use i2c_new_probed_device
226 * and probe for devices at all of the addresses listed.
227 * Returns NULL if no devices found.
228 * See Documentation/i2c/instantiating-devices for more information.
229 */
9ad36924 230static struct i2c_client *add_probed_i2c_device(
bcaf089c
BL
231 const char *name,
232 enum i2c_adapter_type type,
233 struct i2c_board_info *info,
234 const unsigned short *addrs)
235{
236 return __add_probed_i2c_device(name,
237 find_i2c_adapter_num(type),
238 info,
239 addrs);
240}
241
d1381f45
BL
242/*
243 * Probes for a device at a single address, the one provided by
244 * info->addr.
245 * Returns NULL if no device found.
246 */
9ad36924 247static struct i2c_client *add_i2c_device(const char *name,
e7b28845
BL
248 enum i2c_adapter_type type,
249 struct i2c_board_info *info)
d1381f45
BL
250{
251 const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
49c68a21 252
d1381f45 253 return __add_probed_i2c_device(name,
e7b28845 254 find_i2c_adapter_num(type),
d1381f45
BL
255 info,
256 addr_list);
257}
258
9ad36924 259static int setup_cyapa_tp(enum i2c_adapter_type type)
d1381f45 260{
9ad36924
BL
261 if (tp)
262 return 0;
263
ec199dd5
AD
264 /* add cyapa touchpad */
265 tp = add_i2c_device("trackpad", type, &cyapa_device);
9ad36924 266 return (!tp) ? -EAGAIN : 0;
d1381f45
BL
267}
268
9ad36924 269static int setup_atmel_224s_tp(enum i2c_adapter_type type)
8e1ad4c4
BL
270{
271 const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
272 ATMEL_TP_I2C_ADDR,
273 I2C_CLIENT_END };
9ad36924
BL
274 if (tp)
275 return 0;
8e1ad4c4 276
ec199dd5
AD
277 /* add atmel mxt touchpad */
278 tp = add_probed_i2c_device("trackpad", type,
8e1ad4c4 279 &atmel_224s_tp_device, addr_list);
9ad36924 280 return (!tp) ? -EAGAIN : 0;
8e1ad4c4
BL
281}
282
9ad36924 283static int setup_atmel_1664s_ts(enum i2c_adapter_type type)
33a84f8a
YS
284{
285 const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
286 ATMEL_TS_I2C_ADDR,
287 I2C_CLIENT_END };
9ad36924
BL
288 if (ts)
289 return 0;
33a84f8a 290
ec199dd5
AD
291 /* add atmel mxt touch device */
292 ts = add_probed_i2c_device("touchscreen", type,
33a84f8a 293 &atmel_1664s_device, addr_list);
9ad36924 294 return (!ts) ? -EAGAIN : 0;
33a84f8a
YS
295}
296
9ad36924 297static int setup_isl29018_als(enum i2c_adapter_type type)
d1381f45 298{
9ad36924
BL
299 if (als)
300 return 0;
301
d1381f45 302 /* add isl29018 light sensor */
ec199dd5 303 als = add_i2c_device("lightsensor", type, &isl_als_device);
9ad36924 304 return (!als) ? -EAGAIN : 0;
d1381f45
BL
305}
306
9ad36924 307static int setup_tsl2583_als(enum i2c_adapter_type type)
cc5c3985 308{
9ad36924
BL
309 if (als)
310 return 0;
311
ec199dd5
AD
312 /* add tsl2583 light sensor */
313 als = add_i2c_device(NULL, type, &tsl2583_als_device);
9ad36924 314 return (!als) ? -EAGAIN : 0;
cc5c3985
BL
315}
316
9ad36924 317static int setup_tsl2563_als(enum i2c_adapter_type type)
8016bcbc 318{
9ad36924
BL
319 if (als)
320 return 0;
321
ec199dd5
AD
322 /* add tsl2563 light sensor */
323 als = add_i2c_device(NULL, type, &tsl2563_als_device);
9ad36924 324 return (!als) ? -EAGAIN : 0;
8016bcbc
BL
325}
326
9ad36924 327static int __init chromeos_laptop_dmi_matched(const struct dmi_system_id *id)
aabf3f44 328{
9ad36924
BL
329 cros_laptop = (void *)id->driver_data;
330 pr_debug("DMI Matched %s.\n", id->ident);
ec199dd5 331
9ad36924
BL
332 /* Indicate to dmi_scan that processing is done. */
333 return 1;
334}
335
336static int chromeos_laptop_probe(struct platform_device *pdev)
337{
338 int i;
339 int ret = 0;
ec199dd5
AD
340
341 for (i = 0; i < MAX_I2C_PERIPHERALS; i++) {
342 struct i2c_peripheral *i2c_dev;
343
344 i2c_dev = &cros_laptop->i2c_peripherals[i];
345
346 /* No more peripherals. */
347 if (i2c_dev->add == NULL)
348 break;
349
9ad36924
BL
350 /* Add the device. Set -EPROBE_DEFER on any failure */
351 if (i2c_dev->add(i2c_dev->type))
352 ret = -EPROBE_DEFER;
ec199dd5
AD
353 }
354
9ad36924 355 return ret;
aabf3f44
BL
356}
357
9ad36924 358static struct chromeos_laptop samsung_series_5_550 = {
ec199dd5
AD
359 .i2c_peripherals = {
360 /* Touchpad. */
361 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
362 /* Light Sensor. */
363 { .add = setup_isl29018_als, I2C_ADAPTER_SMBUS },
364 },
365};
366
9ad36924 367static struct chromeos_laptop samsung_series_5 = {
ec199dd5
AD
368 .i2c_peripherals = {
369 /* Light Sensor. */
370 { .add = setup_tsl2583_als, I2C_ADAPTER_SMBUS },
371 },
372};
373
9ad36924 374static struct chromeos_laptop chromebook_pixel = {
ec199dd5
AD
375 .i2c_peripherals = {
376 /* Touch Screen. */
377 { .add = setup_atmel_1664s_ts, I2C_ADAPTER_PANEL },
378 /* Touchpad. */
379 { .add = setup_atmel_224s_tp, I2C_ADAPTER_VGADDC },
380 /* Light Sensor. */
381 { .add = setup_isl29018_als, I2C_ADAPTER_PANEL },
382 },
383};
384
5ea9567f
BL
385static struct chromeos_laptop hp_chromebook_14 = {
386 .i2c_peripherals = {
387 /* Touchpad. */
388 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
389 },
390};
391
0e1e5e59
MH
392static struct chromeos_laptop dell_chromebook_11 = {
393 .i2c_peripherals = {
394 /* Touchpad. */
395 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
396 },
397};
398
963cb6fa
GC
399static struct chromeos_laptop toshiba_cb35 = {
400 .i2c_peripherals = {
401 /* Touchpad. */
402 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
403 },
404};
405
9ad36924 406static struct chromeos_laptop acer_c7_chromebook = {
ec199dd5
AD
407 .i2c_peripherals = {
408 /* Touchpad. */
409 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
410 },
411};
412
9ad36924 413static struct chromeos_laptop acer_ac700 = {
ec199dd5
AD
414 .i2c_peripherals = {
415 /* Light Sensor. */
416 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
417 },
418};
419
da3b0ab7
MW
420static struct chromeos_laptop acer_c720 = {
421 .i2c_peripherals = {
b90b3c4a
MM
422 /* Touchscreen. */
423 { .add = setup_atmel_1664s_ts, I2C_ADAPTER_DESIGNWARE_1 },
da3b0ab7
MW
424 /* Touchpad. */
425 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
426 /* Light Sensor. */
427 { .add = setup_isl29018_als, I2C_ADAPTER_DESIGNWARE_1 },
428 },
429};
430
9ad36924 431static struct chromeos_laptop hp_pavilion_14_chromebook = {
ec199dd5
AD
432 .i2c_peripherals = {
433 /* Touchpad. */
434 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
435 },
436};
437
9ad36924 438static struct chromeos_laptop cr48 = {
ec199dd5
AD
439 .i2c_peripherals = {
440 /* Light Sensor. */
441 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
442 },
443};
444
445#define _CBDD(board_) \
9ad36924 446 .callback = chromeos_laptop_dmi_matched, \
ec199dd5
AD
447 .driver_data = (void *)&board_
448
cdddd23f 449static struct dmi_system_id chromeos_laptop_dmi_table[] __initdata = {
d1381f45 450 {
ec199dd5 451 .ident = "Samsung Series 5 550",
d1381f45
BL
452 .matches = {
453 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
454 DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
455 },
ec199dd5 456 _CBDD(samsung_series_5_550),
8e1ad4c4 457 },
d1381f45 458 {
ec199dd5 459 .ident = "Samsung Series 5",
d1381f45 460 .matches = {
ec199dd5 461 DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
d1381f45 462 },
ec199dd5 463 _CBDD(samsung_series_5),
d1381f45 464 },
cc5c3985 465 {
ec199dd5 466 .ident = "Chromebook Pixel",
cc5c3985
BL
467 .matches = {
468 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
469 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
470 },
ec199dd5 471 _CBDD(chromebook_pixel),
cc5c3985 472 },
0e1e5e59
MH
473 {
474 .ident = "Wolf",
475 .matches = {
476 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
477 DMI_MATCH(DMI_PRODUCT_NAME, "Wolf"),
478 },
479 _CBDD(dell_chromebook_11),
480 },
5ea9567f
BL
481 {
482 .ident = "HP Chromebook 14",
483 .matches = {
484 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
485 DMI_MATCH(DMI_PRODUCT_NAME, "Falco"),
486 },
487 _CBDD(hp_chromebook_14),
488 },
963cb6fa
GC
489 {
490 .ident = "Toshiba CB35",
491 .matches = {
492 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
493 DMI_MATCH(DMI_PRODUCT_NAME, "Leon"),
494 },
495 _CBDD(toshiba_cb35),
496 },
261f171f 497 {
ec199dd5 498 .ident = "Acer C7 Chromebook",
261f171f
BL
499 .matches = {
500 DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
501 },
ec199dd5 502 _CBDD(acer_c7_chromebook),
e65a624b
BL
503 },
504 {
ec199dd5 505 .ident = "Acer AC700",
e65a624b 506 .matches = {
ec199dd5 507 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
e65a624b 508 },
ec199dd5 509 _CBDD(acer_ac700),
261f171f 510 },
da3b0ab7
MW
511 {
512 .ident = "Acer C720",
513 .matches = {
514 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
515 },
516 _CBDD(acer_c720),
517 },
8016bcbc 518 {
ec199dd5 519 .ident = "HP Pavilion 14 Chromebook",
8016bcbc 520 .matches = {
ec199dd5 521 DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
8016bcbc 522 },
ec199dd5 523 _CBDD(hp_pavilion_14_chromebook),
8016bcbc 524 },
aabf3f44 525 {
ec199dd5 526 .ident = "Cr-48",
aabf3f44
BL
527 .matches = {
528 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
529 },
ec199dd5 530 _CBDD(cr48),
aabf3f44 531 },
d1381f45
BL
532 { }
533};
534MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
535
9ad36924
BL
536static struct platform_device *cros_platform_device;
537
538static struct platform_driver cros_platform_driver = {
539 .driver = {
540 .name = "chromeos_laptop",
541 .owner = THIS_MODULE,
542 },
543 .probe = chromeos_laptop_probe,
544};
545
d1381f45
BL
546static int __init chromeos_laptop_init(void)
547{
9ad36924 548 int ret;
49c68a21 549
d1381f45
BL
550 if (!dmi_check_system(chromeos_laptop_dmi_table)) {
551 pr_debug("%s unsupported system.\n", __func__);
552 return -ENODEV;
553 }
9ad36924
BL
554
555 ret = platform_driver_register(&cros_platform_driver);
556 if (ret)
557 return ret;
558
559 cros_platform_device = platform_device_alloc("chromeos_laptop", -1);
560 if (!cros_platform_device) {
561 ret = -ENOMEM;
562 goto fail_platform_device1;
563 }
564
565 ret = platform_device_add(cros_platform_device);
566 if (ret)
567 goto fail_platform_device2;
568
d1381f45 569 return 0;
9ad36924
BL
570
571fail_platform_device2:
572 platform_device_put(cros_platform_device);
573fail_platform_device1:
574 platform_driver_unregister(&cros_platform_driver);
575 return ret;
d1381f45
BL
576}
577
578static void __exit chromeos_laptop_exit(void)
579{
580 if (als)
581 i2c_unregister_device(als);
582 if (tp)
583 i2c_unregister_device(tp);
33a84f8a
YS
584 if (ts)
585 i2c_unregister_device(ts);
2b8454a7
WY
586
587 platform_device_unregister(cros_platform_device);
588 platform_driver_unregister(&cros_platform_driver);
d1381f45
BL
589}
590
591module_init(chromeos_laptop_init);
592module_exit(chromeos_laptop_exit);
593
594MODULE_DESCRIPTION("Chrome OS Laptop driver");
595MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
596MODULE_LICENSE("GPL");