chromeos_laptop.c 15 KB

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