chromeos_laptop.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/module.h>
  26. #define ATMEL_TP_I2C_ADDR 0x4b
  27. #define ATMEL_TP_I2C_BL_ADDR 0x25
  28. #define ATMEL_TS_I2C_ADDR 0x4a
  29. #define ATMEL_TS_I2C_BL_ADDR 0x26
  30. #define CYAPA_TP_I2C_ADDR 0x67
  31. #define ISL_ALS_I2C_ADDR 0x44
  32. #define TAOS_ALS_I2C_ADDR 0x29
  33. static struct i2c_client *als;
  34. static struct i2c_client *tp;
  35. static struct i2c_client *ts;
  36. const char *i2c_adapter_names[] = {
  37. "SMBus I801 adapter",
  38. "i915 gmbus vga",
  39. "i915 gmbus panel",
  40. };
  41. /* Keep this enum consistent with i2c_adapter_names */
  42. enum i2c_adapter_type {
  43. I2C_ADAPTER_SMBUS = 0,
  44. I2C_ADAPTER_VGADDC,
  45. I2C_ADAPTER_PANEL,
  46. };
  47. static struct i2c_board_info __initdata cyapa_device = {
  48. I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
  49. .flags = I2C_CLIENT_WAKE,
  50. };
  51. static struct i2c_board_info __initdata isl_als_device = {
  52. I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
  53. };
  54. static struct i2c_board_info __initdata tsl2583_als_device = {
  55. I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
  56. };
  57. static struct i2c_board_info __initdata tsl2563_als_device = {
  58. I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
  59. };
  60. static struct i2c_board_info __initdata atmel_224s_tp_device = {
  61. I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
  62. .platform_data = NULL,
  63. .flags = I2C_CLIENT_WAKE,
  64. };
  65. static struct i2c_board_info __initdata atmel_1664s_device = {
  66. I2C_BOARD_INFO("atmel_mxt_ts", ATMEL_TS_I2C_ADDR),
  67. .platform_data = NULL,
  68. .flags = I2C_CLIENT_WAKE,
  69. };
  70. static struct i2c_client __init *__add_probed_i2c_device(
  71. const char *name,
  72. int bus,
  73. struct i2c_board_info *info,
  74. const unsigned short *addrs)
  75. {
  76. const struct dmi_device *dmi_dev;
  77. const struct dmi_dev_onboard *dev_data;
  78. struct i2c_adapter *adapter;
  79. struct i2c_client *client;
  80. if (bus < 0)
  81. return NULL;
  82. /*
  83. * If a name is specified, look for irq platform information stashed
  84. * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
  85. */
  86. if (name) {
  87. dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
  88. if (!dmi_dev) {
  89. pr_err("%s failed to dmi find device %s.\n",
  90. __func__,
  91. name);
  92. return NULL;
  93. }
  94. dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
  95. if (!dev_data) {
  96. pr_err("%s failed to get data from dmi for %s.\n",
  97. __func__, name);
  98. return NULL;
  99. }
  100. info->irq = dev_data->instance;
  101. }
  102. adapter = i2c_get_adapter(bus);
  103. if (!adapter) {
  104. pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
  105. return NULL;
  106. }
  107. /* add the i2c device */
  108. client = i2c_new_probed_device(adapter, info, addrs, NULL);
  109. if (!client)
  110. pr_err("%s failed to register device %d-%02x\n",
  111. __func__, bus, info->addr);
  112. else
  113. pr_debug("%s added i2c device %d-%02x\n",
  114. __func__, bus, info->addr);
  115. i2c_put_adapter(adapter);
  116. return client;
  117. }
  118. static int __init __find_i2c_adap(struct device *dev, void *data)
  119. {
  120. const char *name = data;
  121. static const char *prefix = "i2c-";
  122. struct i2c_adapter *adapter;
  123. if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
  124. return 0;
  125. adapter = to_i2c_adapter(dev);
  126. return (strncmp(adapter->name, name, strlen(name)) == 0);
  127. }
  128. static int __init find_i2c_adapter_num(enum i2c_adapter_type type)
  129. {
  130. struct device *dev = NULL;
  131. struct i2c_adapter *adapter;
  132. const char *name = i2c_adapter_names[type];
  133. /* find the adapter by name */
  134. dev = bus_find_device(&i2c_bus_type, NULL, (void *)name,
  135. __find_i2c_adap);
  136. if (!dev) {
  137. pr_err("%s: i2c adapter %s not found on system.\n", __func__,
  138. name);
  139. return -ENODEV;
  140. }
  141. adapter = to_i2c_adapter(dev);
  142. return adapter->nr;
  143. }
  144. /*
  145. * Takes a list of addresses in addrs as such :
  146. * { addr1, ... , addrn, I2C_CLIENT_END };
  147. * add_probed_i2c_device will use i2c_new_probed_device
  148. * and probe for devices at all of the addresses listed.
  149. * Returns NULL if no devices found.
  150. * See Documentation/i2c/instantiating-devices for more information.
  151. */
  152. static __init struct i2c_client *add_probed_i2c_device(
  153. const char *name,
  154. enum i2c_adapter_type type,
  155. struct i2c_board_info *info,
  156. const unsigned short *addrs)
  157. {
  158. return __add_probed_i2c_device(name,
  159. find_i2c_adapter_num(type),
  160. info,
  161. addrs);
  162. }
  163. /*
  164. * Probes for a device at a single address, the one provided by
  165. * info->addr.
  166. * Returns NULL if no device found.
  167. */
  168. static __init struct i2c_client *add_i2c_device(const char *name,
  169. enum i2c_adapter_type type,
  170. struct i2c_board_info *info)
  171. {
  172. const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
  173. return __add_probed_i2c_device(name,
  174. find_i2c_adapter_num(type),
  175. info,
  176. addr_list);
  177. }
  178. static struct i2c_client __init *add_smbus_device(const char *name,
  179. struct i2c_board_info *info)
  180. {
  181. return add_i2c_device(name, I2C_ADAPTER_SMBUS, info);
  182. }
  183. static int __init setup_cyapa_smbus_tp(const struct dmi_system_id *id)
  184. {
  185. /* add cyapa touchpad on smbus */
  186. tp = add_smbus_device("trackpad", &cyapa_device);
  187. return 0;
  188. }
  189. static int __init setup_atmel_224s_tp(const struct dmi_system_id *id)
  190. {
  191. const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
  192. ATMEL_TP_I2C_ADDR,
  193. I2C_CLIENT_END };
  194. /* add atmel mxt touchpad on VGA DDC GMBus */
  195. tp = add_probed_i2c_device("trackpad", I2C_ADAPTER_VGADDC,
  196. &atmel_224s_tp_device, addr_list);
  197. return 0;
  198. }
  199. static int __init setup_atmel_1664s_ts(const struct dmi_system_id *id)
  200. {
  201. const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
  202. ATMEL_TS_I2C_ADDR,
  203. I2C_CLIENT_END };
  204. /* add atmel mxt touch device on PANEL GMBus */
  205. ts = add_probed_i2c_device("touchscreen", I2C_ADAPTER_PANEL,
  206. &atmel_1664s_device, addr_list);
  207. return 0;
  208. }
  209. static int __init setup_isl29018_als(const struct dmi_system_id *id)
  210. {
  211. /* add isl29018 light sensor */
  212. als = add_smbus_device("lightsensor", &isl_als_device);
  213. return 0;
  214. }
  215. static int __init setup_isl29023_als(const struct dmi_system_id *id)
  216. {
  217. /* add isl29023 light sensor on Panel GMBus */
  218. als = add_i2c_device("lightsensor", I2C_ADAPTER_PANEL,
  219. &isl_als_device);
  220. return 0;
  221. }
  222. static int __init setup_tsl2583_als(const struct dmi_system_id *id)
  223. {
  224. /* add tsl2583 light sensor on smbus */
  225. als = add_smbus_device(NULL, &tsl2583_als_device);
  226. return 0;
  227. }
  228. static int __init setup_tsl2563_als(const struct dmi_system_id *id)
  229. {
  230. /* add tsl2563 light sensor on smbus */
  231. als = add_smbus_device(NULL, &tsl2563_als_device);
  232. return 0;
  233. }
  234. static struct dmi_system_id __initdata chromeos_laptop_dmi_table[] = {
  235. {
  236. .ident = "Samsung Series 5 550 - Touchpad",
  237. .matches = {
  238. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
  239. DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
  240. },
  241. .callback = setup_cyapa_smbus_tp,
  242. },
  243. {
  244. .ident = "Chromebook Pixel - Touchscreen",
  245. .matches = {
  246. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  247. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  248. },
  249. .callback = setup_atmel_1664s_ts,
  250. },
  251. {
  252. .ident = "Chromebook Pixel - Touchpad",
  253. .matches = {
  254. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  255. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  256. },
  257. .callback = setup_atmel_224s_tp,
  258. },
  259. {
  260. .ident = "Samsung Series 5 550 - Light Sensor",
  261. .matches = {
  262. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
  263. DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
  264. },
  265. .callback = setup_isl29018_als,
  266. },
  267. {
  268. .ident = "Chromebook Pixel - Light Sensor",
  269. .matches = {
  270. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  271. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  272. },
  273. .callback = setup_isl29023_als,
  274. },
  275. {
  276. .ident = "Acer C7 Chromebook - Touchpad",
  277. .matches = {
  278. DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
  279. },
  280. .callback = setup_cyapa_smbus_tp,
  281. },
  282. {
  283. .ident = "HP Pavilion 14 Chromebook - Touchpad",
  284. .matches = {
  285. DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
  286. },
  287. .callback = setup_cyapa_smbus_tp,
  288. },
  289. {
  290. .ident = "Samsung Series 5 - Light Sensor",
  291. .matches = {
  292. DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
  293. },
  294. .callback = setup_tsl2583_als,
  295. },
  296. {
  297. .ident = "Cr-48 - Light Sensor",
  298. .matches = {
  299. DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
  300. },
  301. .callback = setup_tsl2563_als,
  302. },
  303. {
  304. .ident = "Acer AC700 - Light Sensor",
  305. .matches = {
  306. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  307. },
  308. .callback = setup_tsl2563_als,
  309. },
  310. { }
  311. };
  312. MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
  313. static int __init chromeos_laptop_init(void)
  314. {
  315. if (!dmi_check_system(chromeos_laptop_dmi_table)) {
  316. pr_debug("%s unsupported system.\n", __func__);
  317. return -ENODEV;
  318. }
  319. return 0;
  320. }
  321. static void __exit chromeos_laptop_exit(void)
  322. {
  323. if (als)
  324. i2c_unregister_device(als);
  325. if (tp)
  326. i2c_unregister_device(tp);
  327. if (ts)
  328. i2c_unregister_device(ts);
  329. }
  330. module_init(chromeos_laptop_init);
  331. module_exit(chromeos_laptop_exit);
  332. MODULE_DESCRIPTION("Chrome OS Laptop driver");
  333. MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
  334. MODULE_LICENSE("GPL");