Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: tsc2005 - convert to regmap

Convert driver so that it uses regmap instead of directly using
spi_transfer for all register accesses.

Signed-off-by: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Sebastian Reichel and committed by
Dmitry Torokhov
273cf48a f00d1f8f

+60 -112
+1
drivers/input/touchscreen/Kconfig
··· 917 917 config TOUCHSCREEN_TSC2005 918 918 tristate "TSC2005 based touchscreens" 919 919 depends on SPI_MASTER 920 + select REGMAP_SPI 920 921 help 921 922 Say Y here if you have a TSC2005 based touchscreen. 922 923
+59 -112
drivers/input/touchscreen/tsc2005.c
··· 34 34 #include <linux/spi/spi.h> 35 35 #include <linux/spi/tsc2005.h> 36 36 #include <linux/regulator/consumer.h> 37 + #include <linux/regmap.h> 37 38 38 39 /* 39 40 * The touchscreen interface operates as follows: ··· 121 120 #define TSC2005_SPI_MAX_SPEED_HZ 10000000 122 121 #define TSC2005_PENUP_TIME_MS 40 123 122 124 - struct tsc2005_spi_rd { 125 - struct spi_transfer spi_xfer; 126 - u32 spi_tx; 127 - u32 spi_rx; 123 + static const struct regmap_range tsc2005_writable_ranges[] = { 124 + regmap_reg_range(TSC2005_REG_AUX_HIGH, TSC2005_REG_CFR2), 128 125 }; 126 + 127 + static const struct regmap_access_table tsc2005_writable_table = { 128 + .yes_ranges = tsc2005_writable_ranges, 129 + .n_yes_ranges = ARRAY_SIZE(tsc2005_writable_ranges), 130 + }; 131 + 132 + static struct regmap_config tsc2005_regmap_config = { 133 + .reg_bits = 8, 134 + .val_bits = 16, 135 + .reg_stride = 0x08, 136 + .max_register = 0x78, 137 + .read_flag_mask = TSC2005_REG_READ, 138 + .write_flag_mask = TSC2005_REG_PND0, 139 + .wr_table = &tsc2005_writable_table, 140 + .use_single_rw = true, 141 + }; 142 + 143 + struct tsc2005_data { 144 + u16 x; 145 + u16 y; 146 + u16 z1; 147 + u16 z2; 148 + } __packed; 149 + #define TSC2005_DATA_REGS 4 129 150 130 151 struct tsc2005 { 131 152 struct spi_device *spi; 132 - 133 - struct spi_message spi_read_msg; 134 - struct tsc2005_spi_rd spi_x; 135 - struct tsc2005_spi_rd spi_y; 136 - struct tsc2005_spi_rd spi_z1; 137 - struct tsc2005_spi_rd spi_z2; 153 + struct regmap *regmap; 138 154 139 155 struct input_dev *idev; 140 156 char phys[32]; ··· 208 190 return 0; 209 191 } 210 192 211 - static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value) 212 - { 213 - u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value; 214 - struct spi_transfer xfer = { 215 - .tx_buf = &tx, 216 - .len = 4, 217 - .bits_per_word = 24, 218 - }; 219 - struct spi_message msg; 220 - int error; 221 - 222 - spi_message_init(&msg); 223 - spi_message_add_tail(&xfer, &msg); 224 - 225 - error = spi_sync(ts->spi, &msg); 226 - if (error) { 227 - dev_err(&ts->spi->dev, 228 - "%s: failed, register: %x, value: %x, error: %d\n", 229 - __func__, reg, value, error); 230 - return error; 231 - } 232 - 233 - return 0; 234 - } 235 - 236 - static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last) 237 - { 238 - memset(rd, 0, sizeof(*rd)); 239 - 240 - rd->spi_tx = (reg | TSC2005_REG_READ) << 16; 241 - rd->spi_xfer.tx_buf = &rd->spi_tx; 242 - rd->spi_xfer.rx_buf = &rd->spi_rx; 243 - rd->spi_xfer.len = 4; 244 - rd->spi_xfer.bits_per_word = 24; 245 - rd->spi_xfer.cs_change = !last; 246 - } 247 - 248 - static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value) 249 - { 250 - struct tsc2005_spi_rd spi_rd; 251 - struct spi_message msg; 252 - int error; 253 - 254 - tsc2005_setup_read(&spi_rd, reg, true); 255 - 256 - spi_message_init(&msg); 257 - spi_message_add_tail(&spi_rd.spi_xfer, &msg); 258 - 259 - error = spi_sync(ts->spi, &msg); 260 - if (error) 261 - return error; 262 - 263 - *value = spi_rd.spi_rx; 264 - return 0; 265 - } 266 - 267 193 static void tsc2005_update_pen_state(struct tsc2005 *ts, 268 194 int x, int y, int pressure) 269 195 { ··· 236 274 struct tsc2005 *ts = _ts; 237 275 unsigned long flags; 238 276 unsigned int pressure; 239 - u32 x, y; 240 - u32 z1, z2; 277 + struct tsc2005_data tsdata; 241 278 int error; 242 279 243 280 /* read the coordinates */ 244 - error = spi_sync(ts->spi, &ts->spi_read_msg); 281 + error = regmap_bulk_read(ts->regmap, TSC2005_REG_X, &tsdata, 282 + TSC2005_DATA_REGS); 245 283 if (unlikely(error)) 246 284 goto out; 247 285 248 - x = ts->spi_x.spi_rx; 249 - y = ts->spi_y.spi_rx; 250 - z1 = ts->spi_z1.spi_rx; 251 - z2 = ts->spi_z2.spi_rx; 252 - 253 286 /* validate position */ 254 - if (unlikely(x > MAX_12BIT || y > MAX_12BIT)) 287 + if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT)) 255 288 goto out; 256 289 257 290 /* Skip reading if the pressure components are out of range */ 258 - if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2)) 291 + if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT)) 292 + goto out; 293 + if (unlikely(tsdata.z1 >= tsdata.z2)) 259 294 goto out; 260 295 261 296 /* ··· 260 301 * the value before pen-up - that implies SPI fed us stale data 261 302 */ 262 303 if (!ts->pen_down && 263 - ts->in_x == x && ts->in_y == y && 264 - ts->in_z1 == z1 && ts->in_z2 == z2) { 304 + ts->in_x == tsdata.x && ts->in_y == tsdata.y && 305 + ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) { 265 306 goto out; 266 307 } 267 308 ··· 269 310 * At this point we are happy we have a valid and useful reading. 270 311 * Remember it for later comparisons. We may now begin downsampling. 271 312 */ 272 - ts->in_x = x; 273 - ts->in_y = y; 274 - ts->in_z1 = z1; 275 - ts->in_z2 = z2; 313 + ts->in_x = tsdata.x; 314 + ts->in_y = tsdata.y; 315 + ts->in_z1 = tsdata.z1; 316 + ts->in_z2 = tsdata.z2; 276 317 277 318 /* Compute touch pressure resistance using equation #1 */ 278 - pressure = x * (z2 - z1) / z1; 319 + pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1; 279 320 pressure = pressure * ts->x_plate_ohm / 4096; 280 321 if (unlikely(pressure > MAX_12BIT)) 281 322 goto out; 282 323 283 324 spin_lock_irqsave(&ts->lock, flags); 284 325 285 - tsc2005_update_pen_state(ts, x, y, pressure); 326 + tsc2005_update_pen_state(ts, tsdata.x, tsdata.y, pressure); 286 327 mod_timer(&ts->penup_timer, 287 328 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); 288 329 ··· 305 346 306 347 static void tsc2005_start_scan(struct tsc2005 *ts) 307 348 { 308 - tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE); 309 - tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE); 310 - tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE); 349 + regmap_write(ts->regmap, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE); 350 + regmap_write(ts->regmap, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE); 351 + regmap_write(ts->regmap, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE); 311 352 tsc2005_cmd(ts, TSC2005_CMD_NORMAL); 312 353 } 313 354 ··· 357 398 { 358 399 struct spi_device *spi = to_spi_device(dev); 359 400 struct tsc2005 *ts = spi_get_drvdata(spi); 360 - u16 temp_high; 361 - u16 temp_high_orig; 362 - u16 temp_high_test; 401 + unsigned int temp_high; 402 + unsigned int temp_high_orig; 403 + unsigned int temp_high_test; 363 404 bool success = true; 364 405 int error; 365 406 ··· 370 411 */ 371 412 __tsc2005_disable(ts); 372 413 373 - error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig); 414 + error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high_orig); 374 415 if (error) { 375 416 dev_warn(dev, "selftest failed: read error %d\n", error); 376 417 success = false; ··· 379 420 380 421 temp_high_test = (temp_high_orig - 1) & MAX_12BIT; 381 422 382 - error = tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test); 423 + error = regmap_write(ts->regmap, TSC2005_REG_TEMP_HIGH, temp_high_test); 383 424 if (error) { 384 425 dev_warn(dev, "selftest failed: write error %d\n", error); 385 426 success = false; 386 427 goto out; 387 428 } 388 429 389 - error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); 430 + error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high); 390 431 if (error) { 391 432 dev_warn(dev, "selftest failed: read error %d after write\n", 392 433 error); ··· 409 450 goto out; 410 451 411 452 /* test that the reset really happened */ 412 - error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); 453 + error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high); 413 454 if (error) { 414 455 dev_warn(dev, "selftest failed: read error %d after reset\n", 415 456 error); ··· 462 503 { 463 504 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work); 464 505 int error; 465 - u16 r; 506 + unsigned int r; 466 507 467 508 if (!mutex_trylock(&ts->mutex)) { 468 509 /* ··· 478 519 goto out; 479 520 480 521 /* We should be able to read register without disabling interrupts. */ 481 - error = tsc2005_read(ts, TSC2005_REG_CFR0, &r); 522 + error = regmap_read(ts->regmap, TSC2005_REG_CFR0, &r); 482 523 if (!error && 483 524 !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) { 484 525 goto out; ··· 540 581 ts->opened = false; 541 582 542 583 mutex_unlock(&ts->mutex); 543 - } 544 - 545 - static void tsc2005_setup_spi_xfer(struct tsc2005 *ts) 546 - { 547 - tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false); 548 - tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false); 549 - tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false); 550 - tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true); 551 - 552 - spi_message_init(&ts->spi_read_msg); 553 - spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg); 554 - spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg); 555 - spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg); 556 - spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg); 557 584 } 558 585 559 586 static int tsc2005_probe(struct spi_device *spi) ··· 606 661 ts->spi = spi; 607 662 ts->idev = input_dev; 608 663 664 + ts->regmap = devm_regmap_init_spi(spi, &tsc2005_regmap_config); 665 + if (IS_ERR(ts->regmap)) 666 + return PTR_ERR(ts->regmap); 667 + 609 668 ts->x_plate_ohm = x_plate_ohm; 610 669 ts->esd_timeout = esd_timeout; 611 670 ··· 648 699 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); 649 700 650 701 INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work); 651 - 652 - tsc2005_setup_spi_xfer(ts); 653 702 654 703 snprintf(ts->phys, sizeof(ts->phys), 655 704 "%s/input-ts", dev_name(&spi->dev));