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