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