chromeos_laptop.c 13.7 KB
Newer Older
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
/*
 *  chromeos_laptop.c - Driver to instantiate Chromebook i2c/smbus devices.
 *
 *  Author : Benson Leung <bleung@chromium.org>
 *
 *  Copyright (C) 2012 Google, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/dmi.h>
#include <linux/i2c.h>
26 27 28
#include <linux/i2c/atmel_mxt_ts.h>
#include <linux/input.h>
#include <linux/interrupt.h>
29
#include <linux/module.h>
30
#include <linux/platform_device.h>
31

32 33
#define ATMEL_TP_I2C_ADDR	0x4b
#define ATMEL_TP_I2C_BL_ADDR	0x25
34 35
#define ATMEL_TS_I2C_ADDR	0x4a
#define ATMEL_TS_I2C_BL_ADDR	0x26
36 37
#define CYAPA_TP_I2C_ADDR	0x67
#define ISL_ALS_I2C_ADDR	0x44
38
#define TAOS_ALS_I2C_ADDR	0x29
39 40 41

static struct i2c_client *als;
static struct i2c_client *tp;
42
static struct i2c_client *ts;
43

44
static const char *i2c_adapter_names[] = {
45
	"SMBus I801 adapter",
46 47
	"i915 gmbus vga",
	"i915 gmbus panel",
48 49
	"i2c-designware-pci",
	"i2c-designware-pci",
50 51 52 53 54
};

/* Keep this enum consistent with i2c_adapter_names */
enum i2c_adapter_type {
	I2C_ADAPTER_SMBUS = 0,
55 56
	I2C_ADAPTER_VGADDC,
	I2C_ADAPTER_PANEL,
57 58
	I2C_ADAPTER_DESIGNWARE_0,
	I2C_ADAPTER_DESIGNWARE_1,
59 60
};

61
struct i2c_peripheral {
62
	int (*add)(enum i2c_adapter_type type);
63 64 65 66 67 68 69 70 71
	enum i2c_adapter_type type;
};

#define MAX_I2C_PERIPHERALS 3

struct chromeos_laptop {
	struct i2c_peripheral i2c_peripherals[MAX_I2C_PERIPHERALS];
};

72 73 74
static struct chromeos_laptop *cros_laptop;

static struct i2c_board_info cyapa_device = {
75 76 77 78
	I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
	.flags		= I2C_CLIENT_WAKE,
};

79
static struct i2c_board_info isl_als_device = {
80 81 82
	I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
};

83
static struct i2c_board_info tsl2583_als_device = {
84 85 86
	I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
};

87
static struct i2c_board_info tsl2563_als_device = {
88 89 90
	I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
};

91 92 93 94 95 96 97 98 99
static int mxt_t19_keys[] = {
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	KEY_RESERVED,
	BTN_LEFT
};

100 101
static struct mxt_platform_data atmel_224s_tp_platform_data = {
	.irqflags		= IRQF_TRIGGER_FALLING,
102 103
	.t19_num_keys		= ARRAY_SIZE(mxt_t19_keys),
	.t19_keymap		= mxt_t19_keys,
104 105 106 107
	.config			= NULL,
	.config_length		= 0,
};

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 112 113
	.flags		= I2C_CLIENT_WAKE,
};

114 115 116 117 118 119
static struct mxt_platform_data atmel_1664s_platform_data = {
	.irqflags		= IRQF_TRIGGER_FALLING,
	.config			= NULL,
	.config_length		= 0,
};

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 124 125
	.flags		= I2C_CLIENT_WAKE,
};

