chromeos_laptop.c 14 KB

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