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