126
static struct i2c_client *__add_probed_i2c_device(
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
		const char *name,
		int bus,
		struct i2c_board_info *info,
		const unsigned short *addrs)
{
	const struct dmi_device *dmi_dev;
	const struct dmi_dev_onboard *dev_data;
	struct i2c_adapter *adapter;
	struct i2c_client *client;

	if (bus < 0)
		return NULL;
	/*
	 * If a name is specified, look for irq platform information stashed
	 * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
	 */
	if (name) {
		dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
		if (!dmi_dev) {
			pr_err("%s failed to dmi find device %s.\n",
			       __func__,
			       name);
			return NULL;
		}
		dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
		if (!dev_data) {
			pr_err("%s failed to get data from dmi for %s.\n",
			       __func__, name);
			return NULL;
		}
		info->irq = dev_data->instance;
	}

	adapter = i2c_get_adapter(bus);
	if (!adapter) {
		pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
		return NULL;
	}

	/* add the i2c device */
	client = i2c_new_probed_device(adapter, info, addrs, NULL);
	if (!client)
		pr_err("%s failed to register device %d-%02x\n",
		       __func__, bus, info->addr);
	else
		pr_debug("%s added i2c device %d-%02x\n",
			 __func__, bus, info->addr);

	i2c_put_adapter(adapter);
	return client;
}

179 180 181 182 183 184
struct i2c_lookup {
	const char *name;
	int instance;
	int n;
};

185
static int __find_i2c_adap(struct device *dev, void *data)
186
{
187
	struct i2c_lookup *lookup = data;
188 189
	static const char *prefix = "i2c-";
	struct i2c_adapter *adapter;
190

191 192 193
	if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
		return 0;
	adapter = to_i2c_adapter(dev);
194 195 196 197
	if (strncmp(adapter->name, lookup->name, strlen(lookup->name)) == 0 &&
	    lookup->n++ == lookup->instance)
		return 1;
	return 0;
198 199
}

200
static int find_i2c_adapter_num(enum i2c_adapter_type type)
201 202 203
{
	struct device *dev = NULL;
	struct i2c_adapter *adapter;
204 205 206 207 208 209
	struct i2c_lookup lookup;

	memset(&lookup, 0, sizeof(lookup));
	lookup.name = i2c_adapter_names[type];
	lookup.instance = (type == I2C_ADAPTER_DESIGNWARE_1) ? 1 : 0;

210
	/* find the adapter by name */
211
	dev = bus_find_device(&i2c_bus_type, NULL, &lookup, __find_i2c_adap);
212
	if (!dev) {
213 214
		/* Adapters may appear later. Deferred probing will retry */
		pr_notice("%s: i2c adapter %s not found on system.\n", __func__,
215
			  lookup.name);
216 217 218 219 220 221
		return -ENODEV;
	}
	adapter = to_i2c_adapter(dev);
	return adapter->nr;
}

222 223 224 225 226 227 228 229
/*
 * Takes a list of addresses in addrs as such :
 * { addr1, ... , addrn, I2C_CLIENT_END };
 * add_probed_i2c_device will use i2c_new_probed_device
 * and probe for devices at all of the addresses listed.
 * Returns NULL if no devices found.
 * See Documentation/i2c/instantiating-devices for more information.
 */
230
static struct i2c_client *add_probed_i2c_device(
231 232 233 234 235 236 237 238 239 240 241
		const char *name,
		enum i2c_adapter_type type,
		struct i2c_board_info *info,
		const unsigned short *addrs)
{
	return __add_probed_i2c_device(name,
				       find_i2c_adapter_num(type),
				       info,
				       addrs);
}

242 243 244 245 246
/*
 * Probes for a device at a single address, the one provided by
 * info->addr.
 * Returns NULL if no device found.
 */
247
static struct i2c_client *add_i2c_device(const char *name,
248 249
						enum i2c_adapter_type type,
						struct i2c_board_info *info)
250 251
{
	const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
252

253
	return __add_probed_i2c_device(name,
254
				       find_i2c_adapter_num(type),
255 256 257 258
				       info,
				       addr_list);
}

259
static int setup_cyapa_tp(enum i2c_adapter_type type)
260
{
261 262 263
	if (tp)
		return 0;

264 265
	/* add cyapa touchpad */
	tp = add_i2c_device("trackpad", type, &cyapa_device);
266
	return (!tp) ? -EAGAIN : 0;
267 268
}

269
static int setup_atmel_224s_tp(enum i2c_adapter_type type)
270 271 272 273
{
	const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
					     ATMEL_TP_I2C_ADDR,
					     I2C_CLIENT_END };
274 275
	if (tp)
		return 0;
276

277 278
	/* add atmel mxt touchpad */
	tp = add_probed_i2c_device("trackpad", type,
279
				   &atmel_224s_tp_device, addr_list);
