chromeos_laptop.c 9.0 KB

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