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