chromeos_laptop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. static struct i2c_client *als;
  38. static struct i2c_client *tp;
  39. static struct i2c_client *ts;
  40. static const char *i2c_adapter_names[] = {
  41. "SMBus I801 adapter",
  42. "i915 gmbus vga",
  43. "i915 gmbus panel",
  44. };
  45. /* Keep this enum consistent with i2c_adapter_names */
  46. enum i2c_adapter_type {
  47. I2C_ADAPTER_SMBUS = 0,
  48. I2C_ADAPTER_VGADDC,
  49. I2C_ADAPTER_PANEL,
  50. };
  51. struct i2c_peripheral {
  52. int (*add)(enum i2c_adapter_type type);
  53. enum i2c_adapter_type type;
  54. };
  55. #define MAX_I2C_PERIPHERALS 3
  56. struct chromeos_laptop {
  57. struct i2c_peripheral i2c_peripherals[MAX_I2C_PERIPHERALS];
  58. };
  59. static struct chromeos_laptop *cros_laptop;
  60. static struct i2c_board_info cyapa_device = {
  61. I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
  62. .flags = I2C_CLIENT_WAKE,
  63. };
  64. static struct i2c_board_info isl_als_device = {
  65. I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
  66. };
  67. static struct i2c_board_info tsl2583_als_device = {
  68. I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
  69. };
  70. static struct i2c_board_info tsl2563_als_device = {
  71. I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
  72. };
  73. static int mxt_t19_keys[] = {
  74. KEY_RESERVED,
  75. KEY_RESERVED,
  76. KEY_RESERVED,
  77. KEY_RESERVED,
  78. KEY_RESERVED,
  79. BTN_LEFT
  80. };
  81. static struct mxt_platform_data atmel_224s_tp_platform_data = {
  82. .irqflags = IRQF_TRIGGER_FALLING,
  83. .t19_num_keys = ARRAY_SIZE(mxt_t19_keys),
  84. .t19_keymap = mxt_t19_keys,
  85. .config = NULL,
  86. .config_length = 0,
  87. };
  88. static struct i2c_board_info atmel_224s_tp_device = {
  89. I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
  90. .platform_data = &atmel_224s_tp_platform_data,
  91. .flags = I2C_CLIENT_WAKE,
  92. };
  93. static struct mxt_platform_data atmel_1664s_platform_data = {
  94. .irqflags = IRQF_TRIGGER_FALLING,
  95. .config = NULL,
  96. .config_length = 0,
  97. };
  98. static struct i2c_board_info atmel_1664s_device = {
  99. I2C_BOARD_INFO("atmel_mxt_ts", ATMEL_TS_I2C_ADDR),
  100. .platform_data = &atmel_1664s_platform_data,
  101. .flags = I2C_CLIENT_WAKE,
  102. };
  103. static struct i2c_client *__add_probed_i2c_device(
  104. const char *name,
  105. int bus,
  106. struct i2c_board_info *info,
  107. const unsigned short *addrs)
  108. {
  109. const struct dmi_device *dmi_dev;
  110. const struct dmi_dev_onboard *dev_data;
  111. struct i2c_adapter *adapter;
  112. struct i2c_client *client;
  113. if (bus < 0)
  114. return NULL;
  115. /*
  116. * If a name is specified, look for irq platform information stashed
  117. * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
  118. */
  119. if (name) {
  120. dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
  121. if (!dmi_dev) {
  122. pr_err("%s failed to dmi find device %s.\n",
  123. __func__,
  124. name);
  125. return NULL;
  126. }
  127. dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
  128. if (!dev_data) {
  129. pr_err("%s failed to get data from dmi for %s.\n",
  130. __func__, name);
  131. return NULL;
  132. }
  133. info->irq = dev_data->instance;
  134. }
  135. adapter = i2c_get_adapter(bus);
  136. if (!adapter) {
  137. pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
  138. return NULL;
  139. }
  140. /* add the i2c device */
  141. client = i2c_new_probed_device(adapter, info, addrs, NULL);
  142. if (!client)
  143. pr_err("%s failed to register device %d-%02x\n",
  144. __func__, bus, info->addr);
  145. else
  146. pr_debug("%s added i2c device %d-%02x\n",
  147. __func__, bus, info->addr);
  148. i2c_put_adapter(adapter);
  149. return client;
  150. }
  151. static int __find_i2c_adap(struct device *dev, void *data)
  152. {
  153. const char *name = data;
  154. static const char *prefix = "i2c-";
  155. struct i2c_adapter *adapter;
  156. if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
  157. return 0;
  158. adapter = to_i2c_adapter(dev);
  159. return (strncmp(adapter->name, name, strlen(name)) == 0);
  160. }
  161. static int find_i2c_adapter_num(enum i2c_adapter_type type)
  162. {
  163. struct device *dev = NULL;
  164. struct i2c_adapter *adapter;
  165. const char *name = i2c_adapter_names[type];
  166. /* find the adapter by name */
  167. dev = bus_find_device(&i2c_bus_type, NULL, (void *)name,
  168. __find_i2c_adap);
  169. if (!dev) {
  170. /* Adapters may appear later. Deferred probing will retry */
  171. pr_notice("%s: i2c adapter %s not found on system.\n", __func__,
  172. name);
  173. return -ENODEV;
  174. }
  175. adapter = to_i2c_adapter(dev);
  176. return adapter->nr;
  177. }
  178. /*
  179. * Takes a list of addresses in addrs as such :
  180. * { addr1, ... , addrn, I2C_CLIENT_END };
  181. * add_probed_i2c_device will use i2c_new_probed_device
  182. * and probe for devices at all of the addresses listed.
  183. * Returns NULL if no devices found.
  184. * See Documentation/i2c/instantiating-devices for more information.
  185. */
  186. static struct i2c_client *add_probed_i2c_device(
  187. const char *name,
  188. enum i2c_adapter_type type,
  189. struct i2c_board_info *info,
  190. const unsigned short *addrs)
  191. {
  192. return __add_probed_i2c_device(name,
  193. find_i2c_adapter_num(type),
  194. info,
  195. addrs);
  196. }
  197. /*
  198. * Probes for a device at a single address, the one provided by
  199. * info->addr.
  200. * Returns NULL if no device found.
  201. */
  202. static struct i2c_client *add_i2c_device(const char *name,
  203. enum i2c_adapter_type type,
  204. struct i2c_board_info *info)
  205. {
  206. const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
  207. return __add_probed_i2c_device(name,
  208. find_i2c_adapter_num(type),
  209. info,
  210. addr_list);
  211. }
  212. static int setup_cyapa_tp(enum i2c_adapter_type type)
  213. {
  214. if (tp)
  215. return 0;
  216. /* add cyapa touchpad */
  217. tp = add_i2c_device("trackpad", type, &cyapa_device);
  218. return (!tp) ? -EAGAIN : 0;
  219. }
  220. static int setup_atmel_224s_tp(enum i2c_adapter_type type)
  221. {
  222. const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
  223. ATMEL_TP_I2C_ADDR,
  224. I2C_CLIENT_END };
  225. if (tp)
  226. return 0;
  227. /* add atmel mxt touchpad */
  228. tp = add_probed_i2c_device("trackpad", type,
  229. &atmel_224s_tp_device, addr_list);
  230. return (!tp) ? -EAGAIN : 0;
  231. }
  232. static int setup_atmel_1664s_ts(enum i2c_adapter_type type)
  233. {
  234. const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
  235. ATMEL_TS_I2C_ADDR,
  236. I2C_CLIENT_END };
  237. if (ts)
  238. return 0;
  239. /* add atmel mxt touch device */
  240. ts = add_probed_i2c_device("touchscreen", type,
  241. &atmel_1664s_device, addr_list);
  242. return (!ts) ? -EAGAIN : 0;
  243. }
  244. static int setup_isl29018_als(enum i2c_adapter_type type)
  245. {
  246. if (als)
  247. return 0;
  248. /* add isl29018 light sensor */
  249. als = add_i2c_device("lightsensor", type, &isl_als_device);
  250. return (!als) ? -EAGAIN : 0;
  251. }
  252. static int setup_tsl2583_als(enum i2c_adapter_type type)
  253. {
  254. if (als)
  255. return 0;
  256. /* add tsl2583 light sensor */
  257. als = add_i2c_device(NULL, type, &tsl2583_als_device);
  258. return (!als) ? -EAGAIN : 0;
  259. }
  260. static int setup_tsl2563_als(enum i2c_adapter_type type)
  261. {
  262. if (als)
  263. return 0;
  264. /* add tsl2563 light sensor */
  265. als = add_i2c_device(NULL, type, &tsl2563_als_device);
  266. return (!als) ? -EAGAIN : 0;
  267. }
  268. static int __init chromeos_laptop_dmi_matched(const struct dmi_system_id *id)
  269. {
  270. cros_laptop = (void *)id->driver_data;
  271. pr_debug("DMI Matched %s.\n", id->ident);
  272. /* Indicate to dmi_scan that processing is done. */
  273. return 1;
  274. }
  275. static int chromeos_laptop_probe(struct platform_device *pdev)
  276. {
  277. int i;
  278. int ret = 0;
  279. for (i = 0; i < MAX_I2C_PERIPHERALS; i++) {
  280. struct i2c_peripheral *i2c_dev;
  281. i2c_dev = &cros_laptop->i2c_peripherals[i];
  282. /* No more peripherals. */
  283. if (i2c_dev->add == NULL)
  284. break;
  285. /* Add the device. Set -EPROBE_DEFER on any failure */
  286. if (i2c_dev->add(i2c_dev->type))
  287. ret = -EPROBE_DEFER;
  288. }
  289. return ret;
  290. }
  291. static struct chromeos_laptop samsung_series_5_550 = {
  292. .i2c_peripherals = {
  293. /* Touchpad. */
  294. { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
  295. /* Light Sensor. */
  296. { .add = setup_isl29018_als, I2C_ADAPTER_SMBUS },
  297. },
  298. };
  299. static struct chromeos_laptop samsung_series_5 = {
  300. .i2c_peripherals = {
  301. /* Light Sensor. */
  302. { .add = setup_tsl2583_als, I2C_ADAPTER_SMBUS },
  303. },
  304. };
  305. static struct chromeos_laptop chromebook_pixel = {
  306. .i2c_peripherals = {
  307. /* Touch Screen. */
  308. { .add = setup_atmel_1664s_ts, I2C_ADAPTER_PANEL },
  309. /* Touchpad. */
  310. { .add = setup_atmel_224s_tp, I2C_ADAPTER_VGADDC },
  311. /* Light Sensor. */
  312. { .add = setup_isl29018_als, I2C_ADAPTER_PANEL },
  313. },
  314. };
  315. static struct chromeos_laptop acer_c7_chromebook = {
  316. .i2c_peripherals = {
  317. /* Touchpad. */
  318. { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
  319. },
  320. };
  321. static struct chromeos_laptop acer_ac700 = {
  322. .i2c_peripherals = {
  323. /* Light Sensor. */
  324. { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
  325. },
  326. };
  327. static struct chromeos_laptop hp_pavilion_14_chromebook = {
  328. .i2c_peripherals = {
  329. /* Touchpad. */
  330. { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
  331. },
  332. };
  333. static struct chromeos_laptop cr48 = {
  334. .i2c_peripherals = {
  335. /* Light Sensor. */
  336. { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
  337. },
  338. };
  339. #define _CBDD(board_) \
  340. .callback = chromeos_laptop_dmi_matched, \
  341. .driver_data = (void *)&board_
  342. static struct dmi_system_id chromeos_laptop_dmi_table[] __initdata = {
  343. {
  344. .ident = "Samsung Series 5 550",
  345. .matches = {
  346. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
  347. DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
  348. },
  349. _CBDD(samsung_series_5_550),
  350. },
  351. {
  352. .ident = "Samsung Series 5",
  353. .matches = {
  354. DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
  355. },
  356. _CBDD(samsung_series_5),
  357. },
  358. {
  359. .ident = "Chromebook Pixel",
  360. .matches = {
  361. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  362. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  363. },
  364. _CBDD(chromebook_pixel),
  365. },
  366. {
  367. .ident = "Acer C7 Chromebook",
  368. .matches = {
  369. DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
  370. },
  371. _CBDD(acer_c7_chromebook),
  372. },
  373. {
  374. .ident = "Acer AC700",
  375. .matches = {
  376. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  377. },
  378. _CBDD(acer_ac700),
  379. },
  380. {
  381. .ident = "HP Pavilion 14 Chromebook",
  382. .matches = {
  383. DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
  384. },
  385. _CBDD(hp_pavilion_14_chromebook),
  386. },
  387. {
  388. .ident = "Cr-48",
  389. .matches = {
  390. DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
  391. },
  392. _CBDD(cr48),
  393. },
  394. { }
  395. };
  396. MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
  397. static struct platform_device *cros_platform_device;
  398. static struct platform_driver cros_platform_driver = {
  399. .driver = {
  400. .name = "chromeos_laptop",
  401. .owner = THIS_MODULE,
  402. },
  403. .probe = chromeos_laptop_probe,
  404. };
  405. static int __init chromeos_laptop_init(void)
  406. {
  407. int ret;
  408. if (!dmi_check_system(chromeos_laptop_dmi_table)) {
  409. pr_debug("%s unsupported system.\n", __func__);
  410. return -ENODEV;
  411. }
  412. ret = platform_driver_register(&cros_platform_driver);
  413. if (ret)
  414. return ret;
  415. cros_platform_device = platform_device_alloc("chromeos_laptop", -1);
  416. if (!cros_platform_device) {
  417. ret = -ENOMEM;
  418. goto fail_platform_device1;
  419. }
  420. ret = platform_device_add(cros_platform_device);
  421. if (ret)
  422. goto fail_platform_device2;
  423. return 0;
  424. fail_platform_device2:
  425. platform_device_put(cros_platform_device);
  426. fail_platform_device1:
  427. platform_driver_unregister(&cros_platform_driver);
  428. return ret;
  429. }
  430. static void __exit chromeos_laptop_exit(void)
  431. {
  432. if (als)
  433. i2c_unregister_device(als);
  434. if (tp)
  435. i2c_unregister_device(tp);
  436. if (ts)
  437. i2c_unregister_device(ts);
  438. platform_device_unregister(cros_platform_device);
  439. platform_driver_unregister(&cros_platform_driver);
  440. }
  441. module_init(chromeos_laptop_init);
  442. module_exit(chromeos_laptop_exit);
  443. MODULE_DESCRIPTION("Chrome OS Laptop driver");
  444. MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
  445. MODULE_LICENSE("GPL");