chromeos_laptop.c 6.6 KB

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