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