adp5588-keys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. bool gpio_only;
  172. unsigned short keycode[ADP5588_KEYMAPSIZE];
  173. unsigned char gpiomap[ADP5588_MAXGPIO];
  174. struct gpio_chip gc;
  175. struct mutex gpio_lock; /* Protect cached dir, dat_out */
  176. u8 dat_out[3];
  177. u8 dir[3];
  178. u8 int_en[3];
  179. u8 irq_mask[3];
  180. u8 pull_dis[3];
  181. };
  182. static int adp5588_read(struct i2c_client *client, u8 reg)
  183. {
  184. int ret = i2c_smbus_read_byte_data(client, reg);
  185. if (ret < 0)
  186. dev_err(&client->dev, "Read Error\n");
  187. return ret;
  188. }
  189. static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
  190. {
  191. return i2c_smbus_write_byte_data(client, reg, val);
  192. }
  193. static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)
  194. {
  195. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  196. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  197. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  198. int val;
  199. guard(mutex)(&kpad->gpio_lock);
  200. if (kpad->dir[bank] & bit)
  201. val = kpad->dat_out[bank];
  202. else
  203. val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
  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. guard(mutex)(&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. }
  219. static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off,
  220. unsigned long config)
  221. {
  222. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  223. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  224. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  225. bool pull_disable;
  226. switch (pinconf_to_config_param(config)) {
  227. case PIN_CONFIG_BIAS_PULL_UP:
  228. pull_disable = false;
  229. break;
  230. case PIN_CONFIG_BIAS_DISABLE:
  231. pull_disable = true;
  232. break;
  233. default:
  234. return -ENOTSUPP;
  235. }
  236. guard(mutex)(&kpad->gpio_lock);
  237. if (pull_disable)
  238. kpad->pull_dis[bank] |= bit;
  239. else
  240. kpad->pull_dis[bank] &= bit;
  241. return adp5588_write(kpad->client, GPIO_PULL1 + bank,
  242. kpad->pull_dis[bank]);
  243. }
  244. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
  245. {
  246. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  247. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  248. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  249. guard(mutex)(&kpad->gpio_lock);
  250. kpad->dir[bank] &= ~bit;
  251. return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  252. }
  253. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  254. unsigned int off, int val)
  255. {
  256. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  257. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  258. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  259. int error;
  260. guard(mutex)(&kpad->gpio_lock);
  261. kpad->dir[bank] |= bit;
  262. if (val)
  263. kpad->dat_out[bank] |= bit;
  264. else
  265. kpad->dat_out[bank] &= ~bit;
  266. error = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
  267. kpad->dat_out[bank]);
  268. if (error)
  269. return error;
  270. error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  271. if (error)
  272. return error;
  273. return 0;
  274. }
  275. static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
  276. {
  277. bool pin_used[ADP5588_MAXGPIO];
  278. int n_unused = 0;
  279. int i;
  280. memset(pin_used, 0, sizeof(pin_used));
  281. for (i = 0; i < kpad->rows; i++)
  282. pin_used[i] = true;
  283. for (i = 0; i < kpad->cols; i++)
  284. pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
  285. for (i = 0; i < ADP5588_MAXGPIO; i++)
  286. if (!pin_used[i])
  287. kpad->gpiomap[n_unused++] = i;
  288. return n_unused;
  289. }
  290. static void adp5588_irq_bus_lock(struct irq_data *d)
  291. {
  292. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  293. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  294. mutex_lock(&kpad->gpio_lock);
  295. }
  296. static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
  297. {
  298. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  299. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  300. int i;
  301. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  302. if (kpad->int_en[i] ^ kpad->irq_mask[i]) {
  303. kpad->int_en[i] = kpad->irq_mask[i];
  304. adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]);
  305. }
  306. }
  307. mutex_unlock(&kpad->gpio_lock);
  308. }
  309. static void adp5588_irq_mask(struct irq_data *d)
  310. {
  311. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  312. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  313. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  314. unsigned long real_irq = kpad->gpiomap[hwirq];
  315. kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq);
  316. gpiochip_disable_irq(gc, hwirq);
  317. }
  318. static void adp5588_irq_unmask(struct irq_data *d)
  319. {
  320. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  321. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  322. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  323. unsigned long real_irq = kpad->gpiomap[hwirq];
  324. gpiochip_enable_irq(gc, hwirq);
  325. kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq);
  326. }
  327. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  328. {
  329. if (!(type & IRQ_TYPE_EDGE_BOTH))
  330. return -EINVAL;
  331. irq_set_handler_locked(d, handle_edge_irq);
  332. return 0;
  333. }
  334. static const struct irq_chip adp5588_irq_chip = {
  335. .name = "adp5588",
  336. .irq_mask = adp5588_irq_mask,
  337. .irq_unmask = adp5588_irq_unmask,
  338. .irq_bus_lock = adp5588_irq_bus_lock,
  339. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  340. .irq_set_type = adp5588_irq_set_type,
  341. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
  342. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  343. };
  344. static int adp5588_gpio_add(struct adp5588_kpad *kpad)
  345. {
  346. struct device *dev = &kpad->client->dev;
  347. struct gpio_irq_chip *girq;
  348. int i, error;
  349. kpad->gc.ngpio = adp5588_build_gpiomap(kpad);
  350. if (kpad->gc.ngpio == 0) {
  351. dev_info(dev, "No unused gpios left to export\n");
  352. return 0;
  353. }
  354. kpad->gc.parent = &kpad->client->dev;
  355. kpad->gc.direction_input = adp5588_gpio_direction_input;
  356. kpad->gc.direction_output = adp5588_gpio_direction_output;
  357. kpad->gc.get = adp5588_gpio_get_value;
  358. kpad->gc.set = adp5588_gpio_set_value;
  359. kpad->gc.set_config = adp5588_gpio_set_config;
  360. kpad->gc.can_sleep = 1;
  361. kpad->gc.base = -1;
  362. kpad->gc.label = kpad->client->name;
  363. kpad->gc.owner = THIS_MODULE;
  364. if (device_property_present(dev, "interrupt-controller")) {
  365. if (!kpad->client->irq) {
  366. dev_err(dev, "Unable to serve as interrupt controller without interrupt");
  367. return -EINVAL;
  368. }
  369. girq = &kpad->gc.irq;
  370. gpio_irq_chip_set_chip(girq, &adp5588_irq_chip);
  371. girq->handler = handle_bad_irq;
  372. girq->threaded = true;
  373. }
  374. mutex_init(&kpad->gpio_lock);
  375. error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
  376. if (error) {
  377. dev_err(dev, "gpiochip_add failed: %d\n", error);
  378. return error;
  379. }
  380. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  381. kpad->dat_out[i] = adp5588_read(kpad->client,
  382. GPIO_DAT_OUT1 + i);
  383. kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
  384. kpad->pull_dis[i] = adp5588_read(kpad->client, GPIO_PULL1 + i);
  385. }
  386. return 0;
  387. }
  388. static unsigned long adp5588_gpiomap_get_hwirq(struct device *dev,
  389. const u8 *map, unsigned int gpio,
  390. unsigned int ngpios)
  391. {
  392. unsigned int hwirq;
  393. for (hwirq = 0; hwirq < ngpios; hwirq++)
  394. if (map[hwirq] == gpio)
  395. return hwirq;
  396. /* should never happen */
  397. dev_warn_ratelimited(dev, "could not find the hwirq for gpio(%u)\n", gpio);
  398. return ADP5588_INVALID_HWIRQ;
  399. }
  400. static void adp5588_gpio_irq_handle(struct adp5588_kpad *kpad, int key_val,
  401. int key_press)
  402. {
  403. unsigned int irq, gpio = key_val - GPI_PIN_BASE, irq_type;
  404. struct i2c_client *client = kpad->client;
  405. struct irq_data *irqd;
  406. unsigned long hwirq;
  407. hwirq = adp5588_gpiomap_get_hwirq(&client->dev, kpad->gpiomap,
  408. gpio, kpad->gc.ngpio);
  409. if (hwirq == ADP5588_INVALID_HWIRQ) {
  410. dev_err(&client->dev, "Could not get hwirq for key(%u)\n", key_val);
  411. return;
  412. }
  413. irq = irq_find_mapping(kpad->gc.irq.domain, hwirq);
  414. if (!irq)
  415. return;
  416. irqd = irq_get_irq_data(irq);
  417. if (!irqd) {
  418. dev_err(&client->dev, "Could not get irq(%u) data\n", irq);
  419. return;
  420. }
  421. irq_type = irqd_get_trigger_type(irqd);
  422. /*
  423. * Default is active low which means key_press is asserted on
  424. * the falling edge.
  425. */
  426. if ((irq_type & IRQ_TYPE_EDGE_RISING && !key_press) ||
  427. (irq_type & IRQ_TYPE_EDGE_FALLING && key_press))
  428. handle_nested_irq(irq);
  429. }
  430. static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
  431. {
  432. int i;
  433. for (i = 0; i < ev_cnt; i++) {
  434. int key = adp5588_read(kpad->client, KEY_EVENTA + i);
  435. int key_val = key & KEY_EV_MASK;
  436. int key_press = key & KEY_EV_PRESSED;
  437. if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
  438. /* gpio line used as IRQ source */
  439. adp5588_gpio_irq_handle(kpad, key_val, key_press);
  440. } else {
  441. int row = (key_val - 1) / ADP5588_COLS_MAX;
  442. int col = (key_val - 1) % ADP5588_COLS_MAX;
  443. int code = MATRIX_SCAN_CODE(row, col, kpad->row_shift);
  444. dev_dbg_ratelimited(&kpad->client->dev,
  445. "report key(%d) r(%d) c(%d) code(%d)\n",
  446. key_val, row, col, kpad->keycode[code]);
  447. input_report_key(kpad->input,
  448. kpad->keycode[code], key_press);
  449. }
  450. }
  451. }
  452. static irqreturn_t adp5588_hard_irq(int irq, void *handle)
  453. {
  454. struct adp5588_kpad *kpad = handle;
  455. kpad->irq_time = ktime_get();
  456. return IRQ_WAKE_THREAD;
  457. }
  458. static irqreturn_t adp5588_thread_irq(int irq, void *handle)
  459. {
  460. struct adp5588_kpad *kpad = handle;
  461. struct i2c_client *client = kpad->client;
  462. ktime_t target_time, now;
  463. unsigned long delay;
  464. int status, ev_cnt;
  465. /*
  466. * Readout needs to wait for at least 25ms after the notification
  467. * for REVID < 4.
  468. */
  469. if (kpad->delay) {
  470. target_time = ktime_add_ms(kpad->irq_time, kpad->delay);
  471. now = ktime_get();
  472. if (ktime_before(now, target_time)) {
  473. delay = ktime_to_us(ktime_sub(target_time, now));
  474. usleep_range(delay, delay + 1000);
  475. }
  476. }
  477. status = adp5588_read(client, INT_STAT);
  478. if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */
  479. dev_err(&client->dev, "Event Overflow Error\n");
  480. if (status & ADP5588_KE_INT) {
  481. ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
  482. if (ev_cnt) {
  483. adp5588_report_events(kpad, ev_cnt);
  484. input_sync(kpad->input);
  485. }
  486. }
  487. adp5588_write(client, INT_STAT, status); /* Status is W1C */
  488. return IRQ_HANDLED;
  489. }
  490. static int adp5588_setup(struct adp5588_kpad *kpad)
  491. {
  492. struct i2c_client *client = kpad->client;
  493. int i, ret;
  494. ret = adp5588_write(client, KP_GPIO1, KP_SEL(kpad->rows));
  495. if (ret)
  496. return ret;
  497. ret = adp5588_write(client, KP_GPIO2, KP_SEL(kpad->cols) & 0xFF);
  498. if (ret)
  499. return ret;
  500. ret = adp5588_write(client, KP_GPIO3, KP_SEL(kpad->cols) >> 8);
  501. if (ret)
  502. return ret;
  503. for (i = 0; i < kpad->nkeys_unlock; i++) {
  504. ret = adp5588_write(client, UNLOCK1 + i, kpad->unlock_keys[i]);
  505. if (ret)
  506. return ret;
  507. }
  508. if (kpad->nkeys_unlock) {
  509. ret = adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
  510. if (ret)
  511. return ret;
  512. }
  513. for (i = 0; i < KEYP_MAX_EVENT; i++) {
  514. ret = adp5588_read(client, KEY_EVENTA);
  515. if (ret < 0)
  516. return ret;
  517. }
  518. ret = adp5588_write(client, INT_STAT,
  519. ADP5588_CMP2_INT | ADP5588_CMP1_INT |
  520. ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
  521. ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
  522. if (ret)
  523. return ret;
  524. return adp5588_write(client, CFG, ADP5588_INT_CFG |
  525. ADP5588_OVR_FLOW_IEN | ADP5588_KE_IEN);
  526. }
  527. static int adp5588_fw_parse(struct adp5588_kpad *kpad)
  528. {
  529. struct i2c_client *client = kpad->client;
  530. int ret, i;
  531. /*
  532. * Check if the device is to be operated purely in GPIO mode. To do
  533. * so, check that no keypad rows or columns have been specified,
  534. * since all GPINS should be configured as GPIO.
  535. */
  536. if (!device_property_present(&client->dev, "keypad,num-rows") &&
  537. !device_property_present(&client->dev, "keypad,num-columns")) {
  538. /* If purely GPIO, skip keypad setup */
  539. kpad->gpio_only = true;
  540. return 0;
  541. }
  542. ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows,
  543. &kpad->cols);
  544. if (ret)
  545. return ret;
  546. if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) {
  547. dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n",
  548. kpad->rows, kpad->cols);
  549. return -EINVAL;
  550. }
  551. ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols,
  552. kpad->keycode, kpad->input);
  553. if (ret)
  554. return ret;
  555. kpad->row_shift = get_count_order(kpad->cols);
  556. if (device_property_read_bool(&client->dev, "autorepeat"))
  557. __set_bit(EV_REP, kpad->input->evbit);
  558. kpad->nkeys_unlock = device_property_count_u32(&client->dev,
  559. "adi,unlock-keys");
  560. if (kpad->nkeys_unlock <= 0) {
  561. /* so that we don't end up enabling key lock */
  562. kpad->nkeys_unlock = 0;
  563. return 0;
  564. }
  565. if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) {
  566. dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n",
  567. kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys));
  568. return -EINVAL;
  569. }
  570. ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys",
  571. kpad->unlock_keys,
  572. kpad->nkeys_unlock);
  573. if (ret)
  574. return ret;
  575. for (i = 0; i < kpad->nkeys_unlock; i++) {
  576. /*
  577. * Even though it should be possible (as stated in the datasheet)
  578. * to use GPIs (which are part of the keys event) as unlock keys,
  579. * it was not working at all and was leading to overflow events
  580. * at some point. Hence, for now, let's just allow keys which are
  581. * part of keypad matrix to be used and if a reliable way of
  582. * using GPIs is found, this condition can be removed/lightened.
  583. */
  584. if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) {
  585. dev_err(&client->dev, "Invalid unlock key(%d)\n",
  586. kpad->unlock_keys[i]);
  587. return -EINVAL;
  588. }
  589. /*
  590. * Firmware properties keys start from 0 but on the device they
  591. * start from 1.
  592. */
  593. kpad->unlock_keys[i] += 1;
  594. }
  595. return 0;
  596. }
  597. static int adp5588_probe(struct i2c_client *client)
  598. {
  599. struct adp5588_kpad *kpad;
  600. struct input_dev *input;
  601. struct gpio_desc *gpio;
  602. unsigned int revid;
  603. int ret;
  604. int error;
  605. if (!i2c_check_functionality(client->adapter,
  606. I2C_FUNC_SMBUS_BYTE_DATA)) {
  607. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  608. return -EIO;
  609. }
  610. kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
  611. if (!kpad)
  612. return -ENOMEM;
  613. input = devm_input_allocate_device(&client->dev);
  614. if (!input)
  615. return -ENOMEM;
  616. kpad->client = client;
  617. kpad->input = input;
  618. error = adp5588_fw_parse(kpad);
  619. if (error)
  620. return error;
  621. error = devm_regulator_get_enable(&client->dev, "vcc");
  622. if (error)
  623. return error;
  624. gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
  625. if (IS_ERR(gpio))
  626. return PTR_ERR(gpio);
  627. if (gpio) {
  628. fsleep(30);
  629. gpiod_set_value_cansleep(gpio, 0);
  630. fsleep(60);
  631. }
  632. ret = adp5588_read(client, DEV_ID);
  633. if (ret < 0)
  634. return ret;
  635. revid = ret & ADP5588_DEVICE_ID_MASK;
  636. if (WA_DELAYED_READOUT_REVID(revid))
  637. kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
  638. input->name = client->name;
  639. input->phys = "adp5588-keys/input0";
  640. input_set_drvdata(input, kpad);
  641. input->id.bustype = BUS_I2C;
  642. input->id.vendor = 0x0001;
  643. input->id.product = 0x0001;
  644. input->id.version = revid;
  645. error = input_register_device(input);
  646. if (error) {
  647. dev_err(&client->dev, "unable to register input device: %d\n",
  648. error);
  649. return error;
  650. }
  651. error = adp5588_setup(kpad);
  652. if (error)
  653. return error;
  654. error = adp5588_gpio_add(kpad);
  655. if (error)
  656. return error;
  657. if (client->irq) {
  658. error = devm_request_threaded_irq(&client->dev, client->irq,
  659. adp5588_hard_irq, adp5588_thread_irq,
  660. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  661. client->dev.driver->name, kpad);
  662. if (error) {
  663. dev_err(&client->dev, "failed to request irq %d: %d\n",
  664. client->irq, error);
  665. return error;
  666. }
  667. }
  668. dev_info(&client->dev, "Rev.%d controller\n", revid);
  669. return 0;
  670. }
  671. static void adp5588_remove(struct i2c_client *client)
  672. {
  673. adp5588_write(client, CFG, 0);
  674. /* all resources will be freed by devm */
  675. }
  676. static int adp5588_suspend(struct device *dev)
  677. {
  678. struct i2c_client *client = to_i2c_client(dev);
  679. if (client->irq)
  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. if (client->irq)
  687. enable_irq(client->irq);
  688. return 0;
  689. }
  690. static DEFINE_SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
  691. static const struct i2c_device_id adp5588_id[] = {
  692. { "adp5588-keys" },
  693. { "adp5587-keys" },
  694. { }
  695. };
  696. MODULE_DEVICE_TABLE(i2c, adp5588_id);
  697. static const struct of_device_id adp5588_of_match[] = {
  698. { .compatible = "adi,adp5588" },
  699. { .compatible = "adi,adp5587" },
  700. {}
  701. };
  702. MODULE_DEVICE_TABLE(of, adp5588_of_match);
  703. static struct i2c_driver adp5588_driver = {
  704. .driver = {
  705. .name = KBUILD_MODNAME,
  706. .of_match_table = adp5588_of_match,
  707. .pm = pm_sleep_ptr(&adp5588_dev_pm_ops),
  708. },
  709. .probe = adp5588_probe,
  710. .remove = adp5588_remove,
  711. .id_table = adp5588_id,
  712. };
  713. module_i2c_driver(adp5588_driver);
  714. MODULE_LICENSE("GPL");
  715. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  716. MODULE_DESCRIPTION("ADP5588/87 Keypad driver");