chromeos_laptop.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 CYAPA_TP_I2C_ADDR 0x67
  29. #define ISL_ALS_I2C_ADDR 0x44
  30. #define TAOS_ALS_I2C_ADDR 0x29
  31. static struct i2c_client *als;
  32. static struct i2c_client *tp;
  33. const char *i2c_adapter_names[] = {
  34. "SMBus I801 adapter",
  35. };
  36. /* Keep this enum consistent with i2c_adapter_names */
  37. enum i2c_adapter_type {
  38. I2C_ADAPTER_SMBUS = 0,
  39. };
  40. static struct i2c_board_info __initdata cyapa_device = {
  41. I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
  42. .flags = I2C_CLIENT_WAKE,
  43. };
  44. static struct i2c_board_info __initdata isl_als_device = {
  45. I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
  46. };
  47. static struct i2c_board_info __initdata tsl2583_als_device = {
  48. I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
  49. };
  50. static struct i2c_board_info __initdata tsl2563_als_device = {
  51. I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
  52. };
  53. static struct i2c_board_info __initdata atmel_224s_tp_device = {
  54. I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
  55. .platform_data = NULL,
  56. .flags = I2C_CLIENT_WAKE,
  57. };
  58. static struct i2c_client __init *__add_probed_i2c_device(
  59. const char *name,
  60. int bus,
  61. struct i2c_board_info *info,
  62. const unsigned short *addrs)
  63. {
  64. const struct dmi_device *dmi_dev;
  65. const struct dmi_dev_onboard *dev_data;
  66. struct i2c_adapter *adapter;
  67. struct i2c_client *client;
  68. if (bus < 0)
  69. return NULL;
  70. /*
  71. * If a name is specified, look for irq platform information stashed
  72. * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
  73. */
  74. if (name) {
  75. dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
  76. if (!dmi_dev) {
  77. pr_err("%s failed to dmi find device %s.\n",
  78. __func__,
  79. name);
  80. return NULL;
  81. }
  82. dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
  83. if (!dev_data) {
  84. pr_err("%s failed to get data from dmi for %s.\n",
  85. __func__, name);
  86. return NULL;
  87. }
  88. info->irq = dev_data->instance;
  89. }
  90. adapter = i2c_get_adapter(bus);
  91. if (!adapter) {
  92. pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
  93. return NULL;
  94. }
  95. /* add the i2c device */
  96. client = i2c_new_probed_device(adapter, info, addrs, NULL);
  97. if (!client)
  98. pr_err("%s failed to register device %d-%02x\n",
  99. __func__, bus, info->addr);
  100. else
  101. pr_debug("%s added i2c device %d-%02x\n",
  102. __func__, bus, info->addr);
  103. i2c_put_adapter(adapter);
  104. return client;
  105. }
  106. static int __init __find_i2c_adap(struct device *dev, void *data)
  107. {
  108. const char *name = data;
  109. static const char *prefix = "i2c-";
  110. struct i2c_adapter *adapter;
  111. if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
  112. return 0;
  113. adapter = to_i2c_adapter(dev);
  114. return (strncmp(adapter->name, name, strlen(name)) == 0);
  115. }
  116. static int __init find_i2c_adapter_num(enum i2c_adapter_type type)
  117. {
  118. struct device *dev = NULL;
  119. struct i2c_adapter *adapter;
  120. const char *name = i2c_adapter_names[type];
  121. /* find the adapter by name */
  122. dev = bus_find_device(&i2c_bus_type, NULL, (void *)name,
  123. __find_i2c_adap);
  124. if (!dev) {
  125. pr_err("%s: i2c adapter %s not found on system.\n", __func__,
  126. name);
  127. return -ENODEV;
  128. }
  129. adapter = to_i2c_adapter(dev);
  130. return adapter->nr;
  131. }
  132. /*
  133. * Probes for a device at a single address, the one provided by
  134. * info->addr.
  135. * Returns NULL if no device found.
  136. */
  137. static struct i2c_client __init *add_smbus_device(const char *name,
  138. struct i2c_board_info *info)
  139. {
  140. const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
  141. return __add_probed_i2c_device(name,
  142. find_i2c_adapter_num(I2C_ADAPTER_SMBUS),
  143. info,
  144. addr_list);
  145. }
  146. static int __init setup_cyapa_smbus_tp(const struct dmi_system_id *id)
  147. {
  148. /* add cyapa touchpad on smbus */
  149. tp = add_smbus_device("trackpad", &cyapa_device);
  150. return 0;
  151. }
  152. static int __init setup_atmel_224s_tp(const struct dmi_system_id *id)
  153. {
  154. const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
  155. ATMEL_TP_I2C_ADDR,
  156. I2C_CLIENT_END };
  157. /* add atmel mxt touchpad on VGA DDC GMBus */
  158. tp = add_probed_i2c_device("trackpad", I2C_ADAPTER_VGADDC,
  159. &atmel_224s_tp_device, addr_list);
  160. return 0;
  161. }
  162. static int __init setup_isl29018_als(const struct dmi_system_id *id)
  163. {
  164. /* add isl29018 light sensor */
  165. als = add_smbus_device("lightsensor", &isl_als_device);
  166. return 0;
  167. }
  168. static int __init setup_tsl2583_als(const struct dmi_system_id *id)
  169. {
  170. /* add tsl2583 light sensor on smbus */
  171. als = add_smbus_device(NULL, &tsl2583_als_device);
  172. return 0;
  173. }
  174. static int __init setup_tsl2563_als(const struct dmi_system_id *id)
  175. {
  176. /* add tsl2563 light sensor on smbus */
  177. als = add_smbus_device(NULL, &tsl2563_als_device);
  178. return 0;
  179. }
  180. static struct dmi_system_id __initdata chromeos_laptop_dmi_table[] = {
  181. {
  182. .ident = "Samsung Series 5 550 - Touchpad",
  183. .matches = {
  184. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
  185. DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
  186. },
  187. .callback = setup_cyapa_smbus_tp,
  188. },
  189. {
  190. .ident = "Chromebook Pixel - Touchpad",
  191. .matches = {
  192. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  193. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  194. },
  195. .callback = setup_atmel_224s_tp,
  196. },
  197. {
  198. .ident = "Samsung Series 5 550 - Light Sensor",
  199. .matches = {
  200. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
  201. DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
  202. },
  203. .callback = setup_isl29018_als,
  204. },
  205. {
  206. .ident = "Acer C7 Chromebook - Touchpad",
  207. .matches = {
  208. DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
  209. },
  210. .callback = setup_cyapa_smbus_tp,
  211. },
  212. {
  213. .ident = "HP Pavilion 14 Chromebook - Touchpad",
  214. .matches = {
  215. DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
  216. },
  217. .callback = setup_cyapa_smbus_tp,
  218. },
  219. {
  220. .ident = "Samsung Series 5 - Light Sensor",
  221. .matches = {
  222. DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
  223. },
  224. .callback = setup_tsl2583_als,
  225. },
  226. {
  227. .ident = "Cr-48 - Light Sensor",
  228. .matches = {
  229. DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
  230. },
  231. .callback = setup_tsl2563_als,
  232. },
  233. {
  234. .ident = "Acer AC700 - Light Sensor",
  235. .matches = {
  236. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  237. },
  238. .callback = setup_tsl2563_als,
  239. },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
  243. static int __init chromeos_laptop_init(void)
  244. {
  245. if (!dmi_check_system(chromeos_laptop_dmi_table)) {
  246. pr_debug("%s unsupported system.\n", __func__);
  247. return -ENODEV;
  248. }
  249. return 0;
  250. }
  251. static void __exit chromeos_laptop_exit(void)
  252. {
  253. if (als)
  254. i2c_unregister_device(als);
  255. if (tp)
  256. i2c_unregister_device(tp);
  257. }
  258. module_init(chromeos_laptop_init);
  259. module_exit(chromeos_laptop_exit);
  260. MODULE_DESCRIPTION("Chrome OS Laptop driver");
  261. MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
  262. MODULE_LICENSE("GPL");