chromeos_laptop.c 12 KB

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