280
	return (!tp) ? -EAGAIN : 0;
281 282
}

283
static int setup_atmel_1664s_ts(enum i2c_adapter_type type)
284 285 286 287
{
	const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
					     ATMEL_TS_I2C_ADDR,
					     I2C_CLIENT_END };
288 289
	if (ts)
		return 0;
290

291 292
	/* add atmel mxt touch device */
	ts = add_probed_i2c_device("touchscreen", type,
293
				   &atmel_1664s_device, addr_list);
294
	return (!ts) ? -EAGAIN : 0;
295 296
}

297
static int setup_isl29018_als(enum i2c_adapter_type type)
298
{
299 300 301
	if (als)
		return 0;

302
	/* add isl29018 light sensor */
303
	als = add_i2c_device("lightsensor", type, &isl_als_device);
304
	return (!als) ? -EAGAIN : 0;
305 306
}

307
static int setup_tsl2583_als(enum i2c_adapter_type type)
308
{
309 310 311
	if (als)
		return 0;

312 313
	/* add tsl2583 light sensor */
	als = add_i2c_device(NULL, type, &tsl2583_als_device);
314
	return (!als) ? -EAGAIN : 0;
315 316
}

317
static int setup_tsl2563_als(enum i2c_adapter_type type)
318
{
319 320 321
	if (als)
		return 0;

322 323
	/* add tsl2563 light sensor */
	als = add_i2c_device(NULL, type, &tsl2563_als_device);
324
	return (!als) ? -EAGAIN : 0;
325 326
}

327
static int __init chromeos_laptop_dmi_matched(const struct dmi_system_id *id)
328
{
329 330
	cros_laptop = (void *)id->driver_data;
	pr_debug("DMI Matched %s.\n", id->ident);
331

332 333 334 335 336 337 338 339
	/* Indicate to dmi_scan that processing is done. */
	return 1;
}

static int chromeos_laptop_probe(struct platform_device *pdev)
{
	int i;
	int ret = 0;
340 341 342 343 344 345 346 347 348 349

	for (i = 0; i < MAX_I2C_PERIPHERALS; i++) {
		struct i2c_peripheral *i2c_dev;

		i2c_dev = &cros_laptop->i2c_peripherals[i];

		/* No more peripherals. */
		if (i2c_dev->add == NULL)
			break;

350 351 352
		/* Add the device. Set -EPROBE_DEFER on any failure */
		if (i2c_dev->add(i2c_dev->type))
			ret = -EPROBE_DEFER;
353 354
	}

355
	return ret;
356 357
}

358
static struct chromeos_laptop samsung_series_5_550 = {
359 360 361 362 363 364 365 366
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
		/* Light Sensor. */
		{ .add = setup_isl29018_als, I2C_ADAPTER_SMBUS },
	},
};

367
static struct chromeos_laptop samsung_series_5 = {
368 369 370 371 372 373
	.i2c_peripherals = {
		/* Light Sensor. */
		{ .add = setup_tsl2583_als, I2C_ADAPTER_SMBUS },
	},
};

374
static struct chromeos_laptop chromebook_pixel = {
375 376 377 378 379 380 381 382 383 384
	.i2c_peripherals = {
		/* Touch Screen. */
		{ .add = setup_atmel_1664s_ts, I2C_ADAPTER_PANEL },
		/* Touchpad. */
		{ .add = setup_atmel_224s_tp, I2C_ADAPTER_VGADDC },
		/* Light Sensor. */
		{ .add = setup_isl29018_als, I2C_ADAPTER_PANEL },
	},
};

385 386 387 388 389 390 391
static struct chromeos_laptop hp_chromebook_14 = {
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
	},
};

392 393 394 395 396 397 398
static struct chromeos_laptop dell_chromebook_11 = {
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
	},
};

399 400 401 402 403 404 405
static struct chromeos_laptop toshiba_cb35 = {
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
	},
};

406
static struct chromeos_laptop acer_c7_chromebook = {
407 408 409 410 411 412
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
	},
};

413
static struct chromeos_laptop acer_ac700 = {
414 415 416 417 418 419
	.i2c_peripherals = {
		/* Light Sensor. */
		{ .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
	},
};

420 421 422 423 424 425 426 427 428
static struct chromeos_laptop acer_c720 = {
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
		/* Light Sensor. */
		{ .add = setup_isl29018_als, I2C_ADAPTER_DESIGNWARE_1 },
	},
};

429
static struct chromeos_laptop hp_pavilion_14_chromebook = {
430 431 432 433 434 435
	.i2c_peripherals = {
		/* Touchpad. */
		{ .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
	},
};

436
static struct chromeos_laptop cr48 = {
437 438 439 440 441 442 443
	.i2c_peripherals = {
		/* Light Sensor. */
		{ .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
	},
};

#define _CBDD(board_) \
444
	.callback = chromeos_laptop_dmi_matched, \
445 446
	.driver_data = (void *)&board_

447
static struct dmi_system_id chromeos_laptop_dmi_table[] __initdata = {
448
	{
449
		.ident = "Samsung Series 5 550",
450 451 452 453
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
		},
454
		_CBDD(samsung_series_5_550),
455
	},
456
	{
457
		.ident = "Samsung Series 5",
458
		.matches = {
459
			DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
460
		},
461
		_CBDD(samsung_series_5),
462
	},
463
	{
464
		.ident = "Chromebook Pixel",
465 466 467 468
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
		},
469
		_CBDD(chromebook_pixel),
470
	},
471 472 473 474 475 476 477 478
	{
		.ident = "Wolf",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Wolf"),
		},
		_CBDD(dell_chromebook_11),
	},
479 480 481 482 483 484 485 486
	{
		.ident = "HP Chromebook 14",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Falco"),
		},
		_CBDD(hp_chromebook_14),
	},
487 488 489 490 491 492 493 494
	{
		.ident = "Toshiba CB35",
		.matches = {
			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Leon"),
		},
		_CBDD(toshiba_cb35),
	},
495
	{
496
		.ident = "Acer C7 Chromebook",
497 498 499
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
		},
500
		_CBDD(acer_c7_chromebook),
501 502
	},
	{
503
		.ident = "Acer AC700",
504
		.matches = {
505
			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
506
		},
507
		_CBDD(acer_ac700),
508
	},
509 510 511 512 513 514 515
	{
		.ident = "Acer C720",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
		},
		_CBDD(acer_c720),
	},
516
	{
517
		.ident = "HP Pavilion 14 Chromebook",
518
		.matches = {
519
			DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
520
		},
521
		_CBDD(hp_pavilion_14_chromebook),
522
	},
523
	{
524
		.ident = "Cr-48",
525 526 527
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
		},
528
		_CBDD(cr48),
529
	},
530 531 532 533
	{ }
};
MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);

534 535 536 537 538 539 540 541 542 543
static struct platform_device *cros_platform_device;

static struct platform_driver cros_platform_driver = {
	.driver = {
		.name = "chromeos_laptop",
		.owner = THIS_MODULE,
	},
	.probe = chromeos_laptop_probe,
};

544 545
static int __init chromeos_laptop_init(void)
{
546
	int ret;
547

548 549 550 551
	if (!dmi_check_system(chromeos_laptop_dmi_table)) {
		pr_debug("%s unsupported system.\n", __func__);
		return -ENODEV;
	}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

	ret = platform_driver_register(&cros_platform_driver);
	if (ret)
		return ret;

	cros_platform_device = platform_device_alloc("chromeos_laptop", -1);
	if (!cros_platform_device) {
		ret = -ENOMEM;
		goto fail_platform_device1;
	}

	ret = platform_device_add(cros_platform_device);
	if (ret)
		goto fail_platform_device2;

567
	return 0;
568 569 570 571 572 573

fail_platform_device2:
	platform_device_put(cros_platform_device);
fail_platform_device1:
	platform_driver_unregister(&cros_platform_driver);
	return ret;
574 575 576 577 578 579 580 581
}

static void __exit chromeos_laptop_exit(void)
{
	if (als)
		i2c_unregister_device(als);
	if (tp)
		i2c_unregister_device(tp);
582 583
	if (ts)
		i2c_unregister_device(ts);
584 585 586

	platform_device_unregister(cros_platform_device);
	platform_driver_unregister(&cros_platform_driver);
587 588 589 590 591 592 593 594
}

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");