platform/chrome: chromeos_laptop - Add HP Chromebook 14
[linux-2.6-block.git] / drivers / platform / chrome / chromeos_laptop.c
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/i2c/atmel_mxt_ts.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31
32 #define ATMEL_TP_I2C_ADDR       0x4b
33 #define ATMEL_TP_I2C_BL_ADDR    0x25
34 #define ATMEL_TS_I2C_ADDR       0x4a
35 #define ATMEL_TS_I2C_BL_ADDR    0x26
36 #define CYAPA_TP_I2C_ADDR       0x67
37 #define ISL_ALS_I2C_ADDR        0x44
38 #define TAOS_ALS_I2C_ADDR       0x29
39
40 static struct i2c_client *als;
41 static struct i2c_client *tp;
42 static struct i2c_client *ts;
43
44 static const char *i2c_adapter_names[] = {
45         "SMBus I801 adapter",
46         "i915 gmbus vga",
47         "i915 gmbus panel",
48         "i2c-designware-pci",
49         "i2c-designware-pci",
50 };
51
52 /* Keep this enum consistent with i2c_adapter_names */
53 enum i2c_adapter_type {
54         I2C_ADAPTER_SMBUS = 0,
55         I2C_ADAPTER_VGADDC,
56         I2C_ADAPTER_PANEL,
57         I2C_ADAPTER_DESIGNWARE_0,
58         I2C_ADAPTER_DESIGNWARE_1,
59 };
60
61 struct i2c_peripheral {
62         int (*add)(enum i2c_adapter_type type);
63         enum i2c_adapter_type type;
64 };
65
66 #define MAX_I2C_PERIPHERALS 3
67
68 struct chromeos_laptop {
69         struct i2c_peripheral i2c_peripherals[MAX_I2C_PERIPHERALS];
70 };
71
72 static struct chromeos_laptop *cros_laptop;
73
74 static struct i2c_board_info cyapa_device = {
75         I2C_BOARD_INFO("cyapa", CYAPA_TP_I2C_ADDR),
76         .flags          = I2C_CLIENT_WAKE,
77 };
78
79 static struct i2c_board_info isl_als_device = {
80         I2C_BOARD_INFO("isl29018", ISL_ALS_I2C_ADDR),
81 };
82
83 static struct i2c_board_info tsl2583_als_device = {
84         I2C_BOARD_INFO("tsl2583", TAOS_ALS_I2C_ADDR),
85 };
86
87 static struct i2c_board_info tsl2563_als_device = {
88         I2C_BOARD_INFO("tsl2563", TAOS_ALS_I2C_ADDR),
89 };
90
91 static int mxt_t19_keys[] = {
92         KEY_RESERVED,
93         KEY_RESERVED,
94         KEY_RESERVED,
95         KEY_RESERVED,
96         KEY_RESERVED,
97         BTN_LEFT
98 };
99
100 static struct mxt_platform_data atmel_224s_tp_platform_data = {
101         .irqflags               = IRQF_TRIGGER_FALLING,
102         .t19_num_keys           = ARRAY_SIZE(mxt_t19_keys),
103         .t19_keymap             = mxt_t19_keys,
104         .config                 = NULL,
105         .config_length          = 0,
106 };
107
108 static struct i2c_board_info atmel_224s_tp_device = {
109         I2C_BOARD_INFO("atmel_mxt_tp", ATMEL_TP_I2C_ADDR),
110         .platform_data = &atmel_224s_tp_platform_data,
111         .flags          = I2C_CLIENT_WAKE,
112 };
113
114 static struct mxt_platform_data atmel_1664s_platform_data = {
115         .irqflags               = IRQF_TRIGGER_FALLING,
116         .config                 = NULL,
117         .config_length          = 0,
118 };
119
120 static struct i2c_board_info atmel_1664s_device = {
121         I2C_BOARD_INFO("atmel_mxt_ts", ATMEL_TS_I2C_ADDR),
122         .platform_data = &atmel_1664s_platform_data,
123         .flags          = I2C_CLIENT_WAKE,
124 };
125
126 static struct i2c_client *__add_probed_i2c_device(
127                 const char *name,
128                 int bus,
129                 struct i2c_board_info *info,
130                 const unsigned short *addrs)
131 {
132         const struct dmi_device *dmi_dev;
133         const struct dmi_dev_onboard *dev_data;
134         struct i2c_adapter *adapter;
135         struct i2c_client *client;
136
137         if (bus < 0)
138                 return NULL;
139         /*
140          * If a name is specified, look for irq platform information stashed
141          * in DMI_DEV_TYPE_DEV_ONBOARD by the Chrome OS custom system firmware.
142          */
143         if (name) {
144                 dmi_dev = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, name, NULL);
145                 if (!dmi_dev) {
146                         pr_err("%s failed to dmi find device %s.\n",
147                                __func__,
148                                name);
149                         return NULL;
150                 }
151                 dev_data = (struct dmi_dev_onboard *)dmi_dev->device_data;
152                 if (!dev_data) {
153                         pr_err("%s failed to get data from dmi for %s.\n",
154                                __func__, name);
155                         return NULL;
156                 }
157                 info->irq = dev_data->instance;
158         }
159
160         adapter = i2c_get_adapter(bus);
161         if (!adapter) {
162                 pr_err("%s failed to get i2c adapter %d.\n", __func__, bus);
163                 return NULL;
164         }
165
166         /* add the i2c device */
167         client = i2c_new_probed_device(adapter, info, addrs, NULL);
168         if (!client)
169                 pr_err("%s failed to register device %d-%02x\n",
170                        __func__, bus, info->addr);
171         else
172                 pr_debug("%s added i2c device %d-%02x\n",
173                          __func__, bus, info->addr);
174
175         i2c_put_adapter(adapter);
176         return client;
177 }
178
179 struct i2c_lookup {
180         const char *name;
181         int instance;
182         int n;
183 };
184
185 static int __find_i2c_adap(struct device *dev, void *data)
186 {
187         struct i2c_lookup *lookup = data;
188         static const char *prefix = "i2c-";
189         struct i2c_adapter *adapter;
190         if (strncmp(dev_name(dev), prefix, strlen(prefix)) != 0)
191                 return 0;
192         adapter = to_i2c_adapter(dev);
193         if (strncmp(adapter->name, lookup->name, strlen(lookup->name)) == 0 &&
194             lookup->n++ == lookup->instance)
195                 return 1;
196         return 0;
197 }
198
199 static int find_i2c_adapter_num(enum i2c_adapter_type type)
200 {
201         struct device *dev = NULL;
202         struct i2c_adapter *adapter;
203         struct i2c_lookup lookup;
204
205         memset(&lookup, 0, sizeof(lookup));
206         lookup.name = i2c_adapter_names[type];
207         lookup.instance = (type == I2C_ADAPTER_DESIGNWARE_1) ? 1 : 0;
208
209         /* find the adapter by name */
210         dev = bus_find_device(&i2c_bus_type, NULL, &lookup, __find_i2c_adap);
211         if (!dev) {
212                 /* Adapters may appear later. Deferred probing will retry */
213                 pr_notice("%s: i2c adapter %s not found on system.\n", __func__,
214                           lookup.name);
215                 return -ENODEV;
216         }
217         adapter = to_i2c_adapter(dev);
218         return adapter->nr;
219 }
220
221 /*
222  * Takes a list of addresses in addrs as such :
223  * { addr1, ... , addrn, I2C_CLIENT_END };
224  * add_probed_i2c_device will use i2c_new_probed_device
225  * and probe for devices at all of the addresses listed.
226  * Returns NULL if no devices found.
227  * See Documentation/i2c/instantiating-devices for more information.
228  */
229 static struct i2c_client *add_probed_i2c_device(
230                 const char *name,
231                 enum i2c_adapter_type type,
232                 struct i2c_board_info *info,
233                 const unsigned short *addrs)
234 {
235         return __add_probed_i2c_device(name,
236                                        find_i2c_adapter_num(type),
237                                        info,
238                                        addrs);
239 }
240
241 /*
242  * Probes for a device at a single address, the one provided by
243  * info->addr.
244  * Returns NULL if no device found.
245  */
246 static struct i2c_client *add_i2c_device(const char *name,
247                                                 enum i2c_adapter_type type,
248                                                 struct i2c_board_info *info)
249 {
250         const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
251         return __add_probed_i2c_device(name,
252                                        find_i2c_adapter_num(type),
253                                        info,
254                                        addr_list);
255 }
256
257 static int setup_cyapa_tp(enum i2c_adapter_type type)
258 {
259         if (tp)
260                 return 0;
261
262         /* add cyapa touchpad */
263         tp = add_i2c_device("trackpad", type, &cyapa_device);
264         return (!tp) ? -EAGAIN : 0;
265 }
266
267 static int setup_atmel_224s_tp(enum i2c_adapter_type type)
268 {
269         const unsigned short addr_list[] = { ATMEL_TP_I2C_BL_ADDR,
270                                              ATMEL_TP_I2C_ADDR,
271                                              I2C_CLIENT_END };
272         if (tp)
273                 return 0;
274
275         /* add atmel mxt touchpad */
276         tp = add_probed_i2c_device("trackpad", type,
277                                    &atmel_224s_tp_device, addr_list);
278         return (!tp) ? -EAGAIN : 0;
279 }
280
281 static int setup_atmel_1664s_ts(enum i2c_adapter_type type)
282 {
283         const unsigned short addr_list[] = { ATMEL_TS_I2C_BL_ADDR,
284                                              ATMEL_TS_I2C_ADDR,
285                                              I2C_CLIENT_END };
286         if (ts)
287                 return 0;
288
289         /* add atmel mxt touch device */
290         ts = add_probed_i2c_device("touchscreen", type,
291                                    &atmel_1664s_device, addr_list);
292         return (!ts) ? -EAGAIN : 0;
293 }
294
295 static int setup_isl29018_als(enum i2c_adapter_type type)
296 {
297         if (als)
298                 return 0;
299
300         /* add isl29018 light sensor */
301         als = add_i2c_device("lightsensor", type, &isl_als_device);
302         return (!als) ? -EAGAIN : 0;
303 }
304
305 static int setup_tsl2583_als(enum i2c_adapter_type type)
306 {
307         if (als)
308                 return 0;
309
310         /* add tsl2583 light sensor */
311         als = add_i2c_device(NULL, type, &tsl2583_als_device);
312         return (!als) ? -EAGAIN : 0;
313 }
314
315 static int setup_tsl2563_als(enum i2c_adapter_type type)
316 {
317         if (als)
318                 return 0;
319
320         /* add tsl2563 light sensor */
321         als = add_i2c_device(NULL, type, &tsl2563_als_device);
322         return (!als) ? -EAGAIN : 0;
323 }
324
325 static int __init chromeos_laptop_dmi_matched(const struct dmi_system_id *id)
326 {
327         cros_laptop = (void *)id->driver_data;
328         pr_debug("DMI Matched %s.\n", id->ident);
329
330         /* Indicate to dmi_scan that processing is done. */
331         return 1;
332 }
333
334 static int chromeos_laptop_probe(struct platform_device *pdev)
335 {
336         int i;
337         int ret = 0;
338
339         for (i = 0; i < MAX_I2C_PERIPHERALS; i++) {
340                 struct i2c_peripheral *i2c_dev;
341
342                 i2c_dev = &cros_laptop->i2c_peripherals[i];
343
344                 /* No more peripherals. */
345                 if (i2c_dev->add == NULL)
346                         break;
347
348                 /* Add the device. Set -EPROBE_DEFER on any failure */
349                 if (i2c_dev->add(i2c_dev->type))
350                         ret = -EPROBE_DEFER;
351         }
352
353         return ret;
354 }
355
356 static struct chromeos_laptop samsung_series_5_550 = {
357         .i2c_peripherals = {
358                 /* Touchpad. */
359                 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
360                 /* Light Sensor. */
361                 { .add = setup_isl29018_als, I2C_ADAPTER_SMBUS },
362         },
363 };
364
365 static struct chromeos_laptop samsung_series_5 = {
366         .i2c_peripherals = {
367                 /* Light Sensor. */
368                 { .add = setup_tsl2583_als, I2C_ADAPTER_SMBUS },
369         },
370 };
371
372 static struct chromeos_laptop chromebook_pixel = {
373         .i2c_peripherals = {
374                 /* Touch Screen. */
375                 { .add = setup_atmel_1664s_ts, I2C_ADAPTER_PANEL },
376                 /* Touchpad. */
377                 { .add = setup_atmel_224s_tp, I2C_ADAPTER_VGADDC },
378                 /* Light Sensor. */
379                 { .add = setup_isl29018_als, I2C_ADAPTER_PANEL },
380         },
381 };
382
383 static struct chromeos_laptop hp_chromebook_14 = {
384         .i2c_peripherals = {
385                 /* Touchpad. */
386                 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
387         },
388 };
389
390 static struct chromeos_laptop acer_c7_chromebook = {
391         .i2c_peripherals = {
392                 /* Touchpad. */
393                 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
394         },
395 };
396
397 static struct chromeos_laptop acer_ac700 = {
398         .i2c_peripherals = {
399                 /* Light Sensor. */
400                 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
401         },
402 };
403
404 static struct chromeos_laptop acer_c720 = {
405         .i2c_peripherals = {
406                 /* Touchpad. */
407                 { .add = setup_cyapa_tp, I2C_ADAPTER_DESIGNWARE_0 },
408                 /* Light Sensor. */
409                 { .add = setup_isl29018_als, I2C_ADAPTER_DESIGNWARE_1 },
410         },
411 };
412
413 static struct chromeos_laptop hp_pavilion_14_chromebook = {
414         .i2c_peripherals = {
415                 /* Touchpad. */
416                 { .add = setup_cyapa_tp, I2C_ADAPTER_SMBUS },
417         },
418 };
419
420 static struct chromeos_laptop cr48 = {
421         .i2c_peripherals = {
422                 /* Light Sensor. */
423                 { .add = setup_tsl2563_als, I2C_ADAPTER_SMBUS },
424         },
425 };
426
427 #define _CBDD(board_) \
428         .callback = chromeos_laptop_dmi_matched, \
429         .driver_data = (void *)&board_
430
431 static struct dmi_system_id chromeos_laptop_dmi_table[] __initdata = {
432         {
433                 .ident = "Samsung Series 5 550",
434                 .matches = {
435                         DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG"),
436                         DMI_MATCH(DMI_PRODUCT_NAME, "Lumpy"),
437                 },
438                 _CBDD(samsung_series_5_550),
439         },
440         {
441                 .ident = "Samsung Series 5",
442                 .matches = {
443                         DMI_MATCH(DMI_PRODUCT_NAME, "Alex"),
444                 },
445                 _CBDD(samsung_series_5),
446         },
447         {
448                 .ident = "Chromebook Pixel",
449                 .matches = {
450                         DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
451                         DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
452                 },
453                 _CBDD(chromebook_pixel),
454         },
455         {
456                 .ident = "HP Chromebook 14",
457                 .matches = {
458                         DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
459                         DMI_MATCH(DMI_PRODUCT_NAME, "Falco"),
460                 },
461                 _CBDD(hp_chromebook_14),
462         },
463         {
464                 .ident = "Acer C7 Chromebook",
465                 .matches = {
466                         DMI_MATCH(DMI_PRODUCT_NAME, "Parrot"),
467                 },
468                 _CBDD(acer_c7_chromebook),
469         },
470         {
471                 .ident = "Acer AC700",
472                 .matches = {
473                         DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
474                 },
475                 _CBDD(acer_ac700),
476         },
477         {
478                 .ident = "Acer C720",
479                 .matches = {
480                         DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
481                 },
482                 _CBDD(acer_c720),
483         },
484         {
485                 .ident = "HP Pavilion 14 Chromebook",
486                 .matches = {
487                         DMI_MATCH(DMI_PRODUCT_NAME, "Butterfly"),
488                 },
489                 _CBDD(hp_pavilion_14_chromebook),
490         },
491         {
492                 .ident = "Cr-48",
493                 .matches = {
494                         DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
495                 },
496                 _CBDD(cr48),
497         },
498         { }
499 };
500 MODULE_DEVICE_TABLE(dmi, chromeos_laptop_dmi_table);
501
502 static struct platform_device *cros_platform_device;
503
504 static struct platform_driver cros_platform_driver = {
505         .driver = {
506                 .name = "chromeos_laptop",
507                 .owner = THIS_MODULE,
508         },
509         .probe = chromeos_laptop_probe,
510 };
511
512 static int __init chromeos_laptop_init(void)
513 {
514         int ret;
515         if (!dmi_check_system(chromeos_laptop_dmi_table)) {
516                 pr_debug("%s unsupported system.\n", __func__);
517                 return -ENODEV;
518         }
519
520         ret = platform_driver_register(&cros_platform_driver);
521         if (ret)
522                 return ret;
523
524         cros_platform_device = platform_device_alloc("chromeos_laptop", -1);
525         if (!cros_platform_device) {
526                 ret = -ENOMEM;
527                 goto fail_platform_device1;
528         }
529
530         ret = platform_device_add(cros_platform_device);
531         if (ret)
532                 goto fail_platform_device2;
533
534         return 0;
535
536 fail_platform_device2:
537         platform_device_put(cros_platform_device);
538 fail_platform_device1:
539         platform_driver_unregister(&cros_platform_driver);
540         return ret;
541 }
542
543 static void __exit chromeos_laptop_exit(void)
544 {
545         if (als)
546                 i2c_unregister_device(als);
547         if (tp)
548                 i2c_unregister_device(tp);
549         if (ts)
550                 i2c_unregister_device(ts);
551
552         platform_device_unregister(cros_platform_device);
553         platform_driver_unregister(&cros_platform_driver);
554 }
555
556 module_init(chromeos_laptop_init);
557 module_exit(chromeos_laptop_exit);
558
559 MODULE_DESCRIPTION("Chrome OS Laptop driver");
560 MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
561 MODULE_LICENSE("GPL");