iqs626a.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Azoteq IQS626A Capacitive Touch Controller
  4. *
  5. * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com>
  6. *
  7. * This driver registers up to 2 input devices: one representing capacitive or
  8. * inductive keys as well as Hall-effect switches, and one for a trackpad that
  9. * can express various gestures.
  10. */
  11. #include <linux/bits.h>
  12. #include <linux/completion.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/input.h>
  18. #include <linux/input/touchscreen.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/module.h>
  23. #include <linux/property.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #define IQS626_VER_INFO 0x00
  27. #define IQS626_VER_INFO_PROD_NUM 0x51
  28. #define IQS626_SYS_FLAGS 0x02
  29. #define IQS626_SYS_FLAGS_SHOW_RESET BIT(15)
  30. #define IQS626_SYS_FLAGS_IN_ATI BIT(12)
  31. #define IQS626_SYS_FLAGS_PWR_MODE_MASK GENMASK(9, 8)
  32. #define IQS626_SYS_FLAGS_PWR_MODE_SHIFT 8
  33. #define IQS626_HALL_OUTPUT 0x23
  34. #define IQS626_SYS_SETTINGS 0x80
  35. #define IQS626_SYS_SETTINGS_CLK_DIV BIT(15)
  36. #define IQS626_SYS_SETTINGS_ULP_AUTO BIT(14)
  37. #define IQS626_SYS_SETTINGS_DIS_AUTO BIT(13)
  38. #define IQS626_SYS_SETTINGS_PWR_MODE_MASK GENMASK(12, 11)
  39. #define IQS626_SYS_SETTINGS_PWR_MODE_SHIFT 11
  40. #define IQS626_SYS_SETTINGS_PWR_MODE_MAX 3
  41. #define IQS626_SYS_SETTINGS_ULP_UPDATE_MASK GENMASK(10, 8)
  42. #define IQS626_SYS_SETTINGS_ULP_UPDATE_SHIFT 8
  43. #define IQS626_SYS_SETTINGS_ULP_UPDATE_MAX 7
  44. #define IQS626_SYS_SETTINGS_EVENT_MODE BIT(5)
  45. #define IQS626_SYS_SETTINGS_EVENT_MODE_LP BIT(4)
  46. #define IQS626_SYS_SETTINGS_REDO_ATI BIT(2)
  47. #define IQS626_SYS_SETTINGS_ACK_RESET BIT(0)
  48. #define IQS626_MISC_A_ATI_BAND_DISABLE BIT(7)
  49. #define IQS626_MISC_A_TPx_LTA_UPDATE_MASK GENMASK(6, 4)
  50. #define IQS626_MISC_A_TPx_LTA_UPDATE_SHIFT 4
  51. #define IQS626_MISC_A_TPx_LTA_UPDATE_MAX 7
  52. #define IQS626_MISC_A_ATI_LP_ONLY BIT(3)
  53. #define IQS626_MISC_A_GPIO3_SELECT_MASK GENMASK(2, 0)
  54. #define IQS626_MISC_A_GPIO3_SELECT_MAX 7
  55. #define IQS626_EVENT_MASK_SYS BIT(6)
  56. #define IQS626_EVENT_MASK_GESTURE BIT(3)
  57. #define IQS626_EVENT_MASK_DEEP BIT(2)
  58. #define IQS626_EVENT_MASK_TOUCH BIT(1)
  59. #define IQS626_EVENT_MASK_PROX BIT(0)
  60. #define IQS626_RATE_NP_MS_MAX 255
  61. #define IQS626_RATE_LP_MS_MAX 255
  62. #define IQS626_RATE_ULP_MS_MAX 4080
  63. #define IQS626_TIMEOUT_PWR_MS_MAX 130560
  64. #define IQS626_TIMEOUT_LTA_MS_MAX 130560
  65. #define IQS626_MISC_B_RESEED_UI_SEL_MASK GENMASK(7, 6)
  66. #define IQS626_MISC_B_RESEED_UI_SEL_SHIFT 6
  67. #define IQS626_MISC_B_RESEED_UI_SEL_MAX 3
  68. #define IQS626_MISC_B_THRESH_EXTEND BIT(5)
  69. #define IQS626_MISC_B_TRACKING_UI_ENABLE BIT(4)
  70. #define IQS626_MISC_B_TPx_SWIPE BIT(3)
  71. #define IQS626_MISC_B_RESEED_OFFSET BIT(2)
  72. #define IQS626_MISC_B_FILT_STR_TPx GENMASK(1, 0)
  73. #define IQS626_THRESH_SWIPE_MAX 255
  74. #define IQS626_TIMEOUT_TAP_MS_MAX 4080
  75. #define IQS626_TIMEOUT_SWIPE_MS_MAX 4080
  76. #define IQS626_CHx_ENG_0_MEAS_CAP_SIZE BIT(7)
  77. #define IQS626_CHx_ENG_0_RX_TERM_VSS BIT(5)
  78. #define IQS626_CHx_ENG_0_LINEARIZE BIT(4)
  79. #define IQS626_CHx_ENG_0_DUAL_DIR BIT(3)
  80. #define IQS626_CHx_ENG_0_FILT_DISABLE BIT(2)
  81. #define IQS626_CHx_ENG_0_ATI_MODE_MASK GENMASK(1, 0)
  82. #define IQS626_CHx_ENG_0_ATI_MODE_MAX 3
  83. #define IQS626_CHx_ENG_1_CCT_HIGH_1 BIT(7)
  84. #define IQS626_CHx_ENG_1_CCT_HIGH_0 BIT(6)
  85. #define IQS626_CHx_ENG_1_PROJ_BIAS_MASK GENMASK(5, 4)
  86. #define IQS626_CHx_ENG_1_PROJ_BIAS_SHIFT 4
  87. #define IQS626_CHx_ENG_1_PROJ_BIAS_MAX 3
  88. #define IQS626_CHx_ENG_1_CCT_ENABLE BIT(3)
  89. #define IQS626_CHx_ENG_1_SENSE_FREQ_MASK GENMASK(2, 1)
  90. #define IQS626_CHx_ENG_1_SENSE_FREQ_SHIFT 1
  91. #define IQS626_CHx_ENG_1_SENSE_FREQ_MAX 3
  92. #define IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN BIT(0)
  93. #define IQS626_CHx_ENG_2_LOCAL_CAP_MASK GENMASK(7, 6)
  94. #define IQS626_CHx_ENG_2_LOCAL_CAP_SHIFT 6
  95. #define IQS626_CHx_ENG_2_LOCAL_CAP_MAX 3
  96. #define IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE BIT(5)
  97. #define IQS626_CHx_ENG_2_SENSE_MODE_MASK GENMASK(3, 0)
  98. #define IQS626_CHx_ENG_2_SENSE_MODE_MAX 15
  99. #define IQS626_CHx_ENG_3_TX_FREQ_MASK GENMASK(5, 4)
  100. #define IQS626_CHx_ENG_3_TX_FREQ_SHIFT 4
  101. #define IQS626_CHx_ENG_3_TX_FREQ_MAX 3
  102. #define IQS626_CHx_ENG_3_INV_LOGIC BIT(0)
  103. #define IQS626_CHx_ENG_4_RX_TERM_VREG BIT(6)
  104. #define IQS626_CHx_ENG_4_CCT_LOW_1 BIT(5)
  105. #define IQS626_CHx_ENG_4_CCT_LOW_0 BIT(4)
  106. #define IQS626_CHx_ENG_4_COMP_DISABLE BIT(1)
  107. #define IQS626_CHx_ENG_4_STATIC_ENABLE BIT(0)
  108. #define IQS626_TPx_ATI_BASE_MIN 45
  109. #define IQS626_TPx_ATI_BASE_MAX 300
  110. #define IQS626_CHx_ATI_BASE_MASK GENMASK(7, 6)
  111. #define IQS626_CHx_ATI_BASE_75 0x00
  112. #define IQS626_CHx_ATI_BASE_100 0x40
  113. #define IQS626_CHx_ATI_BASE_150 0x80
  114. #define IQS626_CHx_ATI_BASE_200 0xC0
  115. #define IQS626_CHx_ATI_TARGET_MASK GENMASK(5, 0)
  116. #define IQS626_CHx_ATI_TARGET_MAX 2016
  117. #define IQS626_CHx_THRESH_MAX 255
  118. #define IQS626_CHx_HYST_DEEP_MASK GENMASK(7, 4)
  119. #define IQS626_CHx_HYST_DEEP_SHIFT 4
  120. #define IQS626_CHx_HYST_TOUCH_MASK GENMASK(3, 0)
  121. #define IQS626_CHx_HYST_MAX 15
  122. #define IQS626_FILT_STR_NP_TPx_MASK GENMASK(7, 6)
  123. #define IQS626_FILT_STR_NP_TPx_SHIFT 6
  124. #define IQS626_FILT_STR_LP_TPx_MASK GENMASK(5, 4)
  125. #define IQS626_FILT_STR_LP_TPx_SHIFT 4
  126. #define IQS626_FILT_STR_NP_CNT_MASK GENMASK(7, 6)
  127. #define IQS626_FILT_STR_NP_CNT_SHIFT 6
  128. #define IQS626_FILT_STR_LP_CNT_MASK GENMASK(5, 4)
  129. #define IQS626_FILT_STR_LP_CNT_SHIFT 4
  130. #define IQS626_FILT_STR_NP_LTA_MASK GENMASK(3, 2)
  131. #define IQS626_FILT_STR_NP_LTA_SHIFT 2
  132. #define IQS626_FILT_STR_LP_LTA_MASK GENMASK(1, 0)
  133. #define IQS626_FILT_STR_MAX 3
  134. #define IQS626_ULP_PROJ_ENABLE BIT(4)
  135. #define IQS626_GEN_WEIGHT_MAX 255
  136. #define IQS626_MAX_REG 0xFF
  137. #define IQS626_NUM_CH_TP_3 9
  138. #define IQS626_NUM_CH_TP_2 6
  139. #define IQS626_NUM_CH_GEN 3
  140. #define IQS626_NUM_CRx_TX 8
  141. #define IQS626_PWR_MODE_POLL_SLEEP_US 50000
  142. #define IQS626_PWR_MODE_POLL_TIMEOUT_US 500000
  143. #define iqs626_irq_wait() usleep_range(350, 400)
  144. enum iqs626_ch_id {
  145. IQS626_CH_ULP_0,
  146. IQS626_CH_TP_2,
  147. IQS626_CH_TP_3,
  148. IQS626_CH_GEN_0,
  149. IQS626_CH_GEN_1,
  150. IQS626_CH_GEN_2,
  151. IQS626_CH_HALL,
  152. };
  153. enum iqs626_rx_inactive {
  154. IQS626_RX_INACTIVE_VSS,
  155. IQS626_RX_INACTIVE_FLOAT,
  156. IQS626_RX_INACTIVE_VREG,
  157. };
  158. enum iqs626_st_offs {
  159. IQS626_ST_OFFS_PROX,
  160. IQS626_ST_OFFS_DIR,
  161. IQS626_ST_OFFS_TOUCH,
  162. IQS626_ST_OFFS_DEEP,
  163. };
  164. enum iqs626_th_offs {
  165. IQS626_TH_OFFS_PROX,
  166. IQS626_TH_OFFS_TOUCH,
  167. IQS626_TH_OFFS_DEEP,
  168. };
  169. enum iqs626_event_id {
  170. IQS626_EVENT_PROX_DN,
  171. IQS626_EVENT_PROX_UP,
  172. IQS626_EVENT_TOUCH_DN,
  173. IQS626_EVENT_TOUCH_UP,
  174. IQS626_EVENT_DEEP_DN,
  175. IQS626_EVENT_DEEP_UP,
  176. };
  177. enum iqs626_gesture_id {
  178. IQS626_GESTURE_FLICK_X_POS,
  179. IQS626_GESTURE_FLICK_X_NEG,
  180. IQS626_GESTURE_FLICK_Y_POS,
  181. IQS626_GESTURE_FLICK_Y_NEG,
  182. IQS626_GESTURE_TAP,
  183. IQS626_GESTURE_HOLD,
  184. IQS626_NUM_GESTURES,
  185. };
  186. struct iqs626_event_desc {
  187. const char *name;
  188. enum iqs626_st_offs st_offs;
  189. enum iqs626_th_offs th_offs;
  190. bool dir_up;
  191. u8 mask;
  192. };
  193. static const struct iqs626_event_desc iqs626_events[] = {
  194. [IQS626_EVENT_PROX_DN] = {
  195. .name = "event-prox",
  196. .st_offs = IQS626_ST_OFFS_PROX,
  197. .th_offs = IQS626_TH_OFFS_PROX,
  198. .mask = IQS626_EVENT_MASK_PROX,
  199. },
  200. [IQS626_EVENT_PROX_UP] = {
  201. .name = "event-prox-alt",
  202. .st_offs = IQS626_ST_OFFS_PROX,
  203. .th_offs = IQS626_TH_OFFS_PROX,
  204. .dir_up = true,
  205. .mask = IQS626_EVENT_MASK_PROX,
  206. },
  207. [IQS626_EVENT_TOUCH_DN] = {
  208. .name = "event-touch",
  209. .st_offs = IQS626_ST_OFFS_TOUCH,
  210. .th_offs = IQS626_TH_OFFS_TOUCH,
  211. .mask = IQS626_EVENT_MASK_TOUCH,
  212. },
  213. [IQS626_EVENT_TOUCH_UP] = {
  214. .name = "event-touch-alt",
  215. .st_offs = IQS626_ST_OFFS_TOUCH,
  216. .th_offs = IQS626_TH_OFFS_TOUCH,
  217. .dir_up = true,
  218. .mask = IQS626_EVENT_MASK_TOUCH,
  219. },
  220. [IQS626_EVENT_DEEP_DN] = {
  221. .name = "event-deep",
  222. .st_offs = IQS626_ST_OFFS_DEEP,
  223. .th_offs = IQS626_TH_OFFS_DEEP,
  224. .mask = IQS626_EVENT_MASK_DEEP,
  225. },
  226. [IQS626_EVENT_DEEP_UP] = {
  227. .name = "event-deep-alt",
  228. .st_offs = IQS626_ST_OFFS_DEEP,
  229. .th_offs = IQS626_TH_OFFS_DEEP,
  230. .dir_up = true,
  231. .mask = IQS626_EVENT_MASK_DEEP,
  232. },
  233. };
  234. struct iqs626_ver_info {
  235. u8 prod_num;
  236. u8 sw_num;
  237. u8 hw_num;
  238. u8 padding;
  239. } __packed;
  240. struct iqs626_flags {
  241. __be16 system;
  242. u8 gesture;
  243. u8 padding_a;
  244. u8 states[4];
  245. u8 ref_active;
  246. u8 padding_b;
  247. u8 comp_min;
  248. u8 comp_max;
  249. u8 trackpad_x;
  250. u8 trackpad_y;
  251. } __packed;
  252. struct iqs626_ch_reg_ulp {
  253. u8 thresh[2];
  254. u8 hyst;
  255. u8 filter;
  256. u8 engine[2];
  257. u8 ati_target;
  258. u8 padding;
  259. __be16 ati_comp;
  260. u8 rx_enable;
  261. u8 tx_enable;
  262. } __packed;
  263. struct iqs626_ch_reg_tp {
  264. u8 thresh;
  265. u8 ati_base;
  266. __be16 ati_comp;
  267. } __packed;
  268. struct iqs626_tp_grp_reg {
  269. u8 hyst;
  270. u8 ati_target;
  271. u8 engine[2];
  272. struct iqs626_ch_reg_tp ch_reg_tp[IQS626_NUM_CH_TP_3];
  273. } __packed;
  274. struct iqs626_ch_reg_gen {
  275. u8 thresh[3];
  276. u8 padding;
  277. u8 hyst;
  278. u8 ati_target;
  279. __be16 ati_comp;
  280. u8 engine[5];
  281. u8 filter;
  282. u8 rx_enable;
  283. u8 tx_enable;
  284. u8 assoc_select;
  285. u8 assoc_weight;
  286. } __packed;
  287. struct iqs626_ch_reg_hall {
  288. u8 engine;
  289. u8 thresh;
  290. u8 hyst;
  291. u8 ati_target;
  292. __be16 ati_comp;
  293. } __packed;
  294. struct iqs626_sys_reg {
  295. __be16 general;
  296. u8 misc_a;
  297. u8 event_mask;
  298. u8 active;
  299. u8 reseed;
  300. u8 rate_np;
  301. u8 rate_lp;
  302. u8 rate_ulp;
  303. u8 timeout_pwr;
  304. u8 timeout_rdy;
  305. u8 timeout_lta;
  306. u8 misc_b;
  307. u8 thresh_swipe;
  308. u8 timeout_tap;
  309. u8 timeout_swipe;
  310. u8 redo_ati;
  311. u8 padding;
  312. struct iqs626_ch_reg_ulp ch_reg_ulp;
  313. struct iqs626_tp_grp_reg tp_grp_reg;
  314. struct iqs626_ch_reg_gen ch_reg_gen[IQS626_NUM_CH_GEN];
  315. struct iqs626_ch_reg_hall ch_reg_hall;
  316. } __packed;
  317. struct iqs626_channel_desc {
  318. const char *name;
  319. int num_ch;
  320. u8 active;
  321. bool events[ARRAY_SIZE(iqs626_events)];
  322. };
  323. static const struct iqs626_channel_desc iqs626_channels[] = {
  324. [IQS626_CH_ULP_0] = {
  325. .name = "ulp-0",
  326. .num_ch = 1,
  327. .active = BIT(0),
  328. .events = {
  329. [IQS626_EVENT_PROX_DN] = true,
  330. [IQS626_EVENT_PROX_UP] = true,
  331. [IQS626_EVENT_TOUCH_DN] = true,
  332. [IQS626_EVENT_TOUCH_UP] = true,
  333. },
  334. },
  335. [IQS626_CH_TP_2] = {
  336. .name = "trackpad-3x2",
  337. .num_ch = IQS626_NUM_CH_TP_2,
  338. .active = BIT(1),
  339. .events = {
  340. [IQS626_EVENT_TOUCH_DN] = true,
  341. },
  342. },
  343. [IQS626_CH_TP_3] = {
  344. .name = "trackpad-3x3",
  345. .num_ch = IQS626_NUM_CH_TP_3,
  346. .active = BIT(2) | BIT(1),
  347. .events = {
  348. [IQS626_EVENT_TOUCH_DN] = true,
  349. },
  350. },
  351. [IQS626_CH_GEN_0] = {
  352. .name = "generic-0",
  353. .num_ch = 1,
  354. .active = BIT(4),
  355. .events = {
  356. [IQS626_EVENT_PROX_DN] = true,
  357. [IQS626_EVENT_PROX_UP] = true,
  358. [IQS626_EVENT_TOUCH_DN] = true,
  359. [IQS626_EVENT_TOUCH_UP] = true,
  360. [IQS626_EVENT_DEEP_DN] = true,
  361. [IQS626_EVENT_DEEP_UP] = true,
  362. },
  363. },
  364. [IQS626_CH_GEN_1] = {
  365. .name = "generic-1",
  366. .num_ch = 1,
  367. .active = BIT(5),
  368. .events = {
  369. [IQS626_EVENT_PROX_DN] = true,
  370. [IQS626_EVENT_PROX_UP] = true,
  371. [IQS626_EVENT_TOUCH_DN] = true,
  372. [IQS626_EVENT_TOUCH_UP] = true,
  373. [IQS626_EVENT_DEEP_DN] = true,
  374. [IQS626_EVENT_DEEP_UP] = true,
  375. },
  376. },
  377. [IQS626_CH_GEN_2] = {
  378. .name = "generic-2",
  379. .num_ch = 1,
  380. .active = BIT(6),
  381. .events = {
  382. [IQS626_EVENT_PROX_DN] = true,
  383. [IQS626_EVENT_PROX_UP] = true,
  384. [IQS626_EVENT_TOUCH_DN] = true,
  385. [IQS626_EVENT_TOUCH_UP] = true,
  386. [IQS626_EVENT_DEEP_DN] = true,
  387. [IQS626_EVENT_DEEP_UP] = true,
  388. },
  389. },
  390. [IQS626_CH_HALL] = {
  391. .name = "hall",
  392. .num_ch = 1,
  393. .active = BIT(7),
  394. .events = {
  395. [IQS626_EVENT_TOUCH_DN] = true,
  396. [IQS626_EVENT_TOUCH_UP] = true,
  397. },
  398. },
  399. };
  400. struct iqs626_private {
  401. struct i2c_client *client;
  402. struct regmap *regmap;
  403. struct iqs626_sys_reg sys_reg;
  404. struct completion ati_done;
  405. struct input_dev *keypad;
  406. struct input_dev *trackpad;
  407. struct touchscreen_properties prop;
  408. unsigned int kp_type[ARRAY_SIZE(iqs626_channels)]
  409. [ARRAY_SIZE(iqs626_events)];
  410. unsigned int kp_code[ARRAY_SIZE(iqs626_channels)]
  411. [ARRAY_SIZE(iqs626_events)];
  412. unsigned int tp_code[IQS626_NUM_GESTURES];
  413. unsigned int suspend_mode;
  414. };
  415. static noinline_for_stack int
  416. iqs626_parse_events(struct iqs626_private *iqs626,
  417. struct fwnode_handle *ch_node, enum iqs626_ch_id ch_id)
  418. {
  419. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  420. struct i2c_client *client = iqs626->client;
  421. struct fwnode_handle *ev_node;
  422. const char *ev_name;
  423. u8 *thresh, *hyst;
  424. unsigned int val;
  425. int i;
  426. switch (ch_id) {
  427. case IQS626_CH_ULP_0:
  428. thresh = sys_reg->ch_reg_ulp.thresh;
  429. hyst = &sys_reg->ch_reg_ulp.hyst;
  430. break;
  431. case IQS626_CH_TP_2:
  432. case IQS626_CH_TP_3:
  433. thresh = &sys_reg->tp_grp_reg.ch_reg_tp[0].thresh;
  434. hyst = &sys_reg->tp_grp_reg.hyst;
  435. break;
  436. case IQS626_CH_GEN_0:
  437. case IQS626_CH_GEN_1:
  438. case IQS626_CH_GEN_2:
  439. i = ch_id - IQS626_CH_GEN_0;
  440. thresh = sys_reg->ch_reg_gen[i].thresh;
  441. hyst = &sys_reg->ch_reg_gen[i].hyst;
  442. break;
  443. case IQS626_CH_HALL:
  444. thresh = &sys_reg->ch_reg_hall.thresh;
  445. hyst = &sys_reg->ch_reg_hall.hyst;
  446. break;
  447. default:
  448. return -EINVAL;
  449. }
  450. for (i = 0; i < ARRAY_SIZE(iqs626_events); i++) {
  451. if (!iqs626_channels[ch_id].events[i])
  452. continue;
  453. if (ch_id == IQS626_CH_TP_2 || ch_id == IQS626_CH_TP_3) {
  454. /*
  455. * Trackpad touch events are simply described under the
  456. * trackpad child node.
  457. */
  458. ev_node = fwnode_handle_get(ch_node);
  459. } else {
  460. ev_name = iqs626_events[i].name;
  461. ev_node = fwnode_get_named_child_node(ch_node, ev_name);
  462. if (!ev_node)
  463. continue;
  464. if (!fwnode_property_read_u32(ev_node, "linux,code",
  465. &val)) {
  466. iqs626->kp_code[ch_id][i] = val;
  467. if (fwnode_property_read_u32(ev_node,
  468. "linux,input-type",
  469. &val)) {
  470. if (ch_id == IQS626_CH_HALL)
  471. val = EV_SW;
  472. else
  473. val = EV_KEY;
  474. }
  475. if (val != EV_KEY && val != EV_SW) {
  476. dev_err(&client->dev,
  477. "Invalid input type: %u\n",
  478. val);
  479. fwnode_handle_put(ev_node);
  480. return -EINVAL;
  481. }
  482. iqs626->kp_type[ch_id][i] = val;
  483. sys_reg->event_mask &= ~iqs626_events[i].mask;
  484. }
  485. }
  486. if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
  487. if (val > IQS626_CHx_HYST_MAX) {
  488. dev_err(&client->dev,
  489. "Invalid %s channel hysteresis: %u\n",
  490. fwnode_get_name(ch_node), val);
  491. fwnode_handle_put(ev_node);
  492. return -EINVAL;
  493. }
  494. if (i == IQS626_EVENT_DEEP_DN ||
  495. i == IQS626_EVENT_DEEP_UP) {
  496. *hyst &= ~IQS626_CHx_HYST_DEEP_MASK;
  497. *hyst |= (val << IQS626_CHx_HYST_DEEP_SHIFT);
  498. } else if (i == IQS626_EVENT_TOUCH_DN ||
  499. i == IQS626_EVENT_TOUCH_UP) {
  500. *hyst &= ~IQS626_CHx_HYST_TOUCH_MASK;
  501. *hyst |= val;
  502. }
  503. }
  504. if (ch_id != IQS626_CH_TP_2 && ch_id != IQS626_CH_TP_3 &&
  505. !fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
  506. if (val > IQS626_CHx_THRESH_MAX) {
  507. dev_err(&client->dev,
  508. "Invalid %s channel threshold: %u\n",
  509. fwnode_get_name(ch_node), val);
  510. fwnode_handle_put(ev_node);
  511. return -EINVAL;
  512. }
  513. if (ch_id == IQS626_CH_HALL)
  514. *thresh = val;
  515. else
  516. *(thresh + iqs626_events[i].th_offs) = val;
  517. }
  518. fwnode_handle_put(ev_node);
  519. }
  520. return 0;
  521. }
  522. static noinline_for_stack int
  523. iqs626_parse_ati_target(struct iqs626_private *iqs626,
  524. struct fwnode_handle *ch_node, enum iqs626_ch_id ch_id)
  525. {
  526. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  527. struct i2c_client *client = iqs626->client;
  528. unsigned int val;
  529. u8 *ati_target;
  530. int i;
  531. switch (ch_id) {
  532. case IQS626_CH_ULP_0:
  533. ati_target = &sys_reg->ch_reg_ulp.ati_target;
  534. break;
  535. case IQS626_CH_TP_2:
  536. case IQS626_CH_TP_3:
  537. ati_target = &sys_reg->tp_grp_reg.ati_target;
  538. break;
  539. case IQS626_CH_GEN_0:
  540. case IQS626_CH_GEN_1:
  541. case IQS626_CH_GEN_2:
  542. i = ch_id - IQS626_CH_GEN_0;
  543. ati_target = &sys_reg->ch_reg_gen[i].ati_target;
  544. break;
  545. case IQS626_CH_HALL:
  546. ati_target = &sys_reg->ch_reg_hall.ati_target;
  547. break;
  548. default:
  549. return -EINVAL;
  550. }
  551. if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
  552. if (val > IQS626_CHx_ATI_TARGET_MAX) {
  553. dev_err(&client->dev,
  554. "Invalid %s channel ATI target: %u\n",
  555. fwnode_get_name(ch_node), val);
  556. return -EINVAL;
  557. }
  558. *ati_target &= ~IQS626_CHx_ATI_TARGET_MASK;
  559. *ati_target |= (val / 32);
  560. }
  561. if (ch_id != IQS626_CH_TP_2 && ch_id != IQS626_CH_TP_3 &&
  562. !fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
  563. switch (val) {
  564. case 75:
  565. val = IQS626_CHx_ATI_BASE_75;
  566. break;
  567. case 100:
  568. val = IQS626_CHx_ATI_BASE_100;
  569. break;
  570. case 150:
  571. val = IQS626_CHx_ATI_BASE_150;
  572. break;
  573. case 200:
  574. val = IQS626_CHx_ATI_BASE_200;
  575. break;
  576. default:
  577. dev_err(&client->dev,
  578. "Invalid %s channel ATI base: %u\n",
  579. fwnode_get_name(ch_node), val);
  580. return -EINVAL;
  581. }
  582. *ati_target &= ~IQS626_CHx_ATI_BASE_MASK;
  583. *ati_target |= val;
  584. }
  585. return 0;
  586. }
  587. static int iqs626_parse_pins(struct iqs626_private *iqs626,
  588. struct fwnode_handle *ch_node,
  589. const char *propname, u8 *enable)
  590. {
  591. struct i2c_client *client = iqs626->client;
  592. unsigned int val[IQS626_NUM_CRx_TX];
  593. int error, count, i;
  594. if (!fwnode_property_present(ch_node, propname))
  595. return 0;
  596. count = fwnode_property_count_u32(ch_node, propname);
  597. if (count > IQS626_NUM_CRx_TX) {
  598. dev_err(&client->dev,
  599. "Too many %s channel CRX/TX pins present\n",
  600. fwnode_get_name(ch_node));
  601. return -EINVAL;
  602. } else if (count < 0) {
  603. dev_err(&client->dev,
  604. "Failed to count %s channel CRX/TX pins: %d\n",
  605. fwnode_get_name(ch_node), count);
  606. return count;
  607. }
  608. error = fwnode_property_read_u32_array(ch_node, propname, val, count);
  609. if (error) {
  610. dev_err(&client->dev,
  611. "Failed to read %s channel CRX/TX pins: %d\n",
  612. fwnode_get_name(ch_node), error);
  613. return error;
  614. }
  615. *enable = 0;
  616. for (i = 0; i < count; i++) {
  617. if (val[i] >= IQS626_NUM_CRx_TX) {
  618. dev_err(&client->dev,
  619. "Invalid %s channel CRX/TX pin: %u\n",
  620. fwnode_get_name(ch_node), val[i]);
  621. return -EINVAL;
  622. }
  623. *enable |= BIT(val[i]);
  624. }
  625. return 0;
  626. }
  627. static int iqs626_parse_trackpad(struct iqs626_private *iqs626,
  628. struct fwnode_handle *ch_node,
  629. enum iqs626_ch_id ch_id)
  630. {
  631. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  632. struct i2c_client *client = iqs626->client;
  633. u8 *hyst = &sys_reg->tp_grp_reg.hyst;
  634. int error, count, i;
  635. unsigned int val;
  636. if (!fwnode_property_read_u32(ch_node, "azoteq,lta-update", &val)) {
  637. if (val > IQS626_MISC_A_TPx_LTA_UPDATE_MAX) {
  638. dev_err(&client->dev,
  639. "Invalid %s channel update rate: %u\n",
  640. fwnode_get_name(ch_node), val);
  641. return -EINVAL;
  642. }
  643. sys_reg->misc_a &= ~IQS626_MISC_A_TPx_LTA_UPDATE_MASK;
  644. sys_reg->misc_a |= (val << IQS626_MISC_A_TPx_LTA_UPDATE_SHIFT);
  645. }
  646. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-trackpad",
  647. &val)) {
  648. if (val > IQS626_FILT_STR_MAX) {
  649. dev_err(&client->dev,
  650. "Invalid %s channel filter strength: %u\n",
  651. fwnode_get_name(ch_node), val);
  652. return -EINVAL;
  653. }
  654. sys_reg->misc_b &= ~IQS626_MISC_B_FILT_STR_TPx;
  655. sys_reg->misc_b |= val;
  656. }
  657. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-cnt",
  658. &val)) {
  659. if (val > IQS626_FILT_STR_MAX) {
  660. dev_err(&client->dev,
  661. "Invalid %s channel filter strength: %u\n",
  662. fwnode_get_name(ch_node), val);
  663. return -EINVAL;
  664. }
  665. *hyst &= ~IQS626_FILT_STR_NP_TPx_MASK;
  666. *hyst |= (val << IQS626_FILT_STR_NP_TPx_SHIFT);
  667. }
  668. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-cnt",
  669. &val)) {
  670. if (val > IQS626_FILT_STR_MAX) {
  671. dev_err(&client->dev,
  672. "Invalid %s channel filter strength: %u\n",
  673. fwnode_get_name(ch_node), val);
  674. return -EINVAL;
  675. }
  676. *hyst &= ~IQS626_FILT_STR_LP_TPx_MASK;
  677. *hyst |= (val << IQS626_FILT_STR_LP_TPx_SHIFT);
  678. }
  679. for (i = 0; i < iqs626_channels[ch_id].num_ch; i++) {
  680. u8 *ati_base = &sys_reg->tp_grp_reg.ch_reg_tp[i].ati_base;
  681. u8 *thresh = &sys_reg->tp_grp_reg.ch_reg_tp[i].thresh;
  682. struct fwnode_handle *tc_node;
  683. char tc_name[10];
  684. snprintf(tc_name, sizeof(tc_name), "channel-%d", i);
  685. tc_node = fwnode_get_named_child_node(ch_node, tc_name);
  686. if (!tc_node)
  687. continue;
  688. if (!fwnode_property_read_u32(tc_node, "azoteq,ati-base",
  689. &val)) {
  690. if (val < IQS626_TPx_ATI_BASE_MIN ||
  691. val > IQS626_TPx_ATI_BASE_MAX) {
  692. dev_err(&client->dev,
  693. "Invalid %s %s ATI base: %u\n",
  694. fwnode_get_name(ch_node), tc_name, val);
  695. fwnode_handle_put(tc_node);
  696. return -EINVAL;
  697. }
  698. *ati_base = val - IQS626_TPx_ATI_BASE_MIN;
  699. }
  700. if (!fwnode_property_read_u32(tc_node, "azoteq,thresh",
  701. &val)) {
  702. if (val > IQS626_CHx_THRESH_MAX) {
  703. dev_err(&client->dev,
  704. "Invalid %s %s threshold: %u\n",
  705. fwnode_get_name(ch_node), tc_name, val);
  706. fwnode_handle_put(tc_node);
  707. return -EINVAL;
  708. }
  709. *thresh = val;
  710. }
  711. fwnode_handle_put(tc_node);
  712. }
  713. if (!fwnode_property_present(ch_node, "linux,keycodes"))
  714. return 0;
  715. count = fwnode_property_count_u32(ch_node, "linux,keycodes");
  716. if (count > IQS626_NUM_GESTURES) {
  717. dev_err(&client->dev, "Too many keycodes present\n");
  718. return -EINVAL;
  719. } else if (count < 0) {
  720. dev_err(&client->dev, "Failed to count keycodes: %d\n", count);
  721. return count;
  722. }
  723. error = fwnode_property_read_u32_array(ch_node, "linux,keycodes",
  724. iqs626->tp_code, count);
  725. if (error) {
  726. dev_err(&client->dev, "Failed to read keycodes: %d\n", error);
  727. return error;
  728. }
  729. sys_reg->misc_b &= ~IQS626_MISC_B_TPx_SWIPE;
  730. if (fwnode_property_present(ch_node, "azoteq,gesture-swipe"))
  731. sys_reg->misc_b |= IQS626_MISC_B_TPx_SWIPE;
  732. if (!fwnode_property_read_u32(ch_node, "azoteq,timeout-tap-ms",
  733. &val)) {
  734. if (val > IQS626_TIMEOUT_TAP_MS_MAX) {
  735. dev_err(&client->dev,
  736. "Invalid %s channel timeout: %u\n",
  737. fwnode_get_name(ch_node), val);
  738. return -EINVAL;
  739. }
  740. sys_reg->timeout_tap = val / 16;
  741. }
  742. if (!fwnode_property_read_u32(ch_node, "azoteq,timeout-swipe-ms",
  743. &val)) {
  744. if (val > IQS626_TIMEOUT_SWIPE_MS_MAX) {
  745. dev_err(&client->dev,
  746. "Invalid %s channel timeout: %u\n",
  747. fwnode_get_name(ch_node), val);
  748. return -EINVAL;
  749. }
  750. sys_reg->timeout_swipe = val / 16;
  751. }
  752. if (!fwnode_property_read_u32(ch_node, "azoteq,thresh-swipe",
  753. &val)) {
  754. if (val > IQS626_THRESH_SWIPE_MAX) {
  755. dev_err(&client->dev,
  756. "Invalid %s channel threshold: %u\n",
  757. fwnode_get_name(ch_node), val);
  758. return -EINVAL;
  759. }
  760. sys_reg->thresh_swipe = val;
  761. }
  762. sys_reg->event_mask &= ~IQS626_EVENT_MASK_GESTURE;
  763. return 0;
  764. }
  765. static noinline_for_stack int
  766. iqs626_parse_channel(struct iqs626_private *iqs626,
  767. struct fwnode_handle *ch_node, enum iqs626_ch_id ch_id)
  768. {
  769. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  770. struct i2c_client *client = iqs626->client;
  771. u8 *engine, *filter, *rx_enable, *tx_enable;
  772. u8 *assoc_select, *assoc_weight;
  773. unsigned int val;
  774. int error, i;
  775. switch (ch_id) {
  776. case IQS626_CH_ULP_0:
  777. engine = sys_reg->ch_reg_ulp.engine;
  778. break;
  779. case IQS626_CH_TP_2:
  780. case IQS626_CH_TP_3:
  781. engine = sys_reg->tp_grp_reg.engine;
  782. break;
  783. case IQS626_CH_GEN_0:
  784. case IQS626_CH_GEN_1:
  785. case IQS626_CH_GEN_2:
  786. i = ch_id - IQS626_CH_GEN_0;
  787. engine = sys_reg->ch_reg_gen[i].engine;
  788. break;
  789. case IQS626_CH_HALL:
  790. engine = &sys_reg->ch_reg_hall.engine;
  791. break;
  792. default:
  793. return -EINVAL;
  794. }
  795. error = iqs626_parse_ati_target(iqs626, ch_node, ch_id);
  796. if (error)
  797. return error;
  798. error = iqs626_parse_events(iqs626, ch_node, ch_id);
  799. if (error)
  800. return error;
  801. if (!fwnode_property_present(ch_node, "azoteq,ati-exclude"))
  802. sys_reg->redo_ati |= iqs626_channels[ch_id].active;
  803. if (!fwnode_property_present(ch_node, "azoteq,reseed-disable"))
  804. sys_reg->reseed |= iqs626_channels[ch_id].active;
  805. *engine |= IQS626_CHx_ENG_0_MEAS_CAP_SIZE;
  806. if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease"))
  807. *engine &= ~IQS626_CHx_ENG_0_MEAS_CAP_SIZE;
  808. *engine |= IQS626_CHx_ENG_0_RX_TERM_VSS;
  809. if (!fwnode_property_read_u32(ch_node, "azoteq,rx-inactive", &val)) {
  810. switch (val) {
  811. case IQS626_RX_INACTIVE_VSS:
  812. break;
  813. case IQS626_RX_INACTIVE_FLOAT:
  814. *engine &= ~IQS626_CHx_ENG_0_RX_TERM_VSS;
  815. if (ch_id == IQS626_CH_GEN_0 ||
  816. ch_id == IQS626_CH_GEN_1 ||
  817. ch_id == IQS626_CH_GEN_2)
  818. *(engine + 4) &= ~IQS626_CHx_ENG_4_RX_TERM_VREG;
  819. break;
  820. case IQS626_RX_INACTIVE_VREG:
  821. if (ch_id == IQS626_CH_GEN_0 ||
  822. ch_id == IQS626_CH_GEN_1 ||
  823. ch_id == IQS626_CH_GEN_2) {
  824. *engine &= ~IQS626_CHx_ENG_0_RX_TERM_VSS;
  825. *(engine + 4) |= IQS626_CHx_ENG_4_RX_TERM_VREG;
  826. break;
  827. }
  828. fallthrough;
  829. default:
  830. dev_err(&client->dev,
  831. "Invalid %s channel CRX pin termination: %u\n",
  832. fwnode_get_name(ch_node), val);
  833. return -EINVAL;
  834. }
  835. }
  836. *engine &= ~IQS626_CHx_ENG_0_LINEARIZE;
  837. if (fwnode_property_present(ch_node, "azoteq,linearize"))
  838. *engine |= IQS626_CHx_ENG_0_LINEARIZE;
  839. *engine &= ~IQS626_CHx_ENG_0_DUAL_DIR;
  840. if (fwnode_property_present(ch_node, "azoteq,dual-direction"))
  841. *engine |= IQS626_CHx_ENG_0_DUAL_DIR;
  842. *engine &= ~IQS626_CHx_ENG_0_FILT_DISABLE;
  843. if (fwnode_property_present(ch_node, "azoteq,filt-disable"))
  844. *engine |= IQS626_CHx_ENG_0_FILT_DISABLE;
  845. if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
  846. if (val > IQS626_CHx_ENG_0_ATI_MODE_MAX) {
  847. dev_err(&client->dev,
  848. "Invalid %s channel ATI mode: %u\n",
  849. fwnode_get_name(ch_node), val);
  850. return -EINVAL;
  851. }
  852. *engine &= ~IQS626_CHx_ENG_0_ATI_MODE_MASK;
  853. *engine |= val;
  854. }
  855. if (ch_id == IQS626_CH_HALL)
  856. return 0;
  857. *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_ENABLE;
  858. if (!fwnode_property_read_u32(ch_node, "azoteq,cct-increase",
  859. &val) && val) {
  860. unsigned int orig_val = val--;
  861. /*
  862. * In the case of the generic channels, the charge cycle time
  863. * field doubles in size and straddles two separate registers.
  864. */
  865. if (ch_id == IQS626_CH_GEN_0 ||
  866. ch_id == IQS626_CH_GEN_1 ||
  867. ch_id == IQS626_CH_GEN_2) {
  868. *(engine + 4) &= ~IQS626_CHx_ENG_4_CCT_LOW_1;
  869. if (val & BIT(1))
  870. *(engine + 4) |= IQS626_CHx_ENG_4_CCT_LOW_1;
  871. *(engine + 4) &= ~IQS626_CHx_ENG_4_CCT_LOW_0;
  872. if (val & BIT(0))
  873. *(engine + 4) |= IQS626_CHx_ENG_4_CCT_LOW_0;
  874. val >>= 2;
  875. }
  876. if (val & ~GENMASK(1, 0)) {
  877. dev_err(&client->dev,
  878. "Invalid %s channel charge cycle time: %u\n",
  879. fwnode_get_name(ch_node), orig_val);
  880. return -EINVAL;
  881. }
  882. *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_HIGH_1;
  883. if (val & BIT(1))
  884. *(engine + 1) |= IQS626_CHx_ENG_1_CCT_HIGH_1;
  885. *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_HIGH_0;
  886. if (val & BIT(0))
  887. *(engine + 1) |= IQS626_CHx_ENG_1_CCT_HIGH_0;
  888. *(engine + 1) |= IQS626_CHx_ENG_1_CCT_ENABLE;
  889. }
  890. if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
  891. if (val > IQS626_CHx_ENG_1_PROJ_BIAS_MAX) {
  892. dev_err(&client->dev,
  893. "Invalid %s channel bias current: %u\n",
  894. fwnode_get_name(ch_node), val);
  895. return -EINVAL;
  896. }
  897. *(engine + 1) &= ~IQS626_CHx_ENG_1_PROJ_BIAS_MASK;
  898. *(engine + 1) |= (val << IQS626_CHx_ENG_1_PROJ_BIAS_SHIFT);
  899. }
  900. if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
  901. if (val > IQS626_CHx_ENG_1_SENSE_FREQ_MAX) {
  902. dev_err(&client->dev,
  903. "Invalid %s channel sensing frequency: %u\n",
  904. fwnode_get_name(ch_node), val);
  905. return -EINVAL;
  906. }
  907. *(engine + 1) &= ~IQS626_CHx_ENG_1_SENSE_FREQ_MASK;
  908. *(engine + 1) |= (val << IQS626_CHx_ENG_1_SENSE_FREQ_SHIFT);
  909. }
  910. *(engine + 1) &= ~IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN;
  911. if (fwnode_property_present(ch_node, "azoteq,ati-band-tighten"))
  912. *(engine + 1) |= IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN;
  913. if (ch_id == IQS626_CH_TP_2 || ch_id == IQS626_CH_TP_3)
  914. return iqs626_parse_trackpad(iqs626, ch_node, ch_id);
  915. if (ch_id == IQS626_CH_ULP_0) {
  916. sys_reg->ch_reg_ulp.hyst &= ~IQS626_ULP_PROJ_ENABLE;
  917. if (fwnode_property_present(ch_node, "azoteq,proj-enable"))
  918. sys_reg->ch_reg_ulp.hyst |= IQS626_ULP_PROJ_ENABLE;
  919. filter = &sys_reg->ch_reg_ulp.filter;
  920. rx_enable = &sys_reg->ch_reg_ulp.rx_enable;
  921. tx_enable = &sys_reg->ch_reg_ulp.tx_enable;
  922. } else {
  923. i = ch_id - IQS626_CH_GEN_0;
  924. filter = &sys_reg->ch_reg_gen[i].filter;
  925. rx_enable = &sys_reg->ch_reg_gen[i].rx_enable;
  926. tx_enable = &sys_reg->ch_reg_gen[i].tx_enable;
  927. }
  928. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-cnt",
  929. &val)) {
  930. if (val > IQS626_FILT_STR_MAX) {
  931. dev_err(&client->dev,
  932. "Invalid %s channel filter strength: %u\n",
  933. fwnode_get_name(ch_node), val);
  934. return -EINVAL;
  935. }
  936. *filter &= ~IQS626_FILT_STR_NP_CNT_MASK;
  937. *filter |= (val << IQS626_FILT_STR_NP_CNT_SHIFT);
  938. }
  939. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-cnt",
  940. &val)) {
  941. if (val > IQS626_FILT_STR_MAX) {
  942. dev_err(&client->dev,
  943. "Invalid %s channel filter strength: %u\n",
  944. fwnode_get_name(ch_node), val);
  945. return -EINVAL;
  946. }
  947. *filter &= ~IQS626_FILT_STR_LP_CNT_MASK;
  948. *filter |= (val << IQS626_FILT_STR_LP_CNT_SHIFT);
  949. }
  950. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-lta",
  951. &val)) {
  952. if (val > IQS626_FILT_STR_MAX) {
  953. dev_err(&client->dev,
  954. "Invalid %s channel filter strength: %u\n",
  955. fwnode_get_name(ch_node), val);
  956. return -EINVAL;
  957. }
  958. *filter &= ~IQS626_FILT_STR_NP_LTA_MASK;
  959. *filter |= (val << IQS626_FILT_STR_NP_LTA_SHIFT);
  960. }
  961. if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-lta",
  962. &val)) {
  963. if (val > IQS626_FILT_STR_MAX) {
  964. dev_err(&client->dev,
  965. "Invalid %s channel filter strength: %u\n",
  966. fwnode_get_name(ch_node), val);
  967. return -EINVAL;
  968. }
  969. *filter &= ~IQS626_FILT_STR_LP_LTA_MASK;
  970. *filter |= val;
  971. }
  972. error = iqs626_parse_pins(iqs626, ch_node, "azoteq,rx-enable",
  973. rx_enable);
  974. if (error)
  975. return error;
  976. error = iqs626_parse_pins(iqs626, ch_node, "azoteq,tx-enable",
  977. tx_enable);
  978. if (error)
  979. return error;
  980. if (ch_id == IQS626_CH_ULP_0)
  981. return 0;
  982. *(engine + 2) &= ~IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE;
  983. if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size",
  984. &val) && val) {
  985. unsigned int orig_val = val--;
  986. if (val > IQS626_CHx_ENG_2_LOCAL_CAP_MAX) {
  987. dev_err(&client->dev,
  988. "Invalid %s channel local cap. size: %u\n",
  989. fwnode_get_name(ch_node), orig_val);
  990. return -EINVAL;
  991. }
  992. *(engine + 2) &= ~IQS626_CHx_ENG_2_LOCAL_CAP_MASK;
  993. *(engine + 2) |= (val << IQS626_CHx_ENG_2_LOCAL_CAP_SHIFT);
  994. *(engine + 2) |= IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE;
  995. }
  996. if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
  997. if (val > IQS626_CHx_ENG_2_SENSE_MODE_MAX) {
  998. dev_err(&client->dev,
  999. "Invalid %s channel sensing mode: %u\n",
  1000. fwnode_get_name(ch_node), val);
  1001. return -EINVAL;
  1002. }
  1003. *(engine + 2) &= ~IQS626_CHx_ENG_2_SENSE_MODE_MASK;
  1004. *(engine + 2) |= val;
  1005. }
  1006. if (!fwnode_property_read_u32(ch_node, "azoteq,tx-freq", &val)) {
  1007. if (val > IQS626_CHx_ENG_3_TX_FREQ_MAX) {
  1008. dev_err(&client->dev,
  1009. "Invalid %s channel excitation frequency: %u\n",
  1010. fwnode_get_name(ch_node), val);
  1011. return -EINVAL;
  1012. }
  1013. *(engine + 3) &= ~IQS626_CHx_ENG_3_TX_FREQ_MASK;
  1014. *(engine + 3) |= (val << IQS626_CHx_ENG_3_TX_FREQ_SHIFT);
  1015. }
  1016. *(engine + 3) &= ~IQS626_CHx_ENG_3_INV_LOGIC;
  1017. if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
  1018. *(engine + 3) |= IQS626_CHx_ENG_3_INV_LOGIC;
  1019. *(engine + 4) &= ~IQS626_CHx_ENG_4_COMP_DISABLE;
  1020. if (fwnode_property_present(ch_node, "azoteq,comp-disable"))
  1021. *(engine + 4) |= IQS626_CHx_ENG_4_COMP_DISABLE;
  1022. *(engine + 4) &= ~IQS626_CHx_ENG_4_STATIC_ENABLE;
  1023. if (fwnode_property_present(ch_node, "azoteq,static-enable"))
  1024. *(engine + 4) |= IQS626_CHx_ENG_4_STATIC_ENABLE;
  1025. i = ch_id - IQS626_CH_GEN_0;
  1026. assoc_select = &sys_reg->ch_reg_gen[i].assoc_select;
  1027. assoc_weight = &sys_reg->ch_reg_gen[i].assoc_weight;
  1028. *assoc_select = 0;
  1029. if (!fwnode_property_present(ch_node, "azoteq,assoc-select"))
  1030. return 0;
  1031. for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
  1032. if (fwnode_property_match_string(ch_node, "azoteq,assoc-select",
  1033. iqs626_channels[i].name) < 0)
  1034. continue;
  1035. *assoc_select |= iqs626_channels[i].active;
  1036. }
  1037. if (fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val))
  1038. return 0;
  1039. if (val > IQS626_GEN_WEIGHT_MAX) {
  1040. dev_err(&client->dev,
  1041. "Invalid %s channel associated weight: %u\n",
  1042. fwnode_get_name(ch_node), val);
  1043. return -EINVAL;
  1044. }
  1045. *assoc_weight = val;
  1046. return 0;
  1047. }
  1048. static int iqs626_parse_prop(struct iqs626_private *iqs626)
  1049. {
  1050. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  1051. struct i2c_client *client = iqs626->client;
  1052. struct fwnode_handle *ch_node;
  1053. unsigned int val;
  1054. int error, i;
  1055. u16 general;
  1056. if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
  1057. &val)) {
  1058. if (val > IQS626_SYS_SETTINGS_PWR_MODE_MAX) {
  1059. dev_err(&client->dev, "Invalid suspend mode: %u\n",
  1060. val);
  1061. return -EINVAL;
  1062. }
  1063. iqs626->suspend_mode = val;
  1064. }
  1065. error = regmap_raw_read(iqs626->regmap, IQS626_SYS_SETTINGS, sys_reg,
  1066. sizeof(*sys_reg));
  1067. if (error)
  1068. return error;
  1069. general = be16_to_cpu(sys_reg->general);
  1070. general &= IQS626_SYS_SETTINGS_ULP_UPDATE_MASK;
  1071. if (device_property_present(&client->dev, "azoteq,clk-div"))
  1072. general |= IQS626_SYS_SETTINGS_CLK_DIV;
  1073. if (device_property_present(&client->dev, "azoteq,ulp-enable"))
  1074. general |= IQS626_SYS_SETTINGS_ULP_AUTO;
  1075. if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
  1076. &val)) {
  1077. if (val > IQS626_SYS_SETTINGS_ULP_UPDATE_MAX) {
  1078. dev_err(&client->dev, "Invalid update rate: %u\n", val);
  1079. return -EINVAL;
  1080. }
  1081. general &= ~IQS626_SYS_SETTINGS_ULP_UPDATE_MASK;
  1082. general |= (val << IQS626_SYS_SETTINGS_ULP_UPDATE_SHIFT);
  1083. }
  1084. sys_reg->misc_a &= ~IQS626_MISC_A_ATI_BAND_DISABLE;
  1085. if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
  1086. sys_reg->misc_a |= IQS626_MISC_A_ATI_BAND_DISABLE;
  1087. sys_reg->misc_a &= ~IQS626_MISC_A_ATI_LP_ONLY;
  1088. if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
  1089. sys_reg->misc_a |= IQS626_MISC_A_ATI_LP_ONLY;
  1090. if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
  1091. &val)) {
  1092. if (val > IQS626_MISC_A_GPIO3_SELECT_MAX) {
  1093. dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
  1094. val);
  1095. return -EINVAL;
  1096. }
  1097. sys_reg->misc_a &= ~IQS626_MISC_A_GPIO3_SELECT_MASK;
  1098. sys_reg->misc_a |= val;
  1099. }
  1100. if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
  1101. &val)) {
  1102. if (val > IQS626_MISC_B_RESEED_UI_SEL_MAX) {
  1103. dev_err(&client->dev, "Invalid reseed selection: %u\n",
  1104. val);
  1105. return -EINVAL;
  1106. }
  1107. sys_reg->misc_b &= ~IQS626_MISC_B_RESEED_UI_SEL_MASK;
  1108. sys_reg->misc_b |= (val << IQS626_MISC_B_RESEED_UI_SEL_SHIFT);
  1109. }
  1110. sys_reg->misc_b &= ~IQS626_MISC_B_THRESH_EXTEND;
  1111. if (device_property_present(&client->dev, "azoteq,thresh-extend"))
  1112. sys_reg->misc_b |= IQS626_MISC_B_THRESH_EXTEND;
  1113. sys_reg->misc_b &= ~IQS626_MISC_B_TRACKING_UI_ENABLE;
  1114. if (device_property_present(&client->dev, "azoteq,tracking-enable"))
  1115. sys_reg->misc_b |= IQS626_MISC_B_TRACKING_UI_ENABLE;
  1116. sys_reg->misc_b &= ~IQS626_MISC_B_RESEED_OFFSET;
  1117. if (device_property_present(&client->dev, "azoteq,reseed-offset"))
  1118. sys_reg->misc_b |= IQS626_MISC_B_RESEED_OFFSET;
  1119. if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
  1120. &val)) {
  1121. if (val > IQS626_RATE_NP_MS_MAX) {
  1122. dev_err(&client->dev, "Invalid report rate: %u\n", val);
  1123. return -EINVAL;
  1124. }
  1125. sys_reg->rate_np = val;
  1126. }
  1127. if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
  1128. &val)) {
  1129. if (val > IQS626_RATE_LP_MS_MAX) {
  1130. dev_err(&client->dev, "Invalid report rate: %u\n", val);
  1131. return -EINVAL;
  1132. }
  1133. sys_reg->rate_lp = val;
  1134. }
  1135. if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
  1136. &val)) {
  1137. if (val > IQS626_RATE_ULP_MS_MAX) {
  1138. dev_err(&client->dev, "Invalid report rate: %u\n", val);
  1139. return -EINVAL;
  1140. }
  1141. sys_reg->rate_ulp = val / 16;
  1142. }
  1143. if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
  1144. &val)) {
  1145. if (val > IQS626_TIMEOUT_PWR_MS_MAX) {
  1146. dev_err(&client->dev, "Invalid timeout: %u\n", val);
  1147. return -EINVAL;
  1148. }
  1149. sys_reg->timeout_pwr = val / 512;
  1150. }
  1151. if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
  1152. &val)) {
  1153. if (val > IQS626_TIMEOUT_LTA_MS_MAX) {
  1154. dev_err(&client->dev, "Invalid timeout: %u\n", val);
  1155. return -EINVAL;
  1156. }
  1157. sys_reg->timeout_lta = val / 512;
  1158. }
  1159. sys_reg->event_mask = ~((u8)IQS626_EVENT_MASK_SYS);
  1160. sys_reg->redo_ati = 0;
  1161. sys_reg->reseed = 0;
  1162. sys_reg->active = 0;
  1163. for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
  1164. ch_node = device_get_named_child_node(&client->dev,
  1165. iqs626_channels[i].name);
  1166. if (!ch_node)
  1167. continue;
  1168. error = iqs626_parse_channel(iqs626, ch_node, i);
  1169. fwnode_handle_put(ch_node);
  1170. if (error)
  1171. return error;
  1172. sys_reg->active |= iqs626_channels[i].active;
  1173. }
  1174. general |= IQS626_SYS_SETTINGS_EVENT_MODE;
  1175. /*
  1176. * Enable streaming during normal-power mode if the trackpad is used to
  1177. * report raw coordinates instead of gestures. In that case, the device
  1178. * returns to event mode during low-power mode.
  1179. */
  1180. if (sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active &&
  1181. sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE)
  1182. general |= IQS626_SYS_SETTINGS_EVENT_MODE_LP;
  1183. general |= IQS626_SYS_SETTINGS_REDO_ATI;
  1184. general |= IQS626_SYS_SETTINGS_ACK_RESET;
  1185. sys_reg->general = cpu_to_be16(general);
  1186. error = regmap_raw_write(iqs626->regmap, IQS626_SYS_SETTINGS,
  1187. &iqs626->sys_reg, sizeof(iqs626->sys_reg));
  1188. if (error)
  1189. return error;
  1190. iqs626_irq_wait();
  1191. return 0;
  1192. }
  1193. static int iqs626_input_init(struct iqs626_private *iqs626)
  1194. {
  1195. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  1196. struct i2c_client *client = iqs626->client;
  1197. int error, i, j;
  1198. iqs626->keypad = devm_input_allocate_device(&client->dev);
  1199. if (!iqs626->keypad)
  1200. return -ENOMEM;
  1201. iqs626->keypad->keycodemax = ARRAY_SIZE(iqs626->kp_code);
  1202. iqs626->keypad->keycode = iqs626->kp_code;
  1203. iqs626->keypad->keycodesize = sizeof(**iqs626->kp_code);
  1204. iqs626->keypad->name = "iqs626a_keypad";
  1205. iqs626->keypad->id.bustype = BUS_I2C;
  1206. for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
  1207. if (!(sys_reg->active & iqs626_channels[i].active))
  1208. continue;
  1209. for (j = 0; j < ARRAY_SIZE(iqs626_events); j++) {
  1210. if (!iqs626->kp_type[i][j])
  1211. continue;
  1212. input_set_capability(iqs626->keypad,
  1213. iqs626->kp_type[i][j],
  1214. iqs626->kp_code[i][j]);
  1215. }
  1216. }
  1217. if (!(sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active))
  1218. return 0;
  1219. iqs626->trackpad = devm_input_allocate_device(&client->dev);
  1220. if (!iqs626->trackpad)
  1221. return -ENOMEM;
  1222. iqs626->trackpad->keycodemax = ARRAY_SIZE(iqs626->tp_code);
  1223. iqs626->trackpad->keycode = iqs626->tp_code;
  1224. iqs626->trackpad->keycodesize = sizeof(*iqs626->tp_code);
  1225. iqs626->trackpad->name = "iqs626a_trackpad";
  1226. iqs626->trackpad->id.bustype = BUS_I2C;
  1227. /*
  1228. * Present the trackpad as a traditional pointing device if no gestures
  1229. * have been mapped to a keycode.
  1230. */
  1231. if (sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE) {
  1232. u8 tp_mask = iqs626_channels[IQS626_CH_TP_3].active;
  1233. input_set_capability(iqs626->trackpad, EV_KEY, BTN_TOUCH);
  1234. input_set_abs_params(iqs626->trackpad, ABS_Y, 0, 255, 0, 0);
  1235. if ((sys_reg->active & tp_mask) == tp_mask)
  1236. input_set_abs_params(iqs626->trackpad,
  1237. ABS_X, 0, 255, 0, 0);
  1238. else
  1239. input_set_abs_params(iqs626->trackpad,
  1240. ABS_X, 0, 128, 0, 0);
  1241. touchscreen_parse_properties(iqs626->trackpad, false,
  1242. &iqs626->prop);
  1243. } else {
  1244. for (i = 0; i < IQS626_NUM_GESTURES; i++)
  1245. if (iqs626->tp_code[i] != KEY_RESERVED)
  1246. input_set_capability(iqs626->trackpad, EV_KEY,
  1247. iqs626->tp_code[i]);
  1248. }
  1249. error = input_register_device(iqs626->trackpad);
  1250. if (error)
  1251. dev_err(&client->dev, "Failed to register trackpad: %d\n",
  1252. error);
  1253. return error;
  1254. }
  1255. static int iqs626_report(struct iqs626_private *iqs626)
  1256. {
  1257. struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
  1258. struct i2c_client *client = iqs626->client;
  1259. struct iqs626_flags flags;
  1260. __le16 hall_output;
  1261. int error, i, j;
  1262. u8 state;
  1263. u8 *dir_mask = &flags.states[IQS626_ST_OFFS_DIR];
  1264. error = regmap_raw_read(iqs626->regmap, IQS626_SYS_FLAGS, &flags,
  1265. sizeof(flags));
  1266. if (error) {
  1267. dev_err(&client->dev, "Failed to read device status: %d\n",
  1268. error);
  1269. return error;
  1270. }
  1271. /*
  1272. * The device resets itself if its own watchdog bites, which can happen
  1273. * in the event of an I2C communication error. In this case, the device
  1274. * asserts a SHOW_RESET interrupt and all registers must be restored.
  1275. */
  1276. if (be16_to_cpu(flags.system) & IQS626_SYS_FLAGS_SHOW_RESET) {
  1277. dev_err(&client->dev, "Unexpected device reset\n");
  1278. error = regmap_raw_write(iqs626->regmap, IQS626_SYS_SETTINGS,
  1279. sys_reg, sizeof(*sys_reg));
  1280. if (error)
  1281. dev_err(&client->dev,
  1282. "Failed to re-initialize device: %d\n", error);
  1283. return error;
  1284. }
  1285. if (be16_to_cpu(flags.system) & IQS626_SYS_FLAGS_IN_ATI)
  1286. return 0;
  1287. /*
  1288. * Unlike the ULP or generic channels, the Hall channel does not have a
  1289. * direction flag. Instead, the direction (i.e. magnet polarity) can be
  1290. * derived based on the sign of the 2's complement differential output.
  1291. */
  1292. if (sys_reg->active & iqs626_channels[IQS626_CH_HALL].active) {
  1293. error = regmap_raw_read(iqs626->regmap, IQS626_HALL_OUTPUT,
  1294. &hall_output, sizeof(hall_output));
  1295. if (error) {
  1296. dev_err(&client->dev,
  1297. "Failed to read Hall output: %d\n", error);
  1298. return error;
  1299. }
  1300. *dir_mask &= ~iqs626_channels[IQS626_CH_HALL].active;
  1301. if (le16_to_cpu(hall_output) < 0x8000)
  1302. *dir_mask |= iqs626_channels[IQS626_CH_HALL].active;
  1303. }
  1304. for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
  1305. if (!(sys_reg->active & iqs626_channels[i].active))
  1306. continue;
  1307. for (j = 0; j < ARRAY_SIZE(iqs626_events); j++) {
  1308. if (!iqs626->kp_type[i][j])
  1309. continue;
  1310. state = flags.states[iqs626_events[j].st_offs];
  1311. state &= iqs626_events[j].dir_up ? *dir_mask
  1312. : ~(*dir_mask);
  1313. state &= iqs626_channels[i].active;
  1314. input_event(iqs626->keypad, iqs626->kp_type[i][j],
  1315. iqs626->kp_code[i][j], !!state);
  1316. }
  1317. }
  1318. input_sync(iqs626->keypad);
  1319. /*
  1320. * The following completion signals that ATI has finished, any initial
  1321. * switch states have been reported and the keypad can be registered.
  1322. */
  1323. complete_all(&iqs626->ati_done);
  1324. if (!(sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active))
  1325. return 0;
  1326. if (sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE) {
  1327. state = flags.states[IQS626_ST_OFFS_TOUCH];
  1328. state &= iqs626_channels[IQS626_CH_TP_2].active;
  1329. input_report_key(iqs626->trackpad, BTN_TOUCH, state);
  1330. if (state)
  1331. touchscreen_report_pos(iqs626->trackpad, &iqs626->prop,
  1332. flags.trackpad_x,
  1333. flags.trackpad_y, false);
  1334. } else {
  1335. for (i = 0; i < IQS626_NUM_GESTURES; i++)
  1336. input_report_key(iqs626->trackpad, iqs626->tp_code[i],
  1337. flags.gesture & BIT(i));
  1338. if (flags.gesture & GENMASK(IQS626_GESTURE_TAP, 0)) {
  1339. input_sync(iqs626->trackpad);
  1340. /*
  1341. * Momentary gestures are followed by a complementary
  1342. * release cycle so as to emulate a full keystroke.
  1343. */
  1344. for (i = 0; i < IQS626_GESTURE_HOLD; i++)
  1345. input_report_key(iqs626->trackpad,
  1346. iqs626->tp_code[i], 0);
  1347. }
  1348. }
  1349. input_sync(iqs626->trackpad);
  1350. return 0;
  1351. }
  1352. static irqreturn_t iqs626_irq(int irq, void *context)
  1353. {
  1354. struct iqs626_private *iqs626 = context;
  1355. if (iqs626_report(iqs626))
  1356. return IRQ_NONE;
  1357. /*
  1358. * The device does not deassert its interrupt (RDY) pin until shortly
  1359. * after receiving an I2C stop condition; the following delay ensures
  1360. * the interrupt handler does not return before this time.
  1361. */
  1362. iqs626_irq_wait();
  1363. return IRQ_HANDLED;
  1364. }
  1365. static const struct regmap_config iqs626_regmap_config = {
  1366. .reg_bits = 8,
  1367. .val_bits = 16,
  1368. .max_register = IQS626_MAX_REG,
  1369. };
  1370. static int iqs626_probe(struct i2c_client *client)
  1371. {
  1372. struct iqs626_ver_info ver_info;
  1373. struct iqs626_private *iqs626;
  1374. int error;
  1375. iqs626 = devm_kzalloc(&client->dev, sizeof(*iqs626), GFP_KERNEL);
  1376. if (!iqs626)
  1377. return -ENOMEM;
  1378. i2c_set_clientdata(client, iqs626);
  1379. iqs626->client = client;
  1380. iqs626->regmap = devm_regmap_init_i2c(client, &iqs626_regmap_config);
  1381. if (IS_ERR(iqs626->regmap)) {
  1382. error = PTR_ERR(iqs626->regmap);
  1383. dev_err(&client->dev, "Failed to initialize register map: %d\n",
  1384. error);
  1385. return error;
  1386. }
  1387. init_completion(&iqs626->ati_done);
  1388. error = regmap_raw_read(iqs626->regmap, IQS626_VER_INFO, &ver_info,
  1389. sizeof(ver_info));
  1390. if (error)
  1391. return error;
  1392. if (ver_info.prod_num != IQS626_VER_INFO_PROD_NUM) {
  1393. dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
  1394. ver_info.prod_num);
  1395. return -EINVAL;
  1396. }
  1397. error = iqs626_parse_prop(iqs626);
  1398. if (error)
  1399. return error;
  1400. error = iqs626_input_init(iqs626);
  1401. if (error)
  1402. return error;
  1403. error = devm_request_threaded_irq(&client->dev, client->irq,
  1404. NULL, iqs626_irq, IRQF_ONESHOT,
  1405. client->name, iqs626);
  1406. if (error) {
  1407. dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
  1408. return error;
  1409. }
  1410. if (!wait_for_completion_timeout(&iqs626->ati_done,
  1411. msecs_to_jiffies(2000))) {
  1412. dev_err(&client->dev, "Failed to complete ATI\n");
  1413. return -ETIMEDOUT;
  1414. }
  1415. /*
  1416. * The keypad may include one or more switches and is not registered
  1417. * until ATI is complete and the initial switch states are read.
  1418. */
  1419. error = input_register_device(iqs626->keypad);
  1420. if (error)
  1421. dev_err(&client->dev, "Failed to register keypad: %d\n", error);
  1422. return error;
  1423. }
  1424. static int iqs626_suspend(struct device *dev)
  1425. {
  1426. struct iqs626_private *iqs626 = dev_get_drvdata(dev);
  1427. struct i2c_client *client = iqs626->client;
  1428. unsigned int val;
  1429. int error;
  1430. if (!iqs626->suspend_mode)
  1431. return 0;
  1432. disable_irq(client->irq);
  1433. /*
  1434. * Automatic power mode switching must be disabled before the device is
  1435. * forced into any particular power mode. In this case, the device will
  1436. * transition into normal-power mode.
  1437. */
  1438. error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
  1439. IQS626_SYS_SETTINGS_DIS_AUTO, ~0);
  1440. if (error)
  1441. goto err_irq;
  1442. /*
  1443. * The following check ensures the device has completed its transition
  1444. * into normal-power mode before a manual mode switch is performed.
  1445. */
  1446. error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
  1447. !(val & IQS626_SYS_FLAGS_PWR_MODE_MASK),
  1448. IQS626_PWR_MODE_POLL_SLEEP_US,
  1449. IQS626_PWR_MODE_POLL_TIMEOUT_US);
  1450. if (error)
  1451. goto err_irq;
  1452. error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
  1453. IQS626_SYS_SETTINGS_PWR_MODE_MASK,
  1454. iqs626->suspend_mode <<
  1455. IQS626_SYS_SETTINGS_PWR_MODE_SHIFT);
  1456. if (error)
  1457. goto err_irq;
  1458. /*
  1459. * This last check ensures the device has completed its transition into
  1460. * the desired power mode to prevent any spurious interrupts from being
  1461. * triggered after iqs626_suspend has already returned.
  1462. */
  1463. error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
  1464. (val & IQS626_SYS_FLAGS_PWR_MODE_MASK)
  1465. == (iqs626->suspend_mode <<
  1466. IQS626_SYS_FLAGS_PWR_MODE_SHIFT),
  1467. IQS626_PWR_MODE_POLL_SLEEP_US,
  1468. IQS626_PWR_MODE_POLL_TIMEOUT_US);
  1469. err_irq:
  1470. iqs626_irq_wait();
  1471. enable_irq(client->irq);
  1472. return error;
  1473. }
  1474. static int iqs626_resume(struct device *dev)
  1475. {
  1476. struct iqs626_private *iqs626 = dev_get_drvdata(dev);
  1477. struct i2c_client *client = iqs626->client;
  1478. unsigned int val;
  1479. int error;
  1480. if (!iqs626->suspend_mode)
  1481. return 0;
  1482. disable_irq(client->irq);
  1483. error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
  1484. IQS626_SYS_SETTINGS_PWR_MODE_MASK, 0);
  1485. if (error)
  1486. goto err_irq;
  1487. /*
  1488. * This check ensures the device has returned to normal-power mode
  1489. * before automatic power mode switching is re-enabled.
  1490. */
  1491. error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
  1492. !(val & IQS626_SYS_FLAGS_PWR_MODE_MASK),
  1493. IQS626_PWR_MODE_POLL_SLEEP_US,
  1494. IQS626_PWR_MODE_POLL_TIMEOUT_US);
  1495. if (error)
  1496. goto err_irq;
  1497. error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
  1498. IQS626_SYS_SETTINGS_DIS_AUTO, 0);
  1499. if (error)
  1500. goto err_irq;
  1501. /*
  1502. * This step reports any events that may have been "swallowed" as a
  1503. * result of polling PWR_MODE (which automatically acknowledges any
  1504. * pending interrupts).
  1505. */
  1506. error = iqs626_report(iqs626);
  1507. err_irq:
  1508. iqs626_irq_wait();
  1509. enable_irq(client->irq);
  1510. return error;
  1511. }
  1512. static DEFINE_SIMPLE_DEV_PM_OPS(iqs626_pm, iqs626_suspend, iqs626_resume);
  1513. static const struct of_device_id iqs626_of_match[] = {
  1514. { .compatible = "azoteq,iqs626a" },
  1515. { }
  1516. };
  1517. MODULE_DEVICE_TABLE(of, iqs626_of_match);
  1518. static struct i2c_driver iqs626_i2c_driver = {
  1519. .driver = {
  1520. .name = "iqs626a",
  1521. .of_match_table = iqs626_of_match,
  1522. .pm = pm_sleep_ptr(&iqs626_pm),
  1523. },
  1524. .probe = iqs626_probe,
  1525. };
  1526. module_i2c_driver(iqs626_i2c_driver);
  1527. MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
  1528. MODULE_DESCRIPTION("Azoteq IQS626A Capacitive Touch Controller");
  1529. MODULE_LICENSE("GPL");