adp5588-keys.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * File: drivers/input/keyboard/adp5588_keys.c
  4. * Description: keypad driver for ADP5588 and ADP5587
  5. * I2C QWERTY Keypad and IO Expander
  6. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Copyright (C) 2008-2010 Analog Devices Inc.
  9. */
  10. #include <linux/bits.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/i2c.h>
  16. #include <linux/input.h>
  17. #include <linux/input/matrix_keypad.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/ktime.h>
  21. #include <linux/module.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <linux/timekeeping.h>
  29. #define DEV_ID 0x00 /* Device ID */
  30. #define CFG 0x01 /* Configuration Register1 */
  31. #define INT_STAT 0x02 /* Interrupt Status Register */
  32. #define KEY_LCK_EC_STAT 0x03 /* Key Lock and Event Counter Register */
  33. #define KEY_EVENTA 0x04 /* Key Event Register A */
  34. #define KEY_EVENTB 0x05 /* Key Event Register B */
  35. #define KEY_EVENTC 0x06 /* Key Event Register C */
  36. #define KEY_EVENTD 0x07 /* Key Event Register D */
  37. #define KEY_EVENTE 0x08 /* Key Event Register E */
  38. #define KEY_EVENTF 0x09 /* Key Event Register F */
  39. #define KEY_EVENTG 0x0A /* Key Event Register G */
  40. #define KEY_EVENTH 0x0B /* Key Event Register H */
  41. #define KEY_EVENTI 0x0C /* Key Event Register I */
  42. #define KEY_EVENTJ 0x0D /* Key Event Register J */
  43. #define KP_LCK_TMR 0x0E /* Keypad Lock1 to Lock2 Timer */
  44. #define UNLOCK1 0x0F /* Unlock Key1 */
  45. #define UNLOCK2 0x10 /* Unlock Key2 */
  46. #define GPIO_INT_STAT1 0x11 /* GPIO Interrupt Status */
  47. #define GPIO_INT_STAT2 0x12 /* GPIO Interrupt Status */
  48. #define GPIO_INT_STAT3 0x13 /* GPIO Interrupt Status */
  49. #define GPIO_DAT_STAT1 0x14 /* GPIO Data Status, Read twice to clear */
  50. #define GPIO_DAT_STAT2 0x15 /* GPIO Data Status, Read twice to clear */
  51. #define GPIO_DAT_STAT3 0x16 /* GPIO Data Status, Read twice to clear */
  52. #define GPIO_DAT_OUT1 0x17 /* GPIO DATA OUT */
  53. #define GPIO_DAT_OUT2 0x18 /* GPIO DATA OUT */
  54. #define GPIO_DAT_OUT3 0x19 /* GPIO DATA OUT */
  55. #define GPIO_INT_EN1 0x1A /* GPIO Interrupt Enable */
  56. #define GPIO_INT_EN2 0x1B /* GPIO Interrupt Enable */
  57. #define GPIO_INT_EN3 0x1C /* GPIO Interrupt Enable */
  58. #define KP_GPIO1 0x1D /* Keypad or GPIO Selection */
  59. #define KP_GPIO2 0x1E /* Keypad or GPIO Selection */
  60. #define KP_GPIO3 0x1F /* Keypad or GPIO Selection */
  61. #define GPI_EM1 0x20 /* GPI Event Mode 1 */
  62. #define GPI_EM2 0x21 /* GPI Event Mode 2 */
  63. #define GPI_EM3 0x22 /* GPI Event Mode 3 */
  64. #define GPIO_DIR1 0x23 /* GPIO Data Direction */
  65. #define GPIO_DIR2 0x24 /* GPIO Data Direction */
  66. #define GPIO_DIR3 0x25 /* GPIO Data Direction */
  67. #define GPIO_INT_LVL1 0x26 /* GPIO Edge/Level Detect */
  68. #define GPIO_INT_LVL2 0x27 /* GPIO Edge/Level Detect */
  69. #define GPIO_INT_LVL3 0x28 /* GPIO Edge/Level Detect */
  70. #define DEBOUNCE_DIS1 0x29 /* Debounce Disable */
  71. #define DEBOUNCE_DIS2 0x2A /* Debounce Disable */
  72. #define DEBOUNCE_DIS3 0x2B /* Debounce Disable */
  73. #define GPIO_PULL1 0x2C /* GPIO Pull Disable */
  74. #define GPIO_PULL2 0x2D /* GPIO Pull Disable */
  75. #define GPIO_PULL3 0x2E /* GPIO Pull Disable */
  76. #define CMP_CFG_STAT 0x30 /* Comparator Configuration and Status Register */
  77. #define CMP_CONFG_SENS1 0x31 /* Sensor1 Comparator Configuration Register */
  78. #define CMP_CONFG_SENS2 0x32 /* L2 Light Sensor Reference Level, Output Falling for Sensor 1 */
  79. #define CMP1_LVL2_TRIP 0x33 /* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 1 */
  80. #define CMP1_LVL2_HYS 0x34 /* L3 Light Sensor Reference Level, Output Falling For Sensor 1 */
  81. #define CMP1_LVL3_TRIP 0x35 /* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 1 */
  82. #define CMP1_LVL3_HYS 0x36 /* Sensor 2 Comparator Configuration Register */
  83. #define CMP2_LVL2_TRIP 0x37 /* L2 Light Sensor Reference Level, Output Falling for Sensor 2 */
  84. #define CMP2_LVL2_HYS 0x38 /* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 2 */
  85. #define CMP2_LVL3_TRIP 0x39 /* L3 Light Sensor Reference Level, Output Falling For Sensor 2 */
  86. #define CMP2_LVL3_HYS 0x3A /* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 2 */
  87. #define CMP1_ADC_DAT_R1 0x3B /* Comparator 1 ADC data Register1 */
  88. #define CMP1_ADC_DAT_R2 0x3C /* Comparator 1 ADC data Register2 */
  89. #define CMP2_ADC_DAT_R1 0x3D /* Comparator 2 ADC data Register1 */
  90. #define CMP2_ADC_DAT_R2 0x3E /* Comparator 2 ADC data Register2 */
  91. #define ADP5588_DEVICE_ID_MASK 0xF
  92. /* Configuration Register1 */
  93. #define ADP5588_AUTO_INC BIT(7)
  94. #define ADP5588_GPIEM_CFG BIT(6)
  95. #define ADP5588_OVR_FLOW_M BIT(5)
  96. #define ADP5588_INT_CFG BIT(4)
  97. #define ADP5588_OVR_FLOW_IEN BIT(3)
  98. #define ADP5588_K_LCK_IM BIT(2)
  99. #define ADP5588_GPI_IEN BIT(1)
  100. #define ADP5588_KE_IEN BIT(0)
  101. /* Interrupt Status Register */
  102. #define ADP5588_CMP2_INT BIT(5)
  103. #define ADP5588_CMP1_INT BIT(4)
  104. #define ADP5588_OVR_FLOW_INT BIT(3)
  105. #define ADP5588_K_LCK_INT BIT(2)
  106. #define ADP5588_GPI_INT BIT(1)
  107. #define ADP5588_KE_INT BIT(0)
  108. /* Key Lock and Event Counter Register */
  109. #define ADP5588_K_LCK_EN BIT(6)
  110. #define ADP5588_LCK21 0x30
  111. #define ADP5588_KEC GENMASK(3, 0)
  112. #define ADP5588_MAXGPIO 18
  113. #define ADP5588_BANK(offs) ((offs) >> 3)
  114. #define ADP5588_BIT(offs) (1u << ((offs) & 0x7))
  115. /* Put one of these structures in i2c_board_info platform_data */
  116. /*
  117. * 128 so it fits matrix-keymap maximum number of keys when the full
  118. * 10cols * 8rows are used.
  119. */
  120. #define ADP5588_KEYMAPSIZE 128
  121. #define GPI_PIN_ROW0 97
  122. #define GPI_PIN_ROW1 98
  123. #define GPI_PIN_ROW2 99
  124. #define GPI_PIN_ROW3 100
  125. #define GPI_PIN_ROW4 101
  126. #define GPI_PIN_ROW5 102
  127. #define GPI_PIN_ROW6 103
  128. #define GPI_PIN_ROW7 104
  129. #define GPI_PIN_COL0 105
  130. #define GPI_PIN_COL1 106
  131. #define GPI_PIN_COL2 107
  132. #define GPI_PIN_COL3 108
  133. #define GPI_PIN_COL4 109
  134. #define GPI_PIN_COL5 110
  135. #define GPI_PIN_COL6 111
  136. #define GPI_PIN_COL7 112
  137. #define GPI_PIN_COL8 113
  138. #define GPI_PIN_COL9 114
  139. #define GPI_PIN_ROW_BASE GPI_PIN_ROW0
  140. #define GPI_PIN_ROW_END GPI_PIN_ROW7
  141. #define GPI_PIN_COL_BASE GPI_PIN_COL0
  142. #define GPI_PIN_COL_END GPI_PIN_COL9
  143. #define GPI_PIN_BASE GPI_PIN_ROW_BASE
  144. #define GPI_PIN_END GPI_PIN_COL_END
  145. #define ADP5588_ROWS_MAX (GPI_PIN_ROW7 - GPI_PIN_ROW0 + 1)
  146. #define ADP5588_COLS_MAX (GPI_PIN_COL9 - GPI_PIN_COL0 + 1)
  147. #define ADP5588_GPIMAPSIZE_MAX (GPI_PIN_END - GPI_PIN_BASE + 1)
  148. /* Key Event Register xy */
  149. #define KEY_EV_PRESSED BIT(7)
  150. #define KEY_EV_MASK GENMASK(6, 0)
  151. #define KP_SEL(x) (BIT(x) - 1) /* 2^x-1 */
  152. #define KEYP_MAX_EVENT 10
  153. /*
  154. * Early pre 4.0 Silicon required to delay readout by at least 25ms,
  155. * since the Event Counter Register updated 25ms after the interrupt
  156. * asserted.
  157. */
  158. #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
  159. #define WA_DELAYED_READOUT_TIME 25
  160. #define ADP5588_INVALID_HWIRQ (~0UL)
  161. struct adp5588_kpad {
  162. struct i2c_client *client;
  163. struct input_dev *input;
  164. ktime_t irq_time;
  165. unsigned long delay;
  166. u32 row_shift;
  167. u32 rows;
  168. u32 cols;
  169. u32 unlock_keys[2];
  170. int nkeys_unlock;
  171. unsigned short keycode[ADP5588_KEYMAPSIZE];
  172. unsigned char gpiomap[ADP5588_MAXGPIO];
  173. struct gpio_chip gc;
  174. struct mutex gpio_lock; /* Protect cached dir, dat_out */
  175. u8 dat_out[3];
  176. u8 dir[3];
  177. u8 int_en[3];
  178. u8 irq_mask[3];
  179. u8 pull_dis[3];
  180. };
  181. static int adp5588_read(struct i2c_client *client, u8 reg)
  182. {
  183. int ret = i2c_smbus_read_byte_data(client, reg);
  184. if (ret < 0)
  185. dev_err(&client->dev, "Read Error\n");
  186. return ret;
  187. }
  188. static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
  189. {
  190. return i2c_smbus_write_byte_data(client, reg, val);
  191. }
  192. static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)
  193. {
  194. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  195. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  196. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  197. int val;
  198. mutex_lock(&kpad->gpio_lock);
  199. if (kpad->dir[bank] & bit)
  200. val = kpad->dat_out[bank];
  201. else
  202. val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
  203. mutex_unlock(&kpad->gpio_lock);
  204. return !!(val & bit);
  205. }
  206. static void adp5588_gpio_set_value(struct gpio_chip *chip,
  207. unsigned int off, int val)
  208. {
  209. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  210. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  211. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  212. mutex_lock(&kpad->gpio_lock);
  213. if (val)
  214. kpad->dat_out[bank] |= bit;
  215. else
  216. kpad->dat_out[bank] &= ~bit;
  217. adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]);
  218. mutex_unlock(&kpad->gpio_lock);
  219. }
  220. static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,
  221. unsigned long config)
  222. {
  223. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  224. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  225. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  226. bool pull_disable;
  227. int ret;
  228. switch (pinconf_to_config_param(config)) {
  229. case PIN_CONFIG_BIAS_PULL_UP:
  230. pull_disable = false;
  231. break;
  232. case PIN_CONFIG_BIAS_DISABLE:
  233. pull_disable = true;
  234. break;
  235. default:
  236. return -ENOTSUPP;
  237. }
  238. mutex_lock(&kpad->gpio_lock);
  239. if (pull_disable)
  240. kpad->pull_dis[bank] |= bit;
  241. else
  242. kpad->pull_dis[bank] &= bit;
  243. ret = adp5588_write(kpad->client, GPIO_PULL1 + bank,
  244. kpad->pull_dis[bank]);
  245. mutex_unlock(&kpad->gpio_lock);
  246. return ret;
  247. }
  248. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
  249. {
  250. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  251. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  252. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  253. int ret;
  254. mutex_lock(&kpad->gpio_lock);
  255. kpad->dir[bank] &= ~bit;
  256. ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  257. mutex_unlock(&kpad->gpio_lock);
  258. return ret;
  259. }
  260. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  261. unsigned int off, int val)
  262. {
  263. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  264. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  265. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  266. int ret;
  267. mutex_lock(&kpad->gpio_lock);
  268. kpad->dir[bank] |= bit;
  269. if (val)
  270. kpad->dat_out[bank] |= bit;
  271. else
  272. kpad->dat_out[bank] &= ~bit;
  273. ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
  274. kpad->dat_out[bank]);
  275. if (ret)
  276. goto out_unlock;
  277. ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  278. out_unlock:
  279. mutex_unlock(&kpad->gpio_lock);
  280. return ret;
  281. }
  282. static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
  283. {
  284. bool pin_used[ADP5588_MAXGPIO];
  285. int n_unused = 0;
  286. int i;
  287. memset(pin_used, 0, sizeof(pin_used));
  288. for (i = 0; i < kpad->rows; i++)
  289. pin_used[i] = true;
  290. for (i = 0; i < kpad->cols; i++)
  291. pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
  292. for (i = 0; i < ADP5588_MAXGPIO; i++)
  293. if (!pin_used[i])
  294. kpad->gpiomap[n_unused++] = i;
  295. return n_unused;
  296. }
  297. static void adp5588_irq_bus_lock(struct irq_data *d)
  298. {
  299. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  300. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  301. mutex_lock(&kpad->gpio_lock);
  302. }
  303. static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
  304. {
  305. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  306. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  307. int i;
  308. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  309. if (kpad->int_en[i] ^ kpad->irq_mask[i]) {
  310. kpad->int_en[i] = kpad->irq_mask[i];
  311. adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]);
  312. }
  313. }
  314. mutex_unlock(&kpad->gpio_lock);
  315. }
  316. static void adp5588_irq_mask(struct irq_data *d)
  317. {
  318. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  319. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  320. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  321. unsigned long real_irq = kpad->gpiomap[hwirq];
  322. kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq);
  323. gpiochip_disable_irq(gc, hwirq);
  324. }
  325. static void adp5588_irq_unmask(struct irq_data *d)
  326. {
  327. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  328. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  329. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  330. unsigned long real_irq = kpad->gpiomap[hwirq];
  331. gpiochip_enable_irq(gc, hwirq);
  332. kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq);
  333. }
  334. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  335. {
  336. if (!(type & IRQ_TYPE_EDGE_BOTH))
  337. return -EINVAL;
  338. irq_set_handler_locked(d, handle_edge_irq);
  339. return 0;
  340. }
  341. static const struct irq_chip adp5588_irq_chip = {
  342. .name = "adp5588",
  343. .irq_mask = adp5588_irq_mask,
  344. .irq_unmask = adp5588_irq_unmask,
  345. .irq_bus_lock = adp5588_irq_bus_lock,
  346. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  347. .irq_set_type = adp5588_irq_set_type,
  348. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
  349. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  350. };
  351. static int adp5588_gpio_add(struct adp5588_kpad *kpad)
  352. {
  353. struct device *dev = &kpad->client->dev;
  354. struct gpio_irq_chip *girq;
  355. int i, error;
  356. kpad->gc.ngpio = adp5588_build_gpiomap(kpad);
  357. if (kpad->gc.ngpio == 0) {
  358. dev_info(dev, "No unused gpios left to export\n");
  359. return 0;
  360. }
  361. kpad->gc.parent = &kpad->client->dev;
  362. kpad->gc.direction_input = adp5588_gpio_direction_input;
  363. kpad->gc.direction_output = adp5588_gpio_direction_output;
  364. kpad->gc.get = adp5588_gpio_get_value;
  365. kpad->gc.set = adp5588_gpio_set_value;
  366. kpad->gc.set_config = adp5588_gpio_set_config;
  367. kpad->gc.can_sleep = 1;
  368. kpad->gc.base = -1;
  369. kpad->gc.label = kpad->client->name;
  370. kpad->gc.owner = THIS_MODULE;
  371. girq = &kpad->gc.irq;
  372. gpio_irq_chip_set_chip(girq, &adp5588_irq_chip);
  373. girq->handler = handle_bad_irq;
  374. girq->threaded = true;
  375. mutex_init(&kpad->gpio_lock);
  376. error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
  377. if (error) {
  378. dev_err(dev, "gpiochip_add failed: %d\n", error);
  379. return error;
  380. }
  381. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  382. kpad->dat_out[i] = adp5588_read(kpad->client,
  383. GPIO_DAT_OUT1 + i);
  384. kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
  385. kpad->pull_dis[i] = adp5588_read(kpad->client, GPIO_PULL1 + i);
  386. }
  387. return 0;
  388. }
  389. static unsigned long adp5588_gpiomap_get_hwirq(struct device *dev,
  390. const u8 *map, unsigned int gpio,
  391. unsigned int ngpios)
  392. {
  393. unsigned int hwirq;
  394. for (hwirq = 0; hwirq < ngpios; hwirq++)
  395. if (map[hwirq] == gpio)
  396. return hwirq;
  397. /* should never happen */
  398. dev_warn_ratelimited(dev, "could not find the hwirq for gpio(%u)\n", gpio);
  399. return ADP5588_INVALID_HWIRQ;
  400. }
  401. static void adp5588_gpio_irq_handle(struct adp5588_kpad *kpad, int key_val,
  402. int key_press)
  403. {
  404. unsigned int irq, gpio = key_val - GPI_PIN_BASE, irq_type;
  405. struct i2c_client *client = kpad->client;
  406. struct irq_data *irqd;
  407. unsigned long hwirq;
  408. hwirq = adp5588_gpiomap_get_hwirq(&client->dev, kpad->gpiomap,
  409. gpio, kpad->gc.ngpio);
  410. if (hwirq == ADP5588_INVALID_HWIRQ) {
  411. dev_err(&client->dev, "Could not get hwirq for key(%u)\n", key_val);
  412. return;
  413. }
  414. irq = irq_find_mapping(kpad->gc.irq.domain, hwirq);
  415. if (!irq)
  416. return;
  417. irqd = irq_get_irq_data(irq);
  418. if (!irqd) {
  419. dev_err(&client->dev, "Could not get irq(%u) data\n", irq);
  420. return;
  421. }
  422. irq_type = irqd_get_trigger_type(irqd);
  423. /*
  424. * Default is active low which means key_press is asserted on
  425. * the falling edge.
  426. */
  427. if ((irq_type & IRQ_TYPE_EDGE_RISING && !key_press) ||
  428. (irq_type & IRQ_TYPE_EDGE_FALLING && key_press))
  429. handle_nested_irq(irq);
  430. }
  431. static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
  432. {
  433. int i;
  434. for (i = 0; i < ev_cnt; i++) {
  435. int key = adp5588_read(kpad->client, KEY_EVENTA + i);
  436. int key_val = key & KEY_EV_MASK;
  437. int key_press = key & KEY_EV_PRESSED;
  438. if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
  439. /* gpio line used as IRQ source */
  440. adp5588_gpio_irq_handle(kpad, key_val, key_press);
  441. } else {
  442. int row = (key_val - 1) / ADP5588_COLS_MAX;
  443. int col = (key_val - 1) % ADP5588_COLS_MAX;
  444. int code = MATRIX_SCAN_CODE(row, col, kpad->row_shift);
  445. dev_dbg_ratelimited(&kpad->client->dev,
  446. "report key(%d) r(%d) c(%d) code(%d)\n",
  447. key_val, row, col, kpad->keycode[code]);
  448. input_report_key(kpad->input,
  449. kpad->keycode[code], key_press);
  450. }
  451. }
  452. }
  453. static irqreturn_t adp5588_hard_irq(int irq, void *handle)
  454. {
  455. struct adp5588_kpad *kpad = handle;
  456. kpad->irq_time = ktime_get();
  457. return IRQ_WAKE_THREAD;
  458. }
  459. static irqreturn_t adp5588_thread_irq(int irq, void *handle)
  460. {
  461. struct adp5588_kpad *kpad = handle;
  462. struct i2c_client *client = kpad->client;
  463. ktime_t target_time, now;
  464. unsigned long delay;
  465. int status, ev_cnt;
  466. /*
  467. * Readout needs to wait for at least 25ms after the notification
  468. * for REVID < 4.
  469. */
  470. if (kpad->delay) {
  471. target_time = ktime_add_ms(kpad->irq_time, kpad->delay);
  472. now = ktime_get();
  473. if (ktime_before(now, target_time)) {
  474. delay = ktime_to_us(ktime_sub(target_time, now));
  475. usleep_range(delay, delay + 1000);
  476. }
  477. }
  478. status = adp5588_read(client, INT_STAT);
  479. if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */
  480. dev_err(&client->dev, "Event Overflow Error\n");
  481. if (status & ADP5588_KE_INT) {
  482. ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
  483. if (ev_cnt) {
  484. adp5588_report_events(kpad, ev_cnt);
  485. input_sync(kpad->input);
  486. }
  487. }
  488. adp5588_write(client, INT_STAT, status); /* Status is W1C */
  489. return IRQ_HANDLED;
  490. }
  491. static int adp5588_setup(struct adp5588_kpad *kpad)
  492. {
  493. struct i2c_client *client = kpad->client;
  494. int i, ret;
  495. ret = adp5588_write(client, KP_GPIO1, KP_SEL(kpad->rows));
  496. if (ret)
  497. return ret;
  498. ret = adp5588_write(client, KP_GPIO2, KP_SEL(kpad->cols) & 0xFF);
  499. if (ret)
  500. return ret;
  501. ret = adp5588_write(client, KP_GPIO3, KP_SEL(kpad->cols) >> 8);
  502. if (ret)
  503. return ret;
  504. for (i = 0; i < kpad->nkeys_unlock; i++) {
  505. ret = adp5588_write(client, UNLOCK1 + i, kpad->unlock_keys[i]);
  506. if (ret)
  507. return ret;
  508. }
  509. if (kpad->nkeys_unlock) {
  510. ret = adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
  511. if (ret)
  512. return ret;
  513. }
  514. for (i = 0; i < KEYP_MAX_EVENT; i++) {
  515. ret = adp5588_read(client, KEY_EVENTA);
  516. if (ret)
  517. return ret;
  518. }
  519. ret = adp5588_write(client, INT_STAT,
  520. ADP5588_CMP2_INT | ADP5588_CMP1_INT |
  521. ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
  522. ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
  523. if (ret)
  524. return ret;
  525. return adp5588_write(client, CFG, ADP5588_INT_CFG |
  526. ADP5588_OVR_FLOW_IEN | ADP5588_KE_IEN);
  527. }
  528. static int adp5588_fw_parse(struct adp5588_kpad *kpad)
  529. {
  530. struct i2c_client *client = kpad->client;
  531. int ret, i;
  532. ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows,
  533. &kpad->cols);
  534. if (ret)
  535. return ret;
  536. if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) {
  537. dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n",
  538. kpad->rows, kpad->cols);
  539. return -EINVAL;
  540. }
  541. ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols,
  542. kpad->keycode, kpad->input);
  543. if (ret)
  544. return ret;
  545. kpad->row_shift = get_count_order(kpad->cols);
  546. if (device_property_read_bool(&client->dev, "autorepeat"))
  547. __set_bit(EV_REP, kpad->input->evbit);
  548. kpad->nkeys_unlock = device_property_count_u32(&client->dev,
  549. "adi,unlock-keys");
  550. if (kpad->nkeys_unlock <= 0) {
  551. /* so that we don't end up enabling key lock */
  552. kpad->nkeys_unlock = 0;
  553. return 0;
  554. }
  555. if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) {
  556. dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n",
  557. kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys));
  558. return -EINVAL;
  559. }
  560. ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys",
  561. kpad->unlock_keys,
  562. kpad->nkeys_unlock);
  563. if (ret)
  564. return ret;
  565. for (i = 0; i < kpad->nkeys_unlock; i++) {
  566. /*
  567. * Even though it should be possible (as stated in the datasheet)
  568. * to use GPIs (which are part of the keys event) as unlock keys,
  569. * it was not working at all and was leading to overflow events
  570. * at some point. Hence, for now, let's just allow keys which are
  571. * part of keypad matrix to be used and if a reliable way of
  572. * using GPIs is found, this condition can be removed/lightened.
  573. */
  574. if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) {
  575. dev_err(&client->dev, "Invalid unlock key(%d)\n",
  576. kpad->unlock_keys[i]);
  577. return -EINVAL;
  578. }
  579. /*
  580. * Firmware properties keys start from 0 but on the device they
  581. * start from 1.
  582. */
  583. kpad->unlock_keys[i] += 1;
  584. }
  585. return 0;
  586. }
  587. static void adp5588_disable_regulator(void *reg)
  588. {
  589. regulator_disable(reg);
  590. }
  591. static int adp5588_probe(struct i2c_client *client,
  592. const struct i2c_device_id *id)
  593. {
  594. struct adp5588_kpad *kpad;
  595. struct input_dev *input;
  596. struct gpio_desc *gpio;
  597. struct regulator *vcc;
  598. unsigned int revid;
  599. int ret;
  600. int error;
  601. if (!i2c_check_functionality(client->adapter,
  602. I2C_FUNC_SMBUS_BYTE_DATA)) {
  603. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  604. return -EIO;
  605. }
  606. kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
  607. if (!kpad)
  608. return -ENOMEM;
  609. input = devm_input_allocate_device(&client->dev);
  610. if (!input)
  611. return -ENOMEM;
  612. kpad->client = client;
  613. kpad->input = input;
  614. error = adp5588_fw_parse(kpad);
  615. if (error)
  616. return error;
  617. vcc = devm_regulator_get(&client->dev, "vcc");
  618. if (IS_ERR(vcc))
  619. return PTR_ERR(vcc);
  620. error = regulator_enable(vcc);
  621. if (error)
  622. return error;
  623. error = devm_add_action_or_reset(&client->dev,
  624. adp5588_disable_regulator, vcc);
  625. if (error)
  626. return error;
  627. gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
  628. if (IS_ERR(gpio))
  629. return PTR_ERR(gpio);
  630. if (gpio) {
  631. fsleep(30);
  632. gpiod_set_value_cansleep(gpio, 0);
  633. fsleep(60);
  634. }
  635. ret = adp5588_read(client, DEV_ID);
  636. if (ret < 0)
  637. return ret;
  638. revid = ret & ADP5588_DEVICE_ID_MASK;
  639. if (WA_DELAYED_READOUT_REVID(revid))
  640. kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
  641. input->name = client->name;
  642. input->phys = "adp5588-keys/input0";
  643. input_set_drvdata(input, kpad);
  644. input->id.bustype = BUS_I2C;
  645. input->id.vendor = 0x0001;
  646. input->id.product = 0x0001;
  647. input->id.version = revid;
  648. error = input_register_device(input);
  649. if (error) {
  650. dev_err(&client->dev, "unable to register input device: %d\n",
  651. error);
  652. return error;
  653. }
  654. error = adp5588_setup(kpad);
  655. if (error)
  656. return error;
  657. error = adp5588_gpio_add(kpad);
  658. if (error)
  659. return error;
  660. error = devm_request_threaded_irq(&client->dev, client->irq,
  661. adp5588_hard_irq, adp5588_thread_irq,
  662. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  663. client->dev.driver->name, kpad);
  664. if (error) {
  665. dev_err(&client->dev, "failed to request irq %d: %d\n",
  666. client->irq, error);
  667. return error;
  668. }
  669. dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
  670. return 0;
  671. }
  672. static void adp5588_remove(struct i2c_client *client)
  673. {
  674. adp5588_write(client, CFG, 0);
  675. /* all resources will be freed by devm */
  676. }
  677. static int adp5588_suspend(struct device *dev)
  678. {
  679. struct i2c_client *client = to_i2c_client(dev);
  680. disable_irq(client->irq);
  681. return 0;
  682. }
  683. static int adp5588_resume(struct device *dev)
  684. {
  685. struct i2c_client *client = to_i2c_client(dev);
  686. enable_irq(client->irq);
  687. return 0;
  688. }
  689. static DEFINE_SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
  690. static const struct i2c_device_id adp5588_id[] = {
  691. { "adp5588-keys", 0 },
  692. { "adp5587-keys", 0 },
  693. { }
  694. };
  695. MODULE_DEVICE_TABLE(i2c, adp5588_id);
  696. static const struct of_device_id adp5588_of_match[] = {
  697. { .compatible = "adi,adp5588" },
  698. { .compatible = "adi,adp5587" },
  699. {}
  700. };
  701. MODULE_DEVICE_TABLE(of, adp5588_of_match);
  702. static struct i2c_driver adp5588_driver = {
  703. .driver = {
  704. .name = KBUILD_MODNAME,
  705. .of_match_table = adp5588_of_match,
  706. .pm = pm_sleep_ptr(&adp5588_dev_pm_ops),
  707. },
  708. .probe = adp5588_probe,
  709. .remove = adp5588_remove,
  710. .id_table = adp5588_id,
  711. };
  712. module_i2c_driver(adp5588_driver);
  713. MODULE_LICENSE("GPL");
  714. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  715. MODULE_DESCRIPTION("ADP5588/87 Keypad driver");