2 * chromeos_laptop.c - Driver to instantiate Chromebook i2c/smbus devices.
4 * Author : Benson Leung <bleung@chromium.org>
6 * Copyright (C) 2012 Google, Inc.
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.
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.
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
24 #include <linux/dmi.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c/atmel_mxt_ts.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
32 #define ATMEL_TP_I2C_ADDR 0x4b
33 #define ATMEL_TP_I2C_BL_ADDR 0x25
34 #define ATMEL_TS_I2C_ADDR 0x4a
35 #define ATMEL_TS_I2C_BL_ADDR 0x26
36 #define CYAPA_TP_I2C_ADDR 0x67
37 #define ISL_ALS_I2C_ADDR 0x44
38 #define TAOS_ALS_I2C_ADDR 0x29
40 static struct i2c_client *als;
41 static struct i2c_client *tp;
42 static struct i2c_client *ts;
44 static const char *i2c_adapter_names[] = {
52 /* Keep this enum consistent with i2c_adapter_names */
53 enum i2c_adapter_type {
54 I2C_ADAPTER_SMBUS = 0,
57 I2C_ADAPTER_DESIGNWARE_0,
58 I2C_ADAPTER_DESIGNWARE_1,
61 struct i2c_peripheral {
62 int (*add)(enum i2c_adapter_type type);
63 enum i2c_adapter_type type;
66 #define MAX_I2C_PERIPHERALS 3
68 struct chromeos_laptop {
69 struct i2c_peripheral i2c_peripherals[MAX_I2C_PERIPHERALS];
72 static struct chromeos_laptop *cros_laptop;
74 static struct i2c_board_info cyapa_device = {
75 I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
76 .flags = I2C_CLIENT_WAKE,
79 static struct i2c_board_info isl_als_device = {
80 I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
83 static struct i2c_board_info tsl2583_als_device = {
84 I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
87 static struct i2c_board_info tsl2563_als_device = {
88 I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
91 static int mxt_t19_keys[] = {
100 static struct mxt_platform_data atmel_224s_tp_platform_data = {
101 .irqflags = IRQF_TRIGGER_FALLING,
102 .t19_num_keys = ARRAY_SIZE(mxt_t19_keys),
103 .t19_keymap = mxt_t19_keys,
108 static struct i2c_board_info atmel_224s_tp_device = {
109 I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
110 .platform_data = &atmel_224s_tp_platform_data,
111 .flags = I2C_CLIENT_WAKE,
114 static struct mxt_platform_data atmel_1664s_platform_data = {
115 .irqflags = IRQF_TRIGGER_FALLING,
120 static struct i2c_board_info atmel_1664s_device = {
121 I2C_BOARD_INFO("atmel_mxt_ts", ATMEL_TS_I2C_ADDR),
122 .platform_data = &atmel_1664s_platform_data,
123 .flags = I2C_CLIENT_WAKE,
126 static struct i2c_client *__add_probed_i2c_device(
129 struct i2c_board_info *info,
130 const unsigned short *addrs)
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;
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.
144 dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
146 pr_err("%s failed to dmi find device %s.\n",
151 dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
153 pr_err("%s failed to get data from dmi for %s.\n",
157 info->irq = dev_data->instance;
160 adapter = i2c_get_adapter(bus);
162 pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
166 /* add the i2c device */
167 client = i2c_new_probed_device(adapter, info, addrs, NULL);
169 pr_err("%s failed to register device %d-%02x\n",
170 __func__, bus, info->addr);
172 pr_debug("%s added i2c device %d-%02x\n",
173 __func__, bus, info->addr);
175 i2c_put_adapter(adapter);
185 static int __find_i2c_adap(struct device *dev, void *data)
187 struct i2c_lookup *lookup = data;
188 static const char *prefix = "i2c-";
189 struct i2c_adapter *adapter;
190 if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
192 adapter = to_i2c_adapter(dev);
193 if (strncmp(adapter->name, lookup->name, strlen(lookup->name)) == 0 &&
194 lookup->n++ == lookup->instance)
199 static int find_i2c_adapter_num(enum i2c_adapter_type type)
201 struct device *dev = NULL;
202 struct i2c_adapter *adapter;
203 struct i2c_lookup lookup;
205 memset(&lookup, 0, sizeof(lookup));
206 lookup.name = i2c_adapter_names[type];
207 lookup.instance = (type == I2C_ADAPTER_DESIGNWARE_1) ? 1 : 0;
209 /* find the adapter by name */
210 dev = bus_find_device(&i2c_bus_type, NULL, &lookup, __find_i2c_adap);
212 /* Adapters may appear later. Deferred probing will retry */
213 pr_notice("%s: i2c adapter %s not found on system.\n", __func__,
217 adapter = to_i2c_adapter(dev);
222 * Takes a list of addresses in addrs as such :
223 * { addr1, ... , addrn, I2C_CLIENT_END };
224 * add_probed_i2c_device will use i2c_new_probed_device
225 * and probe for devices at all of the addresses listed.
226 * Returns NULL if no devices found.
227 * See Documentation/i2c/instantiating-devices for more information.
229 static struct i2c_client *add_probed_i2c_device(
231 enum i2c_adapter_type type,
232 struct i2c_board_info *info,
233 const unsigned short *addrs)
235 return __add_probed_i2c_device(name,
236 find_i2c_adapter_num(type),
242 * Probes for a device at a single address, the one provided by
244 * Returns NULL if no device found.
246 static struct i2c_client *add_i2c_device(const char *name,
247 enum i2c_adapter_type type,
248 struct i2c_board_info *info)
250 const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
251 return __add_probed_i2c_device(name,
252 find_i2c_adapter_num(type),
257 static int setup_cyapa_tp(enum i2c_adapter_type type)
262 /* add cyapa touchpad */
263 tp = add_i2c_device("trackpad", type, &cyapa_device);
264 return (!tp) ? -EAGAIN : 0;
267 static int setup_atmel_224s_tp(enum i2c_adapter_type type)
269 const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
275 /* add atmel mxt touchpad */
276 tp = add_probed_i2c_device("trackpad", type,
277 &atmel_224s_tp_device, addr_list);
278 return (!tp) ? -EAGAIN : 0;
281 static int setup_atmel_1664s_ts(enum i2c_adapter_type type)
283 const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
289 /* add atmel mxt touch device */
290 ts = add_probed_i2c_device("touchscreen", type,
291 &atmel_1664s_device, addr_list);
292 return (!ts) ? -EAGAIN : 0;
295 static int setup_isl29018_als(enum i2c_adapter_type type)
300 /* add isl29018 light sensor */
301 als = add_i2c_device("lightsensor", type, &isl_als_device);
302 return (!als) ? -EAGAIN : 0;
305 static int setup_tsl2583_als(enum i2c_adapter_type type)
310 /* add tsl2583 light sensor */
311 als = add_i2c_device(NULL, type, &tsl2583_als_device);
312 return (!als) ? -EAGAIN : 0;
315 static int setup_tsl2563_als(enum i2c_adapter_type type)
320 /* add tsl2563 light sensor */
321 als = add_i2c_device(NULL, type, &tsl2563_als_device);
322 return (!als) ? -EAGAIN : 0;
325 static int __init chromeos_laptop_dmi_matched(const struct dmi_system_id *id)
327 cros_laptop = (void *)id->driver_data;
328 pr_debug("DMI Matched %s.\n", id->ident);
330 /* Indicate to dmi_scan that processing is done. */
334 static int chromeos_laptop_probe(struct platform_device *pdev)
339 for (i = 0; i < MAX_I2C_PERIPHERALS; i++) {
340 struct i2c_peripheral *i2c_dev;
342 i2c_dev = &cros_laptop->i2c_peripherals[i];
344 /* No more peripherals. */
345 if (i2c_dev->add == NULL)
348 /* Add the device. Set -EPROBE_DEFER on any failure */
349 if (i2c_dev->add(i2c_dev->type))
356 static struct chromeos_laptop samsung_series_5_550 = {
359 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
361 { .add = setup_isl29018_als, I2C_ADAPTER_SMBUS },
365 static struct chromeos_laptop samsung_series_5 = {
368 { .add = setup_tsl2583_als, I2C_ADAPTER_SMBUS },
372 static struct chromeos_laptop chromebook_pixel = {
375 { .add = setup_atmel_1664s_ts, I2C_ADAPTER_PANEL },
377 { .add = setup_atmel_224s_tp, I2C_ADAPTER_VGADDC },
379 { .add = setup_isl29018_als, I2C_ADAPTER_PANEL },
383 static struct chromeos_laptop acer_c7_chromebook = {
386 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
390 static struct chromeos_laptop acer_ac700 = {
393 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
397 static struct chromeos_laptop acer_c720 = {
400 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
402 { .add = setup_isl29018_als, I2C_ADAPTER_DESIGNWARE_1 },
406 static struct chromeos_laptop hp_pavilion_14_chromebook = {
409 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
413 static struct chromeos_laptop cr48 = {
416 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
420 #define _CBDD(board_) \
421 .callback = chromeos_laptop_dmi_matched, \
422 .driver_data = (void *)&board_
424 static struct dmi_system_id chromeos_laptop_dmi_table[] __initdata = {
426 .ident = "Samsung Series 5 550",
428 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
429 DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
431 _CBDD(samsung_series_5_550),
434 .ident = "Samsung Series 5",
436 DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
438 _CBDD(samsung_series_5),
441 .ident = "Chromebook Pixel",
443 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
444 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
446 _CBDD(chromebook_pixel),
449 .ident = "Acer C7 Chromebook",
451 DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
453 _CBDD(acer_c7_chromebook),
456 .ident = "Acer AC700",
458 DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
463 .ident = "Acer C720",
465 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
470 .ident = "HP Pavilion 14 Chromebook",
472 DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
474 _CBDD(hp_pavilion_14_chromebook),
479 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
485 MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
487 static struct platform_device *cros_platform_device;
489 static struct platform_driver cros_platform_driver = {
491 .name = "chromeos_laptop",
492 .owner = THIS_MODULE,
494 .probe = chromeos_laptop_probe,
497 static int __init chromeos_laptop_init(void)
500 if (!dmi_check_system(chromeos_laptop_dmi_table)) {
501 pr_debug("%s unsupported system.\n", __func__);
505 ret = platform_driver_register(&cros_platform_driver);
509 cros_platform_device = platform_device_alloc("chromeos_laptop", -1);
510 if (!cros_platform_device) {
512 goto fail_platform_device1;
515 ret = platform_device_add(cros_platform_device);
517 goto fail_platform_device2;
521 fail_platform_device2:
522 platform_device_put(cros_platform_device);
523 fail_platform_device1:
524 platform_driver_unregister(&cros_platform_driver);
528 static void __exit chromeos_laptop_exit(void)
531 i2c_unregister_device(als);
533 i2c_unregister_device(tp);
535 i2c_unregister_device(ts);
537 platform_device_unregister(cros_platform_device);
538 platform_driver_unregister(&cros_platform_driver);
541 module_init(chromeos_laptop_init);
542 module_exit(chromeos_laptop_exit);
544 MODULE_DESCRIPTION("Chrome OS Laptop driver");
545 MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
546 MODULE_LICENSE("GPL");