Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2015 - 2016 Red Hat, Inc * Copyright (c) 2011, 2012 Synaptics Incorporated * Copyright (c) 2011 Unixphere */ #include <linux/kernel.h> #include <linux/delay.h> #include <linux/i2c.h> #include <linux/interrupt.h> #include <linux/kconfig.h> #include <linux/lockdep.h> #include <linux/module.h> #include <linux/pm.h> #include <linux/rmi.h> #include <linux/slab.h> #include "rmi_driver.h" #define SMB_PROTOCOL_VERSION_ADDRESS 0xfd #define SMB_MAX_COUNT 32 #define RMI_SMB2_MAP_SIZE 8 /* 8 entry of 4 bytes each */ #define RMI_SMB2_MAP_FLAGS_WE 0x01 struct mapping_table_entry { __le16 rmiaddr; u8 readcount; u8 flags; }; struct rmi_smb_xport { struct rmi_transport_dev xport; struct i2c_client *client; struct mutex page_mutex; int page; u8 table_index; struct mutex mappingtable_mutex; struct mapping_table_entry mapping_table[RMI_SMB2_MAP_SIZE]; }; static int rmi_smb_get_version(struct rmi_smb_xport *rmi_smb) { struct i2c_client *client = rmi_smb->client; int retval; /* Check if for SMBus new version device by reading version byte. */ retval = i2c_smbus_read_byte_data(client, SMB_PROTOCOL_VERSION_ADDRESS); if (retval < 0) { dev_err(&client->dev, "failed to get SMBus version number!\n"); return retval; } return retval + 1; } /* SMB block write - wrapper over ic2_smb_write_block */ static int smb_block_write(struct rmi_transport_dev *xport, u8 commandcode, const void *buf, size_t len) { struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); struct i2c_client *client = rmi_smb->client; int retval; retval = i2c_smbus_write_block_data(client, commandcode, len, buf); rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "wrote %zd bytes at %#04x: %d (%*ph)\n", len, commandcode, retval, (int)len, buf); return retval; } /* * The function to get command code for smbus operations and keeps * records to the driver mapping table */ static int rmi_smb_get_command_code(struct rmi_transport_dev *xport, u16 rmiaddr, int bytecount, bool isread, u8 *commandcode) { struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); struct mapping_table_entry new_map; int i; int retval = 0; mutex_lock(&rmi_smb->mappingtable_mutex); for (i = 0; i < RMI_SMB2_MAP_SIZE; i++) { struct mapping_table_entry *entry = &rmi_smb->mapping_table[i]; if (le16_to_cpu(entry->rmiaddr) == rmiaddr) { if (isread) { if (entry->readcount == bytecount) goto exit; } else { if (entry->flags & RMI_SMB2_MAP_FLAGS_WE) { goto exit; } } } } i = rmi_smb->table_index; rmi_smb->table_index = (i + 1) % RMI_SMB2_MAP_SIZE; /* constructs mapping table data entry. 4 bytes each entry */ memset(&new_map, 0, sizeof(new_map)); new_map.rmiaddr = cpu_to_le16(rmiaddr); new_map.readcount = bytecount; new_map.flags = !isread ? RMI_SMB2_MAP_FLAGS_WE : 0; retval = smb_block_write(xport, i + 0x80, &new_map, sizeof(new_map)); if (retval < 0) { /* * if not written to device mapping table * clear the driver mapping table records */ memset(&new_map, 0, sizeof(new_map)); } /* save to the driver level mapping table */ rmi_smb->mapping_table[i] = new_map; exit: mutex_unlock(&rmi_smb->mappingtable_mutex); if (retval < 0) return retval; *commandcode = i; return 0; } static int rmi_smb_write_block(struct rmi_transport_dev *xport, u16 rmiaddr, const void *databuff, size_t len) { int retval = 0; u8 commandcode; struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); int cur_len = (int)len; mutex_lock(&rmi_smb->page_mutex); while (cur_len > 0) { /* * break into 32 bytes chunks to write get command code */ int block_len = min_t(int, len, SMB_MAX_COUNT); retval = rmi_smb_get_command_code(xport, rmiaddr, block_len, false, &commandcode); if (retval < 0) goto exit; retval = smb_block_write(xport, commandcode, databuff, block_len); if (retval < 0) goto exit; /* prepare to write next block of bytes */ cur_len -= SMB_MAX_COUNT; databuff += SMB_MAX_COUNT; rmiaddr += SMB_MAX_COUNT; } exit: mutex_unlock(&rmi_smb->page_mutex); return retval; } /* SMB block read - wrapper over ic2_smb_read_block */ static int smb_block_read(struct rmi_transport_dev *xport, u8 commandcode, void *buf, size_t len) { struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); struct i2c_client *client = rmi_smb->client; int retval; retval = i2c_smbus_read_block_data(client, commandcode, buf); if (retval < 0) return retval; return retval; } static int rmi_smb_read_block(struct rmi_transport_dev *xport, u16 rmiaddr, void *databuff, size_t len) { struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); int retval; u8 commandcode; int cur_len = (int)len; mutex_lock(&rmi_smb->page_mutex); memset(databuff, 0, len); while (cur_len > 0) { /* break into 32 bytes chunks to write get command code */ int block_len = min_t(int, cur_len, SMB_MAX_COUNT); retval = rmi_smb_get_command_code(xport, rmiaddr, block_len, true, &commandcode); if (retval < 0) goto exit; retval = smb_block_read(xport, commandcode, databuff, block_len); if (retval < 0) goto exit; /* prepare to read next block of bytes */ cur_len -= SMB_MAX_COUNT; databuff += SMB_MAX_COUNT; rmiaddr += SMB_MAX_COUNT; } retval = 0; exit: mutex_unlock(&rmi_smb->page_mutex); return retval; } static void rmi_smb_clear_state(struct rmi_smb_xport *rmi_smb) { /* the mapping table has been flushed, discard the current one */ mutex_lock(&rmi_smb->mappingtable_mutex); memset(rmi_smb->mapping_table, 0, sizeof(rmi_smb->mapping_table)); mutex_unlock(&rmi_smb->mappingtable_mutex); } static int rmi_smb_enable_smbus_mode(struct rmi_smb_xport *rmi_smb) { struct i2c_client *client = rmi_smb->client; int smbus_version; /* * psmouse driver resets the controller, we only need to wait * to give the firmware chance to fully reinitialize. */ if (rmi_smb->xport.pdata.reset_delay_ms) msleep(rmi_smb->xport.pdata.reset_delay_ms); /* we need to get the smbus version to activate the touchpad */ smbus_version = rmi_smb_get_version(rmi_smb); if (smbus_version < 0) return smbus_version; rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Smbus version is %d", smbus_version); if (smbus_version != 2 && smbus_version != 3) { dev_err(&client->dev, "Unrecognized SMB version %d\n", smbus_version); return -ENODEV; } return 0; } static int rmi_smb_reset(struct rmi_transport_dev *xport, u16 reset_addr) { struct rmi_smb_xport *rmi_smb = container_of(xport, struct rmi_smb_xport, xport); rmi_smb_clear_state(rmi_smb); /* * We do not call the actual reset command, it has to be handled in * PS/2 or there will be races between PS/2 and SMBus. PS/2 should * ensure that a psmouse_reset is called before initializing the * device and after it has been removed to be in a known state. */ return rmi_smb_enable_smbus_mode(rmi_smb); } static const struct rmi_transport_ops rmi_smb_ops = { .write_block = rmi_smb_write_block, .read_block = rmi_smb_read_block, .reset = rmi_smb_reset, }; static int rmi_smb_probe(struct i2c_client *client) { struct rmi_device_platform_data *pdata = dev_get_platdata(&client->dev); struct rmi_smb_xport *rmi_smb; int error; if (!pdata) { dev_err(&client->dev, "no platform data, aborting\n"); return -ENOMEM; } if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_HOST_NOTIFY)) { dev_err(&client->dev, "adapter does not support required functionality\n"); return -ENODEV; } if (client->irq <= 0) { dev_err(&client->dev, "no IRQ provided, giving up\n"); return client->irq ? client->irq : -ENODEV; } rmi_smb = devm_kzalloc(&client->dev, sizeof(struct rmi_smb_xport), GFP_KERNEL); if (!rmi_smb) return -ENOMEM; rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s\n", dev_name(&client->dev)); rmi_smb->client = client; mutex_init(&rmi_smb->page_mutex); mutex_init(&rmi_smb->mappingtable_mutex); rmi_smb->xport.dev = &client->dev; rmi_smb->xport.pdata = *pdata; rmi_smb->xport.pdata.irq = client->irq; rmi_smb->xport.proto_name = "smb"; rmi_smb->xport.ops = &rmi_smb_ops; error = rmi_smb_enable_smbus_mode(rmi_smb); if (error) return error; i2c_set_clientdata(client, rmi_smb); dev_info(&client->dev, "registering SMbus-connected sensor\n"); error = rmi_register_transport_device(&rmi_smb->xport); if (error) { dev_err(&client->dev, "failed to register sensor: %d\n", error); return error; } return 0; } static void rmi_smb_remove(struct i2c_client *client) { struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client); rmi_unregister_transport_device(&rmi_smb->xport); } static int rmi_smb_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client); int ret; ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, true); if (ret) dev_warn(dev, "Failed to suspend device: %d\n", ret); return ret; } static int rmi_smb_runtime_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client); int ret; ret = rmi_driver_suspend(rmi_smb->xport.rmi_dev, false); if (ret) dev_warn(dev, "Failed to suspend device: %d\n", ret); return ret; } static int rmi_smb_resume(struct device *dev) { struct i2c_client *client = container_of(dev, struct i2c_client, dev); struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client); struct rmi_device *rmi_dev = rmi_smb->xport.rmi_dev; int ret; rmi_smb_reset(&rmi_smb->xport, 0); rmi_reset(rmi_dev); ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, true); if (ret) dev_warn(dev, "Failed to resume device: %d\n", ret); return 0; } static int rmi_smb_runtime_resume(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct rmi_smb_xport *rmi_smb = i2c_get_clientdata(client); int ret; ret = rmi_driver_resume(rmi_smb->xport.rmi_dev, false); if (ret) dev_warn(dev, "Failed to resume device: %d\n", ret); return 0; } static const struct dev_pm_ops rmi_smb_pm = { SYSTEM_SLEEP_PM_OPS(rmi_smb_suspend, rmi_smb_resume) RUNTIME_PM_OPS(rmi_smb_runtime_suspend, rmi_smb_runtime_resume, NULL) }; static const struct i2c_device_id rmi_id[] = { { "rmi4_smbus", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, rmi_id); static struct i2c_driver rmi_smb_driver = { .driver = { .name = "rmi4_smbus", .pm = pm_ptr(&rmi_smb_pm), }, .id_table = rmi_id, .probe = rmi_smb_probe, .remove = rmi_smb_remove, }; module_i2c_driver(rmi_smb_driver); MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>"); MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>"); MODULE_DESCRIPTION("RMI4 SMBus driver"); MODULE_LICENSE("GPL"); |