1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Azoteq IQS269A Capacitive Touch Controller 4 * 5 * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com> 6 * 7 * This driver registers up to 3 input devices: one representing capacitive or 8 * inductive keys as well as Hall-effect switches, and one for each of the two 9 * axial sliders presented by the device. 10 */ 11 12 #include <linux/delay.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/i2c.h> 16 #include <linux/input.h> 17 #include <linux/interrupt.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/mutex.h> 21 #include <linux/of_device.h> 22 #include <linux/property.h> 23 #include <linux/regmap.h> 24 #include <linux/slab.h> 25 26 #define IQS269_VER_INFO 0x00 27 #define IQS269_VER_INFO_PROD_NUM 0x4F 28 29 #define IQS269_SYS_FLAGS 0x02 30 #define IQS269_SYS_FLAGS_SHOW_RESET BIT(15) 31 #define IQS269_SYS_FLAGS_PWR_MODE_MASK GENMASK(12, 11) 32 #define IQS269_SYS_FLAGS_PWR_MODE_SHIFT 11 33 #define IQS269_SYS_FLAGS_IN_ATI BIT(10) 34 35 #define IQS269_CHx_COUNTS 0x08 36 37 #define IQS269_SLIDER_X 0x30 38 39 #define IQS269_CAL_DATA_A 0x35 40 #define IQS269_CAL_DATA_A_HALL_BIN_L_MASK GENMASK(15, 12) 41 #define IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT 12 42 #define IQS269_CAL_DATA_A_HALL_BIN_R_MASK GENMASK(11, 8) 43 #define IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT 8 44 45 #define IQS269_SYS_SETTINGS 0x80 46 #define IQS269_SYS_SETTINGS_CLK_DIV BIT(15) 47 #define IQS269_SYS_SETTINGS_ULP_AUTO BIT(14) 48 #define IQS269_SYS_SETTINGS_DIS_AUTO BIT(13) 49 #define IQS269_SYS_SETTINGS_PWR_MODE_MASK GENMASK(12, 11) 50 #define IQS269_SYS_SETTINGS_PWR_MODE_SHIFT 11 51 #define IQS269_SYS_SETTINGS_PWR_MODE_MAX 3 52 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MASK GENMASK(10, 8) 53 #define IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT 8 54 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MAX 7 55 #define IQS269_SYS_SETTINGS_RESEED_OFFSET BIT(6) 56 #define IQS269_SYS_SETTINGS_EVENT_MODE BIT(5) 57 #define IQS269_SYS_SETTINGS_EVENT_MODE_LP BIT(4) 58 #define IQS269_SYS_SETTINGS_REDO_ATI BIT(2) 59 #define IQS269_SYS_SETTINGS_ACK_RESET BIT(0) 60 61 #define IQS269_FILT_STR_LP_LTA_MASK GENMASK(7, 6) 62 #define IQS269_FILT_STR_LP_LTA_SHIFT 6 63 #define IQS269_FILT_STR_LP_CNT_MASK GENMASK(5, 4) 64 #define IQS269_FILT_STR_LP_CNT_SHIFT 4 65 #define IQS269_FILT_STR_NP_LTA_MASK GENMASK(3, 2) 66 #define IQS269_FILT_STR_NP_LTA_SHIFT 2 67 #define IQS269_FILT_STR_NP_CNT_MASK GENMASK(1, 0) 68 #define IQS269_FILT_STR_MAX 3 69 70 #define IQS269_EVENT_MASK_SYS BIT(6) 71 #define IQS269_EVENT_MASK_DEEP BIT(2) 72 #define IQS269_EVENT_MASK_TOUCH BIT(1) 73 #define IQS269_EVENT_MASK_PROX BIT(0) 74 75 #define IQS269_RATE_NP_MS_MAX 255 76 #define IQS269_RATE_LP_MS_MAX 255 77 #define IQS269_RATE_ULP_MS_MAX 4080 78 #define IQS269_TIMEOUT_PWR_MS_MAX 130560 79 #define IQS269_TIMEOUT_LTA_MS_MAX 130560 80 81 #define IQS269_MISC_A_ATI_BAND_DISABLE BIT(15) 82 #define IQS269_MISC_A_ATI_LP_ONLY BIT(14) 83 #define IQS269_MISC_A_ATI_BAND_TIGHTEN BIT(13) 84 #define IQS269_MISC_A_FILT_DISABLE BIT(12) 85 #define IQS269_MISC_A_GPIO3_SELECT_MASK GENMASK(10, 8) 86 #define IQS269_MISC_A_GPIO3_SELECT_SHIFT 8 87 #define IQS269_MISC_A_DUAL_DIR BIT(6) 88 #define IQS269_MISC_A_TX_FREQ_MASK GENMASK(5, 4) 89 #define IQS269_MISC_A_TX_FREQ_SHIFT 4 90 #define IQS269_MISC_A_TX_FREQ_MAX 3 91 #define IQS269_MISC_A_GLOBAL_CAP_SIZE BIT(0) 92 93 #define IQS269_MISC_B_RESEED_UI_SEL_MASK GENMASK(7, 6) 94 #define IQS269_MISC_B_RESEED_UI_SEL_SHIFT 6 95 #define IQS269_MISC_B_RESEED_UI_SEL_MAX 3 96 #define IQS269_MISC_B_TRACKING_UI_ENABLE BIT(4) 97 #define IQS269_MISC_B_FILT_STR_SLIDER GENMASK(1, 0) 98 99 #define IQS269_CHx_SETTINGS 0x8C 100 101 #define IQS269_CHx_ENG_A_MEAS_CAP_SIZE BIT(15) 102 #define IQS269_CHx_ENG_A_RX_GND_INACTIVE BIT(13) 103 #define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE BIT(12) 104 #define IQS269_CHx_ENG_A_ATI_MODE_MASK GENMASK(9, 8) 105 #define IQS269_CHx_ENG_A_ATI_MODE_SHIFT 8 106 #define IQS269_CHx_ENG_A_ATI_MODE_MAX 3 107 #define IQS269_CHx_ENG_A_INV_LOGIC BIT(7) 108 #define IQS269_CHx_ENG_A_PROJ_BIAS_MASK GENMASK(6, 5) 109 #define IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT 5 110 #define IQS269_CHx_ENG_A_PROJ_BIAS_MAX 3 111 #define IQS269_CHx_ENG_A_SENSE_MODE_MASK GENMASK(3, 0) 112 #define IQS269_CHx_ENG_A_SENSE_MODE_MAX 15 113 114 #define IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE BIT(13) 115 #define IQS269_CHx_ENG_B_SENSE_FREQ_MASK GENMASK(10, 9) 116 #define IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT 9 117 #define IQS269_CHx_ENG_B_SENSE_FREQ_MAX 3 118 #define IQS269_CHx_ENG_B_STATIC_ENABLE BIT(8) 119 #define IQS269_CHx_ENG_B_ATI_BASE_MASK GENMASK(7, 6) 120 #define IQS269_CHx_ENG_B_ATI_BASE_75 0x00 121 #define IQS269_CHx_ENG_B_ATI_BASE_100 0x40 122 #define IQS269_CHx_ENG_B_ATI_BASE_150 0x80 123 #define IQS269_CHx_ENG_B_ATI_BASE_200 0xC0 124 #define IQS269_CHx_ENG_B_ATI_TARGET_MASK GENMASK(5, 0) 125 #define IQS269_CHx_ENG_B_ATI_TARGET_MAX 2016 126 127 #define IQS269_CHx_WEIGHT_MAX 255 128 #define IQS269_CHx_THRESH_MAX 255 129 #define IQS269_CHx_HYST_DEEP_MASK GENMASK(7, 4) 130 #define IQS269_CHx_HYST_DEEP_SHIFT 4 131 #define IQS269_CHx_HYST_TOUCH_MASK GENMASK(3, 0) 132 #define IQS269_CHx_HYST_MAX 15 133 134 #define IQS269_CHx_HALL_INACTIVE 6 135 #define IQS269_CHx_HALL_ACTIVE 7 136 137 #define IQS269_HALL_PAD_R BIT(0) 138 #define IQS269_HALL_PAD_L BIT(1) 139 #define IQS269_HALL_PAD_INV BIT(6) 140 141 #define IQS269_HALL_UI 0xF5 142 #define IQS269_HALL_UI_ENABLE BIT(15) 143 144 #define IQS269_MAX_REG 0xFF 145 146 #define IQS269_NUM_CH 8 147 #define IQS269_NUM_SL 2 148 149 #define IQS269_ATI_POLL_SLEEP_US (iqs269->delay_mult * 10000) 150 #define IQS269_ATI_POLL_TIMEOUT_US (iqs269->delay_mult * 500000) 151 #define IQS269_ATI_STABLE_DELAY_MS (iqs269->delay_mult * 150) 152 153 #define IQS269_PWR_MODE_POLL_SLEEP_US IQS269_ATI_POLL_SLEEP_US 154 #define IQS269_PWR_MODE_POLL_TIMEOUT_US IQS269_ATI_POLL_TIMEOUT_US 155 156 #define iqs269_irq_wait() usleep_range(100, 150) 157 158 enum iqs269_local_cap_size { 159 IQS269_LOCAL_CAP_SIZE_0, 160 IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY, 161 IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5, 162 }; 163 164 enum iqs269_st_offs { 165 IQS269_ST_OFFS_PROX, 166 IQS269_ST_OFFS_DIR, 167 IQS269_ST_OFFS_TOUCH, 168 IQS269_ST_OFFS_DEEP, 169 }; 170 171 enum iqs269_th_offs { 172 IQS269_TH_OFFS_PROX, 173 IQS269_TH_OFFS_TOUCH, 174 IQS269_TH_OFFS_DEEP, 175 }; 176 177 enum iqs269_event_id { 178 IQS269_EVENT_PROX_DN, 179 IQS269_EVENT_PROX_UP, 180 IQS269_EVENT_TOUCH_DN, 181 IQS269_EVENT_TOUCH_UP, 182 IQS269_EVENT_DEEP_DN, 183 IQS269_EVENT_DEEP_UP, 184 }; 185 186 struct iqs269_switch_desc { 187 unsigned int code; 188 bool enabled; 189 }; 190 191 struct iqs269_event_desc { 192 const char *name; 193 enum iqs269_st_offs st_offs; 194 enum iqs269_th_offs th_offs; 195 bool dir_up; 196 u8 mask; 197 }; 198 199 static const struct iqs269_event_desc iqs269_events[] = { 200 [IQS269_EVENT_PROX_DN] = { 201 .name = "event-prox", 202 .st_offs = IQS269_ST_OFFS_PROX, 203 .th_offs = IQS269_TH_OFFS_PROX, 204 .mask = IQS269_EVENT_MASK_PROX, 205 }, 206 [IQS269_EVENT_PROX_UP] = { 207 .name = "event-prox-alt", 208 .st_offs = IQS269_ST_OFFS_PROX, 209 .th_offs = IQS269_TH_OFFS_PROX, 210 .dir_up = true, 211 .mask = IQS269_EVENT_MASK_PROX, 212 }, 213 [IQS269_EVENT_TOUCH_DN] = { 214 .name = "event-touch", 215 .st_offs = IQS269_ST_OFFS_TOUCH, 216 .th_offs = IQS269_TH_OFFS_TOUCH, 217 .mask = IQS269_EVENT_MASK_TOUCH, 218 }, 219 [IQS269_EVENT_TOUCH_UP] = { 220 .name = "event-touch-alt", 221 .st_offs = IQS269_ST_OFFS_TOUCH, 222 .th_offs = IQS269_TH_OFFS_TOUCH, 223 .dir_up = true, 224 .mask = IQS269_EVENT_MASK_TOUCH, 225 }, 226 [IQS269_EVENT_DEEP_DN] = { 227 .name = "event-deep", 228 .st_offs = IQS269_ST_OFFS_DEEP, 229 .th_offs = IQS269_TH_OFFS_DEEP, 230 .mask = IQS269_EVENT_MASK_DEEP, 231 }, 232 [IQS269_EVENT_DEEP_UP] = { 233 .name = "event-deep-alt", 234 .st_offs = IQS269_ST_OFFS_DEEP, 235 .th_offs = IQS269_TH_OFFS_DEEP, 236 .dir_up = true, 237 .mask = IQS269_EVENT_MASK_DEEP, 238 }, 239 }; 240 241 struct iqs269_ver_info { 242 u8 prod_num; 243 u8 sw_num; 244 u8 hw_num; 245 u8 padding; 246 } __packed; 247 248 struct iqs269_sys_reg { 249 __be16 general; 250 u8 active; 251 u8 filter; 252 u8 reseed; 253 u8 event_mask; 254 u8 rate_np; 255 u8 rate_lp; 256 u8 rate_ulp; 257 u8 timeout_pwr; 258 u8 timeout_rdy; 259 u8 timeout_lta; 260 __be16 misc_a; 261 __be16 misc_b; 262 u8 blocking; 263 u8 padding; 264 u8 slider_select[IQS269_NUM_SL]; 265 u8 timeout_tap; 266 u8 timeout_swipe; 267 u8 thresh_swipe; 268 u8 redo_ati; 269 } __packed; 270 271 struct iqs269_ch_reg { 272 u8 rx_enable; 273 u8 tx_enable; 274 __be16 engine_a; 275 __be16 engine_b; 276 __be16 ati_comp; 277 u8 thresh[3]; 278 u8 hyst; 279 u8 assoc_select; 280 u8 assoc_weight; 281 } __packed; 282 283 struct iqs269_flags { 284 __be16 system; 285 u8 gesture; 286 u8 padding; 287 u8 states[4]; 288 } __packed; 289 290 struct iqs269_private { 291 struct i2c_client *client; 292 struct regmap *regmap; 293 struct mutex lock; 294 struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)]; 295 struct iqs269_ch_reg ch_reg[IQS269_NUM_CH]; 296 struct iqs269_sys_reg sys_reg; 297 struct input_dev *keypad; 298 struct input_dev *slider[IQS269_NUM_SL]; 299 unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH]; 300 unsigned int suspend_mode; 301 unsigned int delay_mult; 302 unsigned int ch_num; 303 bool hall_enable; 304 bool ati_current; 305 }; 306 307 static int iqs269_ati_mode_set(struct iqs269_private *iqs269, 308 unsigned int ch_num, unsigned int mode) 309 { 310 u16 engine_a; 311 312 if (ch_num >= IQS269_NUM_CH) 313 return -EINVAL; 314 315 if (mode > IQS269_CHx_ENG_A_ATI_MODE_MAX) 316 return -EINVAL; 317 318 mutex_lock(&iqs269->lock); 319 320 engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a); 321 322 engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK; 323 engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT); 324 325 iqs269->ch_reg[ch_num].engine_a = cpu_to_be16(engine_a); 326 iqs269->ati_current = false; 327 328 mutex_unlock(&iqs269->lock); 329 330 return 0; 331 } 332 333 static int iqs269_ati_mode_get(struct iqs269_private *iqs269, 334 unsigned int ch_num, unsigned int *mode) 335 { 336 u16 engine_a; 337 338 if (ch_num >= IQS269_NUM_CH) 339 return -EINVAL; 340 341 mutex_lock(&iqs269->lock); 342 engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a); 343 mutex_unlock(&iqs269->lock); 344 345 engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK; 346 *mode = (engine_a >> IQS269_CHx_ENG_A_ATI_MODE_SHIFT); 347 348 return 0; 349 } 350 351 static int iqs269_ati_base_set(struct iqs269_private *iqs269, 352 unsigned int ch_num, unsigned int base) 353 { 354 u16 engine_b; 355 356 if (ch_num >= IQS269_NUM_CH) 357 return -EINVAL; 358 359 switch (base) { 360 case 75: 361 base = IQS269_CHx_ENG_B_ATI_BASE_75; 362 break; 363 364 case 100: 365 base = IQS269_CHx_ENG_B_ATI_BASE_100; 366 break; 367 368 case 150: 369 base = IQS269_CHx_ENG_B_ATI_BASE_150; 370 break; 371 372 case 200: 373 base = IQS269_CHx_ENG_B_ATI_BASE_200; 374 break; 375 376 default: 377 return -EINVAL; 378 } 379 380 mutex_lock(&iqs269->lock); 381 382 engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b); 383 384 engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK; 385 engine_b |= base; 386 387 iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b); 388 iqs269->ati_current = false; 389 390 mutex_unlock(&iqs269->lock); 391 392 return 0; 393 } 394 395 static int iqs269_ati_base_get(struct iqs269_private *iqs269, 396 unsigned int ch_num, unsigned int *base) 397 { 398 u16 engine_b; 399 400 if (ch_num >= IQS269_NUM_CH) 401 return -EINVAL; 402 403 mutex_lock(&iqs269->lock); 404 engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b); 405 mutex_unlock(&iqs269->lock); 406 407 switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) { 408 case IQS269_CHx_ENG_B_ATI_BASE_75: 409 *base = 75; 410 return 0; 411 412 case IQS269_CHx_ENG_B_ATI_BASE_100: 413 *base = 100; 414 return 0; 415 416 case IQS269_CHx_ENG_B_ATI_BASE_150: 417 *base = 150; 418 return 0; 419 420 case IQS269_CHx_ENG_B_ATI_BASE_200: 421 *base = 200; 422 return 0; 423 424 default: 425 return -EINVAL; 426 } 427 } 428 429 static int iqs269_ati_target_set(struct iqs269_private *iqs269, 430 unsigned int ch_num, unsigned int target) 431 { 432 u16 engine_b; 433 434 if (ch_num >= IQS269_NUM_CH) 435 return -EINVAL; 436 437 if (target > IQS269_CHx_ENG_B_ATI_TARGET_MAX) 438 return -EINVAL; 439 440 mutex_lock(&iqs269->lock); 441 442 engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b); 443 444 engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK; 445 engine_b |= target / 32; 446 447 iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b); 448 iqs269->ati_current = false; 449 450 mutex_unlock(&iqs269->lock); 451 452 return 0; 453 } 454 455 static int iqs269_ati_target_get(struct iqs269_private *iqs269, 456 unsigned int ch_num, unsigned int *target) 457 { 458 u16 engine_b; 459 460 if (ch_num >= IQS269_NUM_CH) 461 return -EINVAL; 462 463 mutex_lock(&iqs269->lock); 464 engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b); 465 mutex_unlock(&iqs269->lock); 466 467 *target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32; 468 469 return 0; 470 } 471 472 static int iqs269_parse_mask(const struct fwnode_handle *fwnode, 473 const char *propname, u8 *mask) 474 { 475 unsigned int val[IQS269_NUM_CH]; 476 int count, error, i; 477 478 count = fwnode_property_count_u32(fwnode, propname); 479 if (count < 0) 480 return 0; 481 482 if (count > IQS269_NUM_CH) 483 return -EINVAL; 484 485 error = fwnode_property_read_u32_array(fwnode, propname, val, count); 486 if (error) 487 return error; 488 489 *mask = 0; 490 491 for (i = 0; i < count; i++) { 492 if (val[i] >= IQS269_NUM_CH) 493 return -EINVAL; 494 495 *mask |= BIT(val[i]); 496 } 497 498 return 0; 499 } 500 501 static int iqs269_parse_chan(struct iqs269_private *iqs269, 502 const struct fwnode_handle *ch_node) 503 { 504 struct i2c_client *client = iqs269->client; 505 struct fwnode_handle *ev_node; 506 struct iqs269_ch_reg *ch_reg; 507 u16 engine_a, engine_b; 508 unsigned int reg, val; 509 int error, i; 510 511 error = fwnode_property_read_u32(ch_node, "reg", ®); 512 if (error) { 513 dev_err(&client->dev, "Failed to read channel number: %d\n", 514 error); 515 return error; 516 } else if (reg >= IQS269_NUM_CH) { 517 dev_err(&client->dev, "Invalid channel number: %u\n", reg); 518 return -EINVAL; 519 } 520 521 iqs269->sys_reg.active |= BIT(reg); 522 if (!fwnode_property_present(ch_node, "azoteq,reseed-disable")) 523 iqs269->sys_reg.reseed |= BIT(reg); 524 525 if (fwnode_property_present(ch_node, "azoteq,blocking-enable")) 526 iqs269->sys_reg.blocking |= BIT(reg); 527 528 if (fwnode_property_present(ch_node, "azoteq,slider0-select")) 529 iqs269->sys_reg.slider_select[0] |= BIT(reg); 530 531 if (fwnode_property_present(ch_node, "azoteq,slider1-select")) 532 iqs269->sys_reg.slider_select[1] |= BIT(reg); 533 534 ch_reg = &iqs269->ch_reg[reg]; 535 536 error = regmap_raw_read(iqs269->regmap, 537 IQS269_CHx_SETTINGS + reg * sizeof(*ch_reg) / 2, 538 ch_reg, sizeof(*ch_reg)); 539 if (error) 540 return error; 541 542 error = iqs269_parse_mask(ch_node, "azoteq,rx-enable", 543 &ch_reg->rx_enable); 544 if (error) { 545 dev_err(&client->dev, "Invalid channel %u RX enable mask: %d\n", 546 reg, error); 547 return error; 548 } 549 550 error = iqs269_parse_mask(ch_node, "azoteq,tx-enable", 551 &ch_reg->tx_enable); 552 if (error) { 553 dev_err(&client->dev, "Invalid channel %u TX enable mask: %d\n", 554 reg, error); 555 return error; 556 } 557 558 engine_a = be16_to_cpu(ch_reg->engine_a); 559 engine_b = be16_to_cpu(ch_reg->engine_b); 560 561 engine_a |= IQS269_CHx_ENG_A_MEAS_CAP_SIZE; 562 if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease")) 563 engine_a &= ~IQS269_CHx_ENG_A_MEAS_CAP_SIZE; 564 565 engine_a |= IQS269_CHx_ENG_A_RX_GND_INACTIVE; 566 if (fwnode_property_present(ch_node, "azoteq,rx-float-inactive")) 567 engine_a &= ~IQS269_CHx_ENG_A_RX_GND_INACTIVE; 568 569 engine_a &= ~IQS269_CHx_ENG_A_LOCAL_CAP_SIZE; 570 engine_b &= ~IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE; 571 if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size", &val)) { 572 switch (val) { 573 case IQS269_LOCAL_CAP_SIZE_0: 574 break; 575 576 case IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5: 577 engine_a |= IQS269_CHx_ENG_A_LOCAL_CAP_SIZE; 578 fallthrough; 579 580 case IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY: 581 engine_b |= IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE; 582 break; 583 584 default: 585 dev_err(&client->dev, 586 "Invalid channel %u local cap. size: %u\n", reg, 587 val); 588 return -EINVAL; 589 } 590 } 591 592 engine_a &= ~IQS269_CHx_ENG_A_INV_LOGIC; 593 if (fwnode_property_present(ch_node, "azoteq,invert-enable")) 594 engine_a |= IQS269_CHx_ENG_A_INV_LOGIC; 595 596 if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) { 597 if (val > IQS269_CHx_ENG_A_PROJ_BIAS_MAX) { 598 dev_err(&client->dev, 599 "Invalid channel %u bias current: %u\n", reg, 600 val); 601 return -EINVAL; 602 } 603 604 engine_a &= ~IQS269_CHx_ENG_A_PROJ_BIAS_MASK; 605 engine_a |= (val << IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT); 606 } 607 608 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) { 609 if (val > IQS269_CHx_ENG_A_SENSE_MODE_MAX) { 610 dev_err(&client->dev, 611 "Invalid channel %u sensing mode: %u\n", reg, 612 val); 613 return -EINVAL; 614 } 615 616 engine_a &= ~IQS269_CHx_ENG_A_SENSE_MODE_MASK; 617 engine_a |= val; 618 } 619 620 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) { 621 if (val > IQS269_CHx_ENG_B_SENSE_FREQ_MAX) { 622 dev_err(&client->dev, 623 "Invalid channel %u sensing frequency: %u\n", 624 reg, val); 625 return -EINVAL; 626 } 627 628 engine_b &= ~IQS269_CHx_ENG_B_SENSE_FREQ_MASK; 629 engine_b |= (val << IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT); 630 } 631 632 engine_b &= ~IQS269_CHx_ENG_B_STATIC_ENABLE; 633 if (fwnode_property_present(ch_node, "azoteq,static-enable")) 634 engine_b |= IQS269_CHx_ENG_B_STATIC_ENABLE; 635 636 ch_reg->engine_a = cpu_to_be16(engine_a); 637 ch_reg->engine_b = cpu_to_be16(engine_b); 638 639 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) { 640 error = iqs269_ati_mode_set(iqs269, reg, val); 641 if (error) { 642 dev_err(&client->dev, 643 "Invalid channel %u ATI mode: %u\n", reg, val); 644 return error; 645 } 646 } 647 648 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) { 649 error = iqs269_ati_base_set(iqs269, reg, val); 650 if (error) { 651 dev_err(&client->dev, 652 "Invalid channel %u ATI base: %u\n", reg, val); 653 return error; 654 } 655 } 656 657 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) { 658 error = iqs269_ati_target_set(iqs269, reg, val); 659 if (error) { 660 dev_err(&client->dev, 661 "Invalid channel %u ATI target: %u\n", reg, 662 val); 663 return error; 664 } 665 } 666 667 error = iqs269_parse_mask(ch_node, "azoteq,assoc-select", 668 &ch_reg->assoc_select); 669 if (error) { 670 dev_err(&client->dev, "Invalid channel %u association: %d\n", 671 reg, error); 672 return error; 673 } 674 675 if (!fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val)) { 676 if (val > IQS269_CHx_WEIGHT_MAX) { 677 dev_err(&client->dev, 678 "Invalid channel %u associated weight: %u\n", 679 reg, val); 680 return -EINVAL; 681 } 682 683 ch_reg->assoc_weight = val; 684 } 685 686 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) { 687 ev_node = fwnode_get_named_child_node(ch_node, 688 iqs269_events[i].name); 689 if (!ev_node) 690 continue; 691 692 if (!fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) { 693 if (val > IQS269_CHx_THRESH_MAX) { 694 dev_err(&client->dev, 695 "Invalid channel %u threshold: %u\n", 696 reg, val); 697 fwnode_handle_put(ev_node); 698 return -EINVAL; 699 } 700 701 ch_reg->thresh[iqs269_events[i].th_offs] = val; 702 } 703 704 if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) { 705 u8 *hyst = &ch_reg->hyst; 706 707 if (val > IQS269_CHx_HYST_MAX) { 708 dev_err(&client->dev, 709 "Invalid channel %u hysteresis: %u\n", 710 reg, val); 711 fwnode_handle_put(ev_node); 712 return -EINVAL; 713 } 714 715 if (i == IQS269_EVENT_DEEP_DN || 716 i == IQS269_EVENT_DEEP_UP) { 717 *hyst &= ~IQS269_CHx_HYST_DEEP_MASK; 718 *hyst |= (val << IQS269_CHx_HYST_DEEP_SHIFT); 719 } else if (i == IQS269_EVENT_TOUCH_DN || 720 i == IQS269_EVENT_TOUCH_UP) { 721 *hyst &= ~IQS269_CHx_HYST_TOUCH_MASK; 722 *hyst |= val; 723 } 724 } 725 726 error = fwnode_property_read_u32(ev_node, "linux,code", &val); 727 fwnode_handle_put(ev_node); 728 if (error == -EINVAL) { 729 continue; 730 } else if (error) { 731 dev_err(&client->dev, 732 "Failed to read channel %u code: %d\n", reg, 733 error); 734 return error; 735 } 736 737 switch (reg) { 738 case IQS269_CHx_HALL_ACTIVE: 739 if (iqs269->hall_enable) { 740 iqs269->switches[i].code = val; 741 iqs269->switches[i].enabled = true; 742 } 743 fallthrough; 744 745 case IQS269_CHx_HALL_INACTIVE: 746 if (iqs269->hall_enable) 747 break; 748 fallthrough; 749 750 default: 751 iqs269->keycode[i * IQS269_NUM_CH + reg] = val; 752 } 753 754 iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask; 755 } 756 757 return 0; 758 } 759 760 static int iqs269_parse_prop(struct iqs269_private *iqs269) 761 { 762 struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg; 763 struct i2c_client *client = iqs269->client; 764 struct fwnode_handle *ch_node; 765 u16 general, misc_a, misc_b; 766 unsigned int val; 767 int error; 768 769 iqs269->hall_enable = device_property_present(&client->dev, 770 "azoteq,hall-enable"); 771 772 if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode", 773 &val)) { 774 if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) { 775 dev_err(&client->dev, "Invalid suspend mode: %u\n", 776 val); 777 return -EINVAL; 778 } 779 780 iqs269->suspend_mode = val; 781 } 782 783 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg, 784 sizeof(*sys_reg)); 785 if (error) 786 return error; 787 788 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-lta", 789 &val)) { 790 if (val > IQS269_FILT_STR_MAX) { 791 dev_err(&client->dev, "Invalid filter strength: %u\n", 792 val); 793 return -EINVAL; 794 } 795 796 sys_reg->filter &= ~IQS269_FILT_STR_LP_LTA_MASK; 797 sys_reg->filter |= (val << IQS269_FILT_STR_LP_LTA_SHIFT); 798 } 799 800 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-cnt", 801 &val)) { 802 if (val > IQS269_FILT_STR_MAX) { 803 dev_err(&client->dev, "Invalid filter strength: %u\n", 804 val); 805 return -EINVAL; 806 } 807 808 sys_reg->filter &= ~IQS269_FILT_STR_LP_CNT_MASK; 809 sys_reg->filter |= (val << IQS269_FILT_STR_LP_CNT_SHIFT); 810 } 811 812 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-lta", 813 &val)) { 814 if (val > IQS269_FILT_STR_MAX) { 815 dev_err(&client->dev, "Invalid filter strength: %u\n", 816 val); 817 return -EINVAL; 818 } 819 820 sys_reg->filter &= ~IQS269_FILT_STR_NP_LTA_MASK; 821 sys_reg->filter |= (val << IQS269_FILT_STR_NP_LTA_SHIFT); 822 } 823 824 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-cnt", 825 &val)) { 826 if (val > IQS269_FILT_STR_MAX) { 827 dev_err(&client->dev, "Invalid filter strength: %u\n", 828 val); 829 return -EINVAL; 830 } 831 832 sys_reg->filter &= ~IQS269_FILT_STR_NP_CNT_MASK; 833 sys_reg->filter |= val; 834 } 835 836 if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms", 837 &val)) { 838 if (val > IQS269_RATE_NP_MS_MAX) { 839 dev_err(&client->dev, "Invalid report rate: %u\n", val); 840 return -EINVAL; 841 } 842 843 sys_reg->rate_np = val; 844 } 845 846 if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms", 847 &val)) { 848 if (val > IQS269_RATE_LP_MS_MAX) { 849 dev_err(&client->dev, "Invalid report rate: %u\n", val); 850 return -EINVAL; 851 } 852 853 sys_reg->rate_lp = val; 854 } 855 856 if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms", 857 &val)) { 858 if (val > IQS269_RATE_ULP_MS_MAX) { 859 dev_err(&client->dev, "Invalid report rate: %u\n", val); 860 return -EINVAL; 861 } 862 863 sys_reg->rate_ulp = val / 16; 864 } 865 866 if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms", 867 &val)) { 868 if (val > IQS269_TIMEOUT_PWR_MS_MAX) { 869 dev_err(&client->dev, "Invalid timeout: %u\n", val); 870 return -EINVAL; 871 } 872 873 sys_reg->timeout_pwr = val / 512; 874 } 875 876 if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms", 877 &val)) { 878 if (val > IQS269_TIMEOUT_LTA_MS_MAX) { 879 dev_err(&client->dev, "Invalid timeout: %u\n", val); 880 return -EINVAL; 881 } 882 883 sys_reg->timeout_lta = val / 512; 884 } 885 886 misc_a = be16_to_cpu(sys_reg->misc_a); 887 misc_b = be16_to_cpu(sys_reg->misc_b); 888 889 misc_a &= ~IQS269_MISC_A_ATI_BAND_DISABLE; 890 if (device_property_present(&client->dev, "azoteq,ati-band-disable")) 891 misc_a |= IQS269_MISC_A_ATI_BAND_DISABLE; 892 893 misc_a &= ~IQS269_MISC_A_ATI_LP_ONLY; 894 if (device_property_present(&client->dev, "azoteq,ati-lp-only")) 895 misc_a |= IQS269_MISC_A_ATI_LP_ONLY; 896 897 misc_a &= ~IQS269_MISC_A_ATI_BAND_TIGHTEN; 898 if (device_property_present(&client->dev, "azoteq,ati-band-tighten")) 899 misc_a |= IQS269_MISC_A_ATI_BAND_TIGHTEN; 900 901 misc_a &= ~IQS269_MISC_A_FILT_DISABLE; 902 if (device_property_present(&client->dev, "azoteq,filt-disable")) 903 misc_a |= IQS269_MISC_A_FILT_DISABLE; 904 905 if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select", 906 &val)) { 907 if (val >= IQS269_NUM_CH) { 908 dev_err(&client->dev, "Invalid GPIO3 selection: %u\n", 909 val); 910 return -EINVAL; 911 } 912 913 misc_a &= ~IQS269_MISC_A_GPIO3_SELECT_MASK; 914 misc_a |= (val << IQS269_MISC_A_GPIO3_SELECT_SHIFT); 915 } 916 917 misc_a &= ~IQS269_MISC_A_DUAL_DIR; 918 if (device_property_present(&client->dev, "azoteq,dual-direction")) 919 misc_a |= IQS269_MISC_A_DUAL_DIR; 920 921 if (!device_property_read_u32(&client->dev, "azoteq,tx-freq", &val)) { 922 if (val > IQS269_MISC_A_TX_FREQ_MAX) { 923 dev_err(&client->dev, 924 "Invalid excitation frequency: %u\n", val); 925 return -EINVAL; 926 } 927 928 misc_a &= ~IQS269_MISC_A_TX_FREQ_MASK; 929 misc_a |= (val << IQS269_MISC_A_TX_FREQ_SHIFT); 930 } 931 932 misc_a &= ~IQS269_MISC_A_GLOBAL_CAP_SIZE; 933 if (device_property_present(&client->dev, "azoteq,global-cap-increase")) 934 misc_a |= IQS269_MISC_A_GLOBAL_CAP_SIZE; 935 936 if (!device_property_read_u32(&client->dev, "azoteq,reseed-select", 937 &val)) { 938 if (val > IQS269_MISC_B_RESEED_UI_SEL_MAX) { 939 dev_err(&client->dev, "Invalid reseed selection: %u\n", 940 val); 941 return -EINVAL; 942 } 943 944 misc_b &= ~IQS269_MISC_B_RESEED_UI_SEL_MASK; 945 misc_b |= (val << IQS269_MISC_B_RESEED_UI_SEL_SHIFT); 946 } 947 948 misc_b &= ~IQS269_MISC_B_TRACKING_UI_ENABLE; 949 if (device_property_present(&client->dev, "azoteq,tracking-enable")) 950 misc_b |= IQS269_MISC_B_TRACKING_UI_ENABLE; 951 952 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-slider", 953 &val)) { 954 if (val > IQS269_FILT_STR_MAX) { 955 dev_err(&client->dev, "Invalid filter strength: %u\n", 956 val); 957 return -EINVAL; 958 } 959 960 misc_b &= ~IQS269_MISC_B_FILT_STR_SLIDER; 961 misc_b |= val; 962 } 963 964 sys_reg->misc_a = cpu_to_be16(misc_a); 965 sys_reg->misc_b = cpu_to_be16(misc_b); 966 967 sys_reg->active = 0; 968 sys_reg->reseed = 0; 969 970 sys_reg->blocking = 0; 971 972 sys_reg->slider_select[0] = 0; 973 sys_reg->slider_select[1] = 0; 974 975 sys_reg->event_mask = ~((u8)IQS269_EVENT_MASK_SYS); 976 977 device_for_each_child_node(&client->dev, ch_node) { 978 error = iqs269_parse_chan(iqs269, ch_node); 979 if (error) { 980 fwnode_handle_put(ch_node); 981 return error; 982 } 983 } 984 985 /* 986 * Volunteer all active channels to participate in ATI when REDO-ATI is 987 * manually triggered. 988 */ 989 sys_reg->redo_ati = sys_reg->active; 990 991 general = be16_to_cpu(sys_reg->general); 992 993 if (device_property_present(&client->dev, "azoteq,clk-div")) { 994 general |= IQS269_SYS_SETTINGS_CLK_DIV; 995 iqs269->delay_mult = 4; 996 } else { 997 general &= ~IQS269_SYS_SETTINGS_CLK_DIV; 998 iqs269->delay_mult = 1; 999 } 1000 1001 /* 1002 * Configure the device to automatically switch between normal and low- 1003 * power modes as a function of sensing activity. Ultra-low-power mode, 1004 * if enabled, is reserved for suspend. 1005 */ 1006 general &= ~IQS269_SYS_SETTINGS_ULP_AUTO; 1007 general &= ~IQS269_SYS_SETTINGS_DIS_AUTO; 1008 general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK; 1009 1010 if (!device_property_read_u32(&client->dev, "azoteq,ulp-update", 1011 &val)) { 1012 if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) { 1013 dev_err(&client->dev, "Invalid update rate: %u\n", val); 1014 return -EINVAL; 1015 } 1016 1017 general &= ~IQS269_SYS_SETTINGS_ULP_UPDATE_MASK; 1018 general |= (val << IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT); 1019 } 1020 1021 general &= ~IQS269_SYS_SETTINGS_RESEED_OFFSET; 1022 if (device_property_present(&client->dev, "azoteq,reseed-offset")) 1023 general |= IQS269_SYS_SETTINGS_RESEED_OFFSET; 1024 1025 general |= IQS269_SYS_SETTINGS_EVENT_MODE; 1026 1027 /* 1028 * As per the datasheet, enable streaming during normal-power mode if 1029 * either slider is in use. In that case, the device returns to event 1030 * mode during low-power mode. 1031 */ 1032 if (sys_reg->slider_select[0] || sys_reg->slider_select[1]) 1033 general |= IQS269_SYS_SETTINGS_EVENT_MODE_LP; 1034 1035 general |= IQS269_SYS_SETTINGS_REDO_ATI; 1036 general |= IQS269_SYS_SETTINGS_ACK_RESET; 1037 1038 sys_reg->general = cpu_to_be16(general); 1039 1040 return 0; 1041 } 1042 1043 static int iqs269_dev_init(struct iqs269_private *iqs269) 1044 { 1045 struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg; 1046 struct iqs269_ch_reg *ch_reg; 1047 unsigned int val; 1048 int error, i; 1049 1050 mutex_lock(&iqs269->lock); 1051 1052 error = regmap_update_bits(iqs269->regmap, IQS269_HALL_UI, 1053 IQS269_HALL_UI_ENABLE, 1054 iqs269->hall_enable ? ~0 : 0); 1055 if (error) 1056 goto err_mutex; 1057 1058 for (i = 0; i < IQS269_NUM_CH; i++) { 1059 if (!(sys_reg->active & BIT(i))) 1060 continue; 1061 1062 ch_reg = &iqs269->ch_reg[i]; 1063 1064 error = regmap_raw_write(iqs269->regmap, 1065 IQS269_CHx_SETTINGS + i * 1066 sizeof(*ch_reg) / 2, ch_reg, 1067 sizeof(*ch_reg)); 1068 if (error) 1069 goto err_mutex; 1070 } 1071 1072 /* 1073 * The REDO-ATI and ATI channel selection fields must be written in the 1074 * same block write, so every field between registers 0x80 through 0x8B 1075 * (inclusive) must be written as well. 1076 */ 1077 error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg, 1078 sizeof(*sys_reg)); 1079 if (error) 1080 goto err_mutex; 1081 1082 error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val, 1083 !(val & IQS269_SYS_FLAGS_IN_ATI), 1084 IQS269_ATI_POLL_SLEEP_US, 1085 IQS269_ATI_POLL_TIMEOUT_US); 1086 if (error) 1087 goto err_mutex; 1088 1089 msleep(IQS269_ATI_STABLE_DELAY_MS); 1090 iqs269->ati_current = true; 1091 1092 err_mutex: 1093 mutex_unlock(&iqs269->lock); 1094 1095 return error; 1096 } 1097 1098 static int iqs269_input_init(struct iqs269_private *iqs269) 1099 { 1100 struct i2c_client *client = iqs269->client; 1101 struct iqs269_flags flags; 1102 unsigned int sw_code, keycode; 1103 int error, i, j; 1104 u8 dir_mask, state; 1105 1106 iqs269->keypad = devm_input_allocate_device(&client->dev); 1107 if (!iqs269->keypad) 1108 return -ENOMEM; 1109 1110 iqs269->keypad->keycodemax = ARRAY_SIZE(iqs269->keycode); 1111 iqs269->keypad->keycode = iqs269->keycode; 1112 iqs269->keypad->keycodesize = sizeof(*iqs269->keycode); 1113 1114 iqs269->keypad->name = "iqs269a_keypad"; 1115 iqs269->keypad->id.bustype = BUS_I2C; 1116 1117 if (iqs269->hall_enable) { 1118 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, 1119 &flags, sizeof(flags)); 1120 if (error) { 1121 dev_err(&client->dev, 1122 "Failed to read initial status: %d\n", error); 1123 return error; 1124 } 1125 } 1126 1127 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) { 1128 dir_mask = flags.states[IQS269_ST_OFFS_DIR]; 1129 if (!iqs269_events[i].dir_up) 1130 dir_mask = ~dir_mask; 1131 1132 state = flags.states[iqs269_events[i].st_offs] & dir_mask; 1133 1134 sw_code = iqs269->switches[i].code; 1135 1136 for (j = 0; j < IQS269_NUM_CH; j++) { 1137 keycode = iqs269->keycode[i * IQS269_NUM_CH + j]; 1138 1139 /* 1140 * Hall-effect sensing repurposes a pair of dedicated 1141 * channels, only one of which reports events. 1142 */ 1143 switch (j) { 1144 case IQS269_CHx_HALL_ACTIVE: 1145 if (iqs269->hall_enable && 1146 iqs269->switches[i].enabled) { 1147 input_set_capability(iqs269->keypad, 1148 EV_SW, sw_code); 1149 input_report_switch(iqs269->keypad, 1150 sw_code, 1151 state & BIT(j)); 1152 } 1153 fallthrough; 1154 1155 case IQS269_CHx_HALL_INACTIVE: 1156 if (iqs269->hall_enable) 1157 continue; 1158 fallthrough; 1159 1160 default: 1161 if (keycode != KEY_RESERVED) 1162 input_set_capability(iqs269->keypad, 1163 EV_KEY, keycode); 1164 } 1165 } 1166 } 1167 1168 input_sync(iqs269->keypad); 1169 1170 error = input_register_device(iqs269->keypad); 1171 if (error) { 1172 dev_err(&client->dev, "Failed to register keypad: %d\n", error); 1173 return error; 1174 } 1175 1176 for (i = 0; i < IQS269_NUM_SL; i++) { 1177 if (!iqs269->sys_reg.slider_select[i]) 1178 continue; 1179 1180 iqs269->slider[i] = devm_input_allocate_device(&client->dev); 1181 if (!iqs269->slider[i]) 1182 return -ENOMEM; 1183 1184 iqs269->slider[i]->name = i ? "iqs269a_slider_1" 1185 : "iqs269a_slider_0"; 1186 iqs269->slider[i]->id.bustype = BUS_I2C; 1187 1188 input_set_capability(iqs269->slider[i], EV_KEY, BTN_TOUCH); 1189 input_set_abs_params(iqs269->slider[i], ABS_X, 0, 255, 0, 0); 1190 1191 error = input_register_device(iqs269->slider[i]); 1192 if (error) { 1193 dev_err(&client->dev, 1194 "Failed to register slider %d: %d\n", i, error); 1195 return error; 1196 } 1197 } 1198 1199 return 0; 1200 } 1201 1202 static int iqs269_report(struct iqs269_private *iqs269) 1203 { 1204 struct i2c_client *client = iqs269->client; 1205 struct iqs269_flags flags; 1206 unsigned int sw_code, keycode; 1207 int error, i, j; 1208 u8 slider_x[IQS269_NUM_SL]; 1209 u8 dir_mask, state; 1210 1211 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, &flags, 1212 sizeof(flags)); 1213 if (error) { 1214 dev_err(&client->dev, "Failed to read device status: %d\n", 1215 error); 1216 return error; 1217 } 1218 1219 /* 1220 * The device resets itself if its own watchdog bites, which can happen 1221 * in the event of an I2C communication error. In this case, the device 1222 * asserts a SHOW_RESET interrupt and all registers must be restored. 1223 */ 1224 if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_SHOW_RESET) { 1225 dev_err(&client->dev, "Unexpected device reset\n"); 1226 1227 error = iqs269_dev_init(iqs269); 1228 if (error) 1229 dev_err(&client->dev, 1230 "Failed to re-initialize device: %d\n", error); 1231 1232 return error; 1233 } 1234 1235 error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X, slider_x, 1236 sizeof(slider_x)); 1237 if (error) { 1238 dev_err(&client->dev, "Failed to read slider position: %d\n", 1239 error); 1240 return error; 1241 } 1242 1243 for (i = 0; i < IQS269_NUM_SL; i++) { 1244 if (!iqs269->sys_reg.slider_select[i]) 1245 continue; 1246 1247 /* 1248 * Report BTN_TOUCH if any channel that participates in the 1249 * slider is in a state of touch. 1250 */ 1251 if (flags.states[IQS269_ST_OFFS_TOUCH] & 1252 iqs269->sys_reg.slider_select[i]) { 1253 input_report_key(iqs269->slider[i], BTN_TOUCH, 1); 1254 input_report_abs(iqs269->slider[i], ABS_X, slider_x[i]); 1255 } else { 1256 input_report_key(iqs269->slider[i], BTN_TOUCH, 0); 1257 } 1258 1259 input_sync(iqs269->slider[i]); 1260 } 1261 1262 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) { 1263 dir_mask = flags.states[IQS269_ST_OFFS_DIR]; 1264 if (!iqs269_events[i].dir_up) 1265 dir_mask = ~dir_mask; 1266 1267 state = flags.states[iqs269_events[i].st_offs] & dir_mask; 1268 1269 sw_code = iqs269->switches[i].code; 1270 1271 for (j = 0; j < IQS269_NUM_CH; j++) { 1272 keycode = iqs269->keycode[i * IQS269_NUM_CH + j]; 1273 1274 switch (j) { 1275 case IQS269_CHx_HALL_ACTIVE: 1276 if (iqs269->hall_enable && 1277 iqs269->switches[i].enabled) 1278 input_report_switch(iqs269->keypad, 1279 sw_code, 1280 state & BIT(j)); 1281 fallthrough; 1282 1283 case IQS269_CHx_HALL_INACTIVE: 1284 if (iqs269->hall_enable) 1285 continue; 1286 fallthrough; 1287 1288 default: 1289 input_report_key(iqs269->keypad, keycode, 1290 state & BIT(j)); 1291 } 1292 } 1293 } 1294 1295 input_sync(iqs269->keypad); 1296 1297 return 0; 1298 } 1299 1300 static irqreturn_t iqs269_irq(int irq, void *context) 1301 { 1302 struct iqs269_private *iqs269 = context; 1303 1304 if (iqs269_report(iqs269)) 1305 return IRQ_NONE; 1306 1307 /* 1308 * The device does not deassert its interrupt (RDY) pin until shortly 1309 * after receiving an I2C stop condition; the following delay ensures 1310 * the interrupt handler does not return before this time. 1311 */ 1312 iqs269_irq_wait(); 1313 1314 return IRQ_HANDLED; 1315 } 1316 1317 static ssize_t counts_show(struct device *dev, 1318 struct device_attribute *attr, char *buf) 1319 { 1320 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1321 struct i2c_client *client = iqs269->client; 1322 __le16 counts; 1323 int error; 1324 1325 if (!iqs269->ati_current || iqs269->hall_enable) 1326 return -EPERM; 1327 1328 /* 1329 * Unsolicited I2C communication prompts the device to assert its RDY 1330 * pin, so disable the interrupt line until the operation is finished 1331 * and RDY has been deasserted. 1332 */ 1333 disable_irq(client->irq); 1334 1335 error = regmap_raw_read(iqs269->regmap, 1336 IQS269_CHx_COUNTS + iqs269->ch_num * 2, 1337 &counts, sizeof(counts)); 1338 1339 iqs269_irq_wait(); 1340 enable_irq(client->irq); 1341 1342 if (error) 1343 return error; 1344 1345 return scnprintf(buf, PAGE_SIZE, "%u\n", le16_to_cpu(counts)); 1346 } 1347 1348 static ssize_t hall_bin_show(struct device *dev, 1349 struct device_attribute *attr, char *buf) 1350 { 1351 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1352 struct i2c_client *client = iqs269->client; 1353 unsigned int val; 1354 int error; 1355 1356 disable_irq(client->irq); 1357 1358 error = regmap_read(iqs269->regmap, IQS269_CAL_DATA_A, &val); 1359 1360 iqs269_irq_wait(); 1361 enable_irq(client->irq); 1362 1363 if (error) 1364 return error; 1365 1366 switch (iqs269->ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable & 1367 iqs269->ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) { 1368 case IQS269_HALL_PAD_R: 1369 val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK; 1370 val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT; 1371 break; 1372 1373 case IQS269_HALL_PAD_L: 1374 val &= IQS269_CAL_DATA_A_HALL_BIN_L_MASK; 1375 val >>= IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT; 1376 break; 1377 1378 default: 1379 return -EINVAL; 1380 } 1381 1382 return scnprintf(buf, PAGE_SIZE, "%u\n", val); 1383 } 1384 1385 static ssize_t hall_enable_show(struct device *dev, 1386 struct device_attribute *attr, char *buf) 1387 { 1388 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1389 1390 return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->hall_enable); 1391 } 1392 1393 static ssize_t hall_enable_store(struct device *dev, 1394 struct device_attribute *attr, const char *buf, 1395 size_t count) 1396 { 1397 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1398 unsigned int val; 1399 int error; 1400 1401 error = kstrtouint(buf, 10, &val); 1402 if (error) 1403 return error; 1404 1405 mutex_lock(&iqs269->lock); 1406 1407 iqs269->hall_enable = val; 1408 iqs269->ati_current = false; 1409 1410 mutex_unlock(&iqs269->lock); 1411 1412 return count; 1413 } 1414 1415 static ssize_t ch_number_show(struct device *dev, 1416 struct device_attribute *attr, char *buf) 1417 { 1418 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1419 1420 return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ch_num); 1421 } 1422 1423 static ssize_t ch_number_store(struct device *dev, 1424 struct device_attribute *attr, const char *buf, 1425 size_t count) 1426 { 1427 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1428 unsigned int val; 1429 int error; 1430 1431 error = kstrtouint(buf, 10, &val); 1432 if (error) 1433 return error; 1434 1435 if (val >= IQS269_NUM_CH) 1436 return -EINVAL; 1437 1438 iqs269->ch_num = val; 1439 1440 return count; 1441 } 1442 1443 static ssize_t rx_enable_show(struct device *dev, 1444 struct device_attribute *attr, char *buf) 1445 { 1446 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1447 1448 return scnprintf(buf, PAGE_SIZE, "%u\n", 1449 iqs269->ch_reg[iqs269->ch_num].rx_enable); 1450 } 1451 1452 static ssize_t rx_enable_store(struct device *dev, 1453 struct device_attribute *attr, const char *buf, 1454 size_t count) 1455 { 1456 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1457 unsigned int val; 1458 int error; 1459 1460 error = kstrtouint(buf, 10, &val); 1461 if (error) 1462 return error; 1463 1464 if (val > 0xFF) 1465 return -EINVAL; 1466 1467 mutex_lock(&iqs269->lock); 1468 1469 iqs269->ch_reg[iqs269->ch_num].rx_enable = val; 1470 iqs269->ati_current = false; 1471 1472 mutex_unlock(&iqs269->lock); 1473 1474 return count; 1475 } 1476 1477 static ssize_t ati_mode_show(struct device *dev, 1478 struct device_attribute *attr, char *buf) 1479 { 1480 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1481 unsigned int val; 1482 int error; 1483 1484 error = iqs269_ati_mode_get(iqs269, iqs269->ch_num, &val); 1485 if (error) 1486 return error; 1487 1488 return scnprintf(buf, PAGE_SIZE, "%u\n", val); 1489 } 1490 1491 static ssize_t ati_mode_store(struct device *dev, 1492 struct device_attribute *attr, const char *buf, 1493 size_t count) 1494 { 1495 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1496 unsigned int val; 1497 int error; 1498 1499 error = kstrtouint(buf, 10, &val); 1500 if (error) 1501 return error; 1502 1503 error = iqs269_ati_mode_set(iqs269, iqs269->ch_num, val); 1504 if (error) 1505 return error; 1506 1507 return count; 1508 } 1509 1510 static ssize_t ati_base_show(struct device *dev, 1511 struct device_attribute *attr, char *buf) 1512 { 1513 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1514 unsigned int val; 1515 int error; 1516 1517 error = iqs269_ati_base_get(iqs269, iqs269->ch_num, &val); 1518 if (error) 1519 return error; 1520 1521 return scnprintf(buf, PAGE_SIZE, "%u\n", val); 1522 } 1523 1524 static ssize_t ati_base_store(struct device *dev, 1525 struct device_attribute *attr, const char *buf, 1526 size_t count) 1527 { 1528 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1529 unsigned int val; 1530 int error; 1531 1532 error = kstrtouint(buf, 10, &val); 1533 if (error) 1534 return error; 1535 1536 error = iqs269_ati_base_set(iqs269, iqs269->ch_num, val); 1537 if (error) 1538 return error; 1539 1540 return count; 1541 } 1542 1543 static ssize_t ati_target_show(struct device *dev, 1544 struct device_attribute *attr, char *buf) 1545 { 1546 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1547 unsigned int val; 1548 int error; 1549 1550 error = iqs269_ati_target_get(iqs269, iqs269->ch_num, &val); 1551 if (error) 1552 return error; 1553 1554 return scnprintf(buf, PAGE_SIZE, "%u\n", val); 1555 } 1556 1557 static ssize_t ati_target_store(struct device *dev, 1558 struct device_attribute *attr, const char *buf, 1559 size_t count) 1560 { 1561 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1562 unsigned int val; 1563 int error; 1564 1565 error = kstrtouint(buf, 10, &val); 1566 if (error) 1567 return error; 1568 1569 error = iqs269_ati_target_set(iqs269, iqs269->ch_num, val); 1570 if (error) 1571 return error; 1572 1573 return count; 1574 } 1575 1576 static ssize_t ati_trigger_show(struct device *dev, 1577 struct device_attribute *attr, char *buf) 1578 { 1579 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1580 1581 return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ati_current); 1582 } 1583 1584 static ssize_t ati_trigger_store(struct device *dev, 1585 struct device_attribute *attr, const char *buf, 1586 size_t count) 1587 { 1588 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1589 struct i2c_client *client = iqs269->client; 1590 unsigned int val; 1591 int error; 1592 1593 error = kstrtouint(buf, 10, &val); 1594 if (error) 1595 return error; 1596 1597 if (!val) 1598 return count; 1599 1600 disable_irq(client->irq); 1601 1602 error = iqs269_dev_init(iqs269); 1603 1604 iqs269_irq_wait(); 1605 enable_irq(client->irq); 1606 1607 if (error) 1608 return error; 1609 1610 return count; 1611 } 1612 1613 static DEVICE_ATTR_RO(counts); 1614 static DEVICE_ATTR_RO(hall_bin); 1615 static DEVICE_ATTR_RW(hall_enable); 1616 static DEVICE_ATTR_RW(ch_number); 1617 static DEVICE_ATTR_RW(rx_enable); 1618 static DEVICE_ATTR_RW(ati_mode); 1619 static DEVICE_ATTR_RW(ati_base); 1620 static DEVICE_ATTR_RW(ati_target); 1621 static DEVICE_ATTR_RW(ati_trigger); 1622 1623 static struct attribute *iqs269_attrs[] = { 1624 &dev_attr_counts.attr, 1625 &dev_attr_hall_bin.attr, 1626 &dev_attr_hall_enable.attr, 1627 &dev_attr_ch_number.attr, 1628 &dev_attr_rx_enable.attr, 1629 &dev_attr_ati_mode.attr, 1630 &dev_attr_ati_base.attr, 1631 &dev_attr_ati_target.attr, 1632 &dev_attr_ati_trigger.attr, 1633 NULL, 1634 }; 1635 1636 static const struct attribute_group iqs269_attr_group = { 1637 .attrs = iqs269_attrs, 1638 }; 1639 1640 static const struct regmap_config iqs269_regmap_config = { 1641 .reg_bits = 8, 1642 .val_bits = 16, 1643 .max_register = IQS269_MAX_REG, 1644 }; 1645 1646 static int iqs269_probe(struct i2c_client *client) 1647 { 1648 struct iqs269_ver_info ver_info; 1649 struct iqs269_private *iqs269; 1650 int error; 1651 1652 iqs269 = devm_kzalloc(&client->dev, sizeof(*iqs269), GFP_KERNEL); 1653 if (!iqs269) 1654 return -ENOMEM; 1655 1656 i2c_set_clientdata(client, iqs269); 1657 iqs269->client = client; 1658 1659 iqs269->regmap = devm_regmap_init_i2c(client, &iqs269_regmap_config); 1660 if (IS_ERR(iqs269->regmap)) { 1661 error = PTR_ERR(iqs269->regmap); 1662 dev_err(&client->dev, "Failed to initialize register map: %d\n", 1663 error); 1664 return error; 1665 } 1666 1667 mutex_init(&iqs269->lock); 1668 1669 error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO, &ver_info, 1670 sizeof(ver_info)); 1671 if (error) 1672 return error; 1673 1674 if (ver_info.prod_num != IQS269_VER_INFO_PROD_NUM) { 1675 dev_err(&client->dev, "Unrecognized product number: 0x%02X\n", 1676 ver_info.prod_num); 1677 return -EINVAL; 1678 } 1679 1680 error = iqs269_parse_prop(iqs269); 1681 if (error) 1682 return error; 1683 1684 error = iqs269_dev_init(iqs269); 1685 if (error) { 1686 dev_err(&client->dev, "Failed to initialize device: %d\n", 1687 error); 1688 return error; 1689 } 1690 1691 error = iqs269_input_init(iqs269); 1692 if (error) 1693 return error; 1694 1695 error = devm_request_threaded_irq(&client->dev, client->irq, 1696 NULL, iqs269_irq, IRQF_ONESHOT, 1697 client->name, iqs269); 1698 if (error) { 1699 dev_err(&client->dev, "Failed to request IRQ: %d\n", error); 1700 return error; 1701 } 1702 1703 error = devm_device_add_group(&client->dev, &iqs269_attr_group); 1704 if (error) 1705 dev_err(&client->dev, "Failed to add attributes: %d\n", error); 1706 1707 return error; 1708 } 1709 1710 static int iqs269_suspend(struct device *dev) 1711 { 1712 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1713 struct i2c_client *client = iqs269->client; 1714 unsigned int val; 1715 int error; 1716 1717 if (!iqs269->suspend_mode) 1718 return 0; 1719 1720 disable_irq(client->irq); 1721 1722 /* 1723 * Automatic power mode switching must be disabled before the device is 1724 * forced into any particular power mode. In this case, the device will 1725 * transition into normal-power mode. 1726 */ 1727 error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS, 1728 IQS269_SYS_SETTINGS_DIS_AUTO, ~0); 1729 if (error) 1730 goto err_irq; 1731 1732 /* 1733 * The following check ensures the device has completed its transition 1734 * into normal-power mode before a manual mode switch is performed. 1735 */ 1736 error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val, 1737 !(val & IQS269_SYS_FLAGS_PWR_MODE_MASK), 1738 IQS269_PWR_MODE_POLL_SLEEP_US, 1739 IQS269_PWR_MODE_POLL_TIMEOUT_US); 1740 if (error) 1741 goto err_irq; 1742 1743 error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS, 1744 IQS269_SYS_SETTINGS_PWR_MODE_MASK, 1745 iqs269->suspend_mode << 1746 IQS269_SYS_SETTINGS_PWR_MODE_SHIFT); 1747 if (error) 1748 goto err_irq; 1749 1750 /* 1751 * This last check ensures the device has completed its transition into 1752 * the desired power mode to prevent any spurious interrupts from being 1753 * triggered after iqs269_suspend has already returned. 1754 */ 1755 error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val, 1756 (val & IQS269_SYS_FLAGS_PWR_MODE_MASK) 1757 == (iqs269->suspend_mode << 1758 IQS269_SYS_FLAGS_PWR_MODE_SHIFT), 1759 IQS269_PWR_MODE_POLL_SLEEP_US, 1760 IQS269_PWR_MODE_POLL_TIMEOUT_US); 1761 1762 err_irq: 1763 iqs269_irq_wait(); 1764 enable_irq(client->irq); 1765 1766 return error; 1767 } 1768 1769 static int iqs269_resume(struct device *dev) 1770 { 1771 struct iqs269_private *iqs269 = dev_get_drvdata(dev); 1772 struct i2c_client *client = iqs269->client; 1773 unsigned int val; 1774 int error; 1775 1776 if (!iqs269->suspend_mode) 1777 return 0; 1778 1779 disable_irq(client->irq); 1780 1781 error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS, 1782 IQS269_SYS_SETTINGS_PWR_MODE_MASK, 0); 1783 if (error) 1784 goto err_irq; 1785 1786 /* 1787 * This check ensures the device has returned to normal-power mode 1788 * before automatic power mode switching is re-enabled. 1789 */ 1790 error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val, 1791 !(val & IQS269_SYS_FLAGS_PWR_MODE_MASK), 1792 IQS269_PWR_MODE_POLL_SLEEP_US, 1793 IQS269_PWR_MODE_POLL_TIMEOUT_US); 1794 if (error) 1795 goto err_irq; 1796 1797 error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS, 1798 IQS269_SYS_SETTINGS_DIS_AUTO, 0); 1799 if (error) 1800 goto err_irq; 1801 1802 /* 1803 * This step reports any events that may have been "swallowed" as a 1804 * result of polling PWR_MODE (which automatically acknowledges any 1805 * pending interrupts). 1806 */ 1807 error = iqs269_report(iqs269); 1808 1809 err_irq: 1810 iqs269_irq_wait(); 1811 enable_irq(client->irq); 1812 1813 return error; 1814 } 1815 1816 static DEFINE_SIMPLE_DEV_PM_OPS(iqs269_pm, iqs269_suspend, iqs269_resume); 1817 1818 static const struct of_device_id iqs269_of_match[] = { 1819 { .compatible = "azoteq,iqs269a" }, 1820 { } 1821 }; 1822 MODULE_DEVICE_TABLE(of, iqs269_of_match); 1823 1824 static struct i2c_driver iqs269_i2c_driver = { 1825 .driver = { 1826 .name = "iqs269a", 1827 .of_match_table = iqs269_of_match, 1828 .pm = pm_sleep_ptr(&iqs269_pm), 1829 }, 1830 .probe_new = iqs269_probe, 1831 }; 1832 module_i2c_driver(iqs269_i2c_driver); 1833 1834 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>"); 1835 MODULE_DESCRIPTION("Azoteq IQS269A Capacitive Touch Controller"); 1836 MODULE_LICENSE("GPL"); 1837