1 /* 2 * Copyright (c) 2011-2016 Synaptics Incorporated 3 * Copyright (c) 2011 Unixphere 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/i2c.h> 11 #include <linux/rmi.h> 12 #include <linux/irq.h> 13 #include <linux/of.h> 14 #include "rmi_driver.h" 15 16 #define BUFFER_SIZE_INCREMENT 32 17 18 /** 19 * struct rmi_i2c_xport - stores information for i2c communication 20 * 21 * @xport: The transport interface structure 22 * 23 * @page_mutex: Locks current page to avoid changing pages in unexpected ways. 24 * @page: Keeps track of the current virtual page 25 * 26 * @tx_buf: Buffer used for transmitting data to the sensor over i2c. 27 * @tx_buf_size: Size of the buffer 28 */ 29 struct rmi_i2c_xport { 30 struct rmi_transport_dev xport; 31 struct i2c_client *client; 32 33 struct mutex page_mutex; 34 int page; 35 36 int irq; 37 38 u8 *tx_buf; 39 size_t tx_buf_size; 40 }; 41 42 #define RMI_PAGE_SELECT_REGISTER 0xff 43 #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff) 44 45 /* 46 * rmi_set_page - Set RMI page 47 * @xport: The pointer to the rmi_transport_dev struct 48 * @page: The new page address. 49 * 50 * RMI devices have 16-bit addressing, but some of the transport 51 * implementations (like SMBus) only have 8-bit addressing. So RMI implements 52 * a page address at 0xff of every page so we can reliable page addresses 53 * every 256 registers. 54 * 55 * The page_mutex lock must be held when this function is entered. 56 * 57 * Returns zero on success, non-zero on failure. 58 */ 59 static int rmi_set_page(struct rmi_i2c_xport *rmi_i2c, u8 page) 60 { 61 struct i2c_client *client = rmi_i2c->client; 62 u8 txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page}; 63 int retval; 64 65 retval = i2c_master_send(client, txbuf, sizeof(txbuf)); 66 if (retval != sizeof(txbuf)) { 67 dev_err(&client->dev, 68 "%s: set page failed: %d.", __func__, retval); 69 return (retval < 0) ? retval : -EIO; 70 } 71 72 rmi_i2c->page = page; 73 return 0; 74 } 75 76 static int rmi_i2c_write_block(struct rmi_transport_dev *xport, u16 addr, 77 const void *buf, size_t len) 78 { 79 struct rmi_i2c_xport *rmi_i2c = 80 container_of(xport, struct rmi_i2c_xport, xport); 81 struct i2c_client *client = rmi_i2c->client; 82 size_t tx_size = len + 1; 83 int retval; 84 85 mutex_lock(&rmi_i2c->page_mutex); 86 87 if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) { 88 if (rmi_i2c->tx_buf) 89 devm_kfree(&client->dev, rmi_i2c->tx_buf); 90 rmi_i2c->tx_buf_size = tx_size + BUFFER_SIZE_INCREMENT; 91 rmi_i2c->tx_buf = devm_kzalloc(&client->dev, 92 rmi_i2c->tx_buf_size, 93 GFP_KERNEL); 94 if (!rmi_i2c->tx_buf) { 95 rmi_i2c->tx_buf_size = 0; 96 retval = -ENOMEM; 97 goto exit; 98 } 99 } 100 101 rmi_i2c->tx_buf[0] = addr & 0xff; 102 memcpy(rmi_i2c->tx_buf + 1, buf, len); 103 104 if (RMI_I2C_PAGE(addr) != rmi_i2c->page) { 105 retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr)); 106 if (retval) 107 goto exit; 108 } 109 110 retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size); 111 if (retval == tx_size) 112 retval = 0; 113 else if (retval >= 0) 114 retval = -EIO; 115 116 exit: 117 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, 118 "write %zd bytes at %#06x: %d (%*ph)\n", 119 len, addr, retval, (int)len, buf); 120 121 mutex_unlock(&rmi_i2c->page_mutex); 122 return retval; 123 } 124 125 static int rmi_i2c_read_block(struct rmi_transport_dev *xport, u16 addr, 126 void *buf, size_t len) 127 { 128 struct rmi_i2c_xport *rmi_i2c = 129 container_of(xport, struct rmi_i2c_xport, xport); 130 struct i2c_client *client = rmi_i2c->client; 131 u8 addr_offset = addr & 0xff; 132 int retval; 133 struct i2c_msg msgs[] = { 134 { 135 .addr = client->addr, 136 .len = sizeof(addr_offset), 137 .buf = &addr_offset, 138 }, 139 { 140 .addr = client->addr, 141 .flags = I2C_M_RD, 142 .len = len, 143 .buf = buf, 144 }, 145 }; 146 147 mutex_lock(&rmi_i2c->page_mutex); 148 149 if (RMI_I2C_PAGE(addr) != rmi_i2c->page) { 150 retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr)); 151 if (retval) 152 goto exit; 153 } 154 155 retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 156 if (retval == ARRAY_SIZE(msgs)) 157 retval = 0; /* success */ 158 else if (retval >= 0) 159 retval = -EIO; 160 161 exit: 162 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, 163 "read %zd bytes at %#06x: %d (%*ph)\n", 164 len, addr, retval, (int)len, buf); 165 166 mutex_unlock(&rmi_i2c->page_mutex); 167 return retval; 168 } 169 170 static const struct rmi_transport_ops rmi_i2c_ops = { 171 .write_block = rmi_i2c_write_block, 172 .read_block = rmi_i2c_read_block, 173 }; 174 175 static irqreturn_t rmi_i2c_irq(int irq, void *dev_id) 176 { 177 struct rmi_i2c_xport *rmi_i2c = dev_id; 178 struct rmi_device *rmi_dev = rmi_i2c->xport.rmi_dev; 179 int ret; 180 181 ret = rmi_process_interrupt_requests(rmi_dev); 182 if (ret) 183 rmi_dbg(RMI_DEBUG_XPORT, &rmi_dev->dev, 184 "Failed to process interrupt request: %d\n", ret); 185 186 return IRQ_HANDLED; 187 } 188 189 static int rmi_i2c_init_irq(struct i2c_client *client) 190 { 191 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 192 int irq_flags = irqd_get_trigger_type(irq_get_irq_data(rmi_i2c->irq)); 193 int ret; 194 195 if (!irq_flags) 196 irq_flags = IRQF_TRIGGER_LOW; 197 198 ret = devm_request_threaded_irq(&client->dev, rmi_i2c->irq, NULL, 199 rmi_i2c_irq, irq_flags | IRQF_ONESHOT, client->name, 200 rmi_i2c); 201 if (ret < 0) { 202 dev_warn(&client->dev, "Failed to register interrupt %d\n", 203 rmi_i2c->irq); 204 205 return ret; 206 } 207 208 return 0; 209 } 210 211 #ifdef CONFIG_OF 212 static const struct of_device_id rmi_i2c_of_match[] = { 213 { .compatible = "syna,rmi4-i2c" }, 214 {}, 215 }; 216 MODULE_DEVICE_TABLE(of, rmi_i2c_of_match); 217 #endif 218 219 static int rmi_i2c_probe(struct i2c_client *client, 220 const struct i2c_device_id *id) 221 { 222 struct rmi_device_platform_data *pdata; 223 struct rmi_device_platform_data *client_pdata = 224 dev_get_platdata(&client->dev); 225 struct rmi_i2c_xport *rmi_i2c; 226 int retval; 227 228 rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport), 229 GFP_KERNEL); 230 if (!rmi_i2c) 231 return -ENOMEM; 232 233 pdata = &rmi_i2c->xport.pdata; 234 235 if (!client->dev.of_node && client_pdata) 236 *pdata = *client_pdata; 237 238 if (client->irq > 0) 239 rmi_i2c->irq = client->irq; 240 241 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s.\n", 242 dev_name(&client->dev)); 243 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 244 dev_err(&client->dev, 245 "adapter does not support required functionality.\n"); 246 return -ENODEV; 247 } 248 249 rmi_i2c->client = client; 250 mutex_init(&rmi_i2c->page_mutex); 251 252 rmi_i2c->xport.dev = &client->dev; 253 rmi_i2c->xport.proto_name = "i2c"; 254 rmi_i2c->xport.ops = &rmi_i2c_ops; 255 256 i2c_set_clientdata(client, rmi_i2c); 257 258 /* 259 * Setting the page to zero will (a) make sure the PSR is in a 260 * known state, and (b) make sure we can talk to the device. 261 */ 262 retval = rmi_set_page(rmi_i2c, 0); 263 if (retval) { 264 dev_err(&client->dev, "Failed to set page select to 0.\n"); 265 return retval; 266 } 267 268 retval = rmi_register_transport_device(&rmi_i2c->xport); 269 if (retval) { 270 dev_err(&client->dev, "Failed to register transport driver at 0x%.2X.\n", 271 client->addr); 272 return retval; 273 } 274 275 retval = rmi_i2c_init_irq(client); 276 if (retval < 0) 277 return retval; 278 279 dev_info(&client->dev, "registered rmi i2c driver at %#04x.\n", 280 client->addr); 281 return 0; 282 } 283 284 static int rmi_i2c_remove(struct i2c_client *client) 285 { 286 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 287 288 rmi_unregister_transport_device(&rmi_i2c->xport); 289 290 return 0; 291 } 292 293 #ifdef CONFIG_PM_SLEEP 294 static int rmi_i2c_suspend(struct device *dev) 295 { 296 struct i2c_client *client = to_i2c_client(dev); 297 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 298 int ret; 299 300 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev); 301 if (ret) 302 dev_warn(dev, "Failed to resume device: %d\n", ret); 303 304 disable_irq(rmi_i2c->irq); 305 if (device_may_wakeup(&client->dev)) { 306 ret = enable_irq_wake(rmi_i2c->irq); 307 if (!ret) 308 dev_warn(dev, "Failed to enable irq for wake: %d\n", 309 ret); 310 } 311 return ret; 312 } 313 314 static int rmi_i2c_resume(struct device *dev) 315 { 316 struct i2c_client *client = to_i2c_client(dev); 317 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 318 int ret; 319 320 enable_irq(rmi_i2c->irq); 321 if (device_may_wakeup(&client->dev)) { 322 ret = disable_irq_wake(rmi_i2c->irq); 323 if (!ret) 324 dev_warn(dev, "Failed to disable irq for wake: %d\n", 325 ret); 326 } 327 328 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev); 329 if (ret) 330 dev_warn(dev, "Failed to resume device: %d\n", ret); 331 332 return ret; 333 } 334 #endif 335 336 #ifdef CONFIG_PM 337 static int rmi_i2c_runtime_suspend(struct device *dev) 338 { 339 struct i2c_client *client = to_i2c_client(dev); 340 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 341 int ret; 342 343 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev); 344 if (ret) 345 dev_warn(dev, "Failed to resume device: %d\n", ret); 346 347 disable_irq(rmi_i2c->irq); 348 349 return 0; 350 } 351 352 static int rmi_i2c_runtime_resume(struct device *dev) 353 { 354 struct i2c_client *client = to_i2c_client(dev); 355 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client); 356 int ret; 357 358 enable_irq(rmi_i2c->irq); 359 360 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev); 361 if (ret) 362 dev_warn(dev, "Failed to resume device: %d\n", ret); 363 364 return 0; 365 } 366 #endif 367 368 static const struct dev_pm_ops rmi_i2c_pm = { 369 SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend, rmi_i2c_resume) 370 SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend, rmi_i2c_runtime_resume, 371 NULL) 372 }; 373 374 static const struct i2c_device_id rmi_id[] = { 375 { "rmi4_i2c", 0 }, 376 { } 377 }; 378 MODULE_DEVICE_TABLE(i2c, rmi_id); 379 380 static struct i2c_driver rmi_i2c_driver = { 381 .driver = { 382 .name = "rmi4_i2c", 383 .pm = &rmi_i2c_pm, 384 .of_match_table = of_match_ptr(rmi_i2c_of_match), 385 }, 386 .id_table = rmi_id, 387 .probe = rmi_i2c_probe, 388 .remove = rmi_i2c_remove, 389 }; 390 391 module_i2c_driver(rmi_i2c_driver); 392 393 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>"); 394 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>"); 395 MODULE_DESCRIPTION("RMI I2C driver"); 396 MODULE_LICENSE("GPL"); 397 MODULE_VERSION(RMI_DRIVER_VERSION); 398