adp5588-keys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 int adp5588_gpio_set_value(struct gpio_chip *chip, unsigned int off,
  207. 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. return adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
  218. kpad->dat_out[bank]);
  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. switch (pinconf_to_config_param(config)) {
  228. case PIN_CONFIG_BIAS_PULL_UP:
  229. pull_disable = false;
  230. break;
  231. case PIN_CONFIG_BIAS_DISABLE:
  232. pull_disable = true;
  233. break;
  234. default:
  235. return -ENOTSUPP;
  236. }
  237. guard(mutex)(&kpad->gpio_lock);
  238. if (pull_disable)
  239. kpad->pull_dis[bank] |= bit;
  240. else
  241. kpad->pull_dis[bank] &= bit;
  242. return adp5588_write(kpad->client, GPIO_PULL1 + bank,
  243. kpad->pull_dis[bank]);
  244. }
  245. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
  246. {
  247. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  248. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  249. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  250. guard(mutex)(&kpad->gpio_lock);
  251. kpad->dir[bank] &= ~bit;
  252. return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  253. }
  254. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  255. unsigned int off, int val)
  256. {
  257. struct adp5588_kpad *kpad = gpiochip_get_data(chip);
  258. unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
  259. unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
  260. int error;
  261. guard(mutex)(&kpad->gpio_lock);
  262. kpad->dir[bank] |= bit;
  263. if (val)
  264. kpad->dat_out[bank] |= bit;
  265. else
  266. kpad->dat_out[bank] &= ~bit;
  267. error = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
  268. kpad->dat_out[bank]);
  269. if (error)
  270. return error;
  271. error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
  272. if (error)
  273. return error;
  274. return 0;
  275. }
  276. static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
  277. {
  278. bool pin_used[ADP5588_MAXGPIO];
  279. int n_unused = 0;
  280. int i;
  281. memset(pin_used, 0, sizeof(pin_used));
  282. for (i = 0; i < kpad->rows; i++)
  283. pin_used[i] = true;
  284. for (i = 0; i < kpad->cols; i++)
  285. pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
  286. for (i = 0; i < ADP5588_MAXGPIO; i++)
  287. if (!pin_used[i])
  288. kpad->gpiomap[n_unused++] = i;
  289. return n_unused;
  290. }
  291. static void adp5588_irq_bus_lock(struct irq_data *d)
  292. {
  293. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  294. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  295. mutex_lock(&kpad->gpio_lock);
  296. }
  297. static void adp5588_irq_bus_sync_unlock(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. int i;
  302. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  303. if (kpad->int_en[i] ^ kpad->irq_mask[i]) {
  304. kpad->int_en[i] = kpad->irq_mask[i];
  305. adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]);
  306. }
  307. }
  308. mutex_unlock(&kpad->gpio_lock);
  309. }
  310. static void adp5588_irq_mask(struct irq_data *d)
  311. {
  312. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  313. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  314. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  315. unsigned long real_irq = kpad->gpiomap[hwirq];
  316. kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq);
  317. gpiochip_disable_irq(gc, hwirq);
  318. }
  319. static void adp5588_irq_unmask(struct irq_data *d)
  320. {
  321. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  322. struct adp5588_kpad *kpad = gpiochip_get_data(gc);
  323. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  324. unsigned long real_irq = kpad->gpiomap[hwirq];
  325. gpiochip_enable_irq(gc, hwirq);
  326. kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq);
  327. }
  328. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  329. {
  330. if (!(type & IRQ_TYPE_EDGE_BOTH))
  331. return -EINVAL;
  332. irq_set_handler_locked(d, handle_edge_irq);
  333. return 0;
  334. }
  335. static const struct irq_chip adp5588_irq_chip = {
  336. .name = "adp5588",
  337. .irq_mask = adp5588_irq_mask,
  338. .irq_unmask = adp5588_irq_unmask,
  339. .irq_bus_lock = adp5588_irq_bus_lock,
  340. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  341. .irq_set_type = adp5588_irq_set_type,
  342. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
  343. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  344. };
  345. static int adp5588_gpio_add(struct adp5588_kpad *kpad)
  346. {
  347. struct device *dev = &kpad->client->dev;
  348. struct gpio_irq_chip *girq;
  349. int i, error;
  350. kpad->gc.ngpio = adp5588_build_gpiomap(kpad);
  351. if (kpad->gc.ngpio == 0) {
  352. dev_info(dev, "No unused gpios left to export\n");
  353. return 0;
  354. }
  355. kpad->gc.parent = &kpad->client->dev;
  356. kpad->gc.direction_input = adp5588_gpio_direction_input;
  357. kpad->gc.direction_output = adp5588_gpio_direction_output;
  358. kpad->gc.get = adp5588_gpio_get_value;
  359. kpad->gc.set = adp5588_gpio_set_value;
  360. kpad->gc.set_config = adp5588_gpio_set_config;
  361. kpad->gc.can_sleep = 1;
  362. kpad->gc.base = -1;
  363. kpad->gc.label = kpad->client->name;
  364. kpad->gc.owner = THIS_MODULE;
  365. if (device_property_present(dev, "interrupt-controller")) {
  366. if (!kpad->client->irq) {
  367. dev_err(dev, "Unable to serve as interrupt controller without interrupt");
  368. return -EINVAL;
  369. }
  370. girq = &kpad->gc.irq;
  371. gpio_irq_chip_set_chip(girq, &adp5588_irq_chip);
  372. girq->handler = handle_bad_irq;
  373. girq->threaded = true;
  374. }
  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 < 0)
  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. /*
  533. * Check if the device is to be operated purely in GPIO mode. To do
  534. * so, check that no keypad rows or columns have been specified,
  535. * since all GPINS should be configured as GPIO.
  536. */
  537. if (!device_property_present(&client->dev, "keypad,num-rows") &&
  538. !device_property_present(&client->dev, "keypad,num-columns")) {
  539. /* If purely GPIO, skip keypad setup */
  540. kpad->gpio_only = true;
  541. return 0;
  542. }
  543. ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows,
  544. &kpad->cols);
  545. if (ret)
  546. return ret;
  547. if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) {
  548. dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n",
  549. kpad->rows, kpad->cols);
  550. return -EINVAL;
  551. }
  552. ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols,
  553. kpad->keycode, kpad->input);
  554. if (ret)
  555. return ret;
  556. kpad->row_shift = get_count_order(kpad->cols);
  557. if (device_property_read_bool(&client->dev, "autorepeat"))
  558. __set_bit(EV_REP, kpad->input->evbit);
  559. kpad->nkeys_unlock = device_property_count_u32(&client->dev,
  560. "adi,unlock-keys");
  561. if (kpad->nkeys_unlock <= 0) {
  562. /* so that we don't end up enabling key lock */
  563. kpad->nkeys_unlock = 0;
  564. return 0;
  565. }
  566. if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) {
  567. dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n",
  568. kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys));
  569. return -EINVAL;
  570. }
  571. ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys",
  572. kpad->unlock_keys,
  573. kpad->nkeys_unlock);
  574. if (ret)
  575. return ret;
  576. for (i = 0; i < kpad->nkeys_unlock; i++) {
  577. /*
  578. * Even though it should be possible (as stated in the datasheet)
  579. * to use GPIs (which are part of the keys event) as unlock keys,
  580. * it was not working at all and was leading to overflow events
  581. * at some point. Hence, for now, let's just allow keys which are
  582. * part of keypad matrix to be used and if a reliable way of
  583. * using GPIs is found, this condition can be removed/lightened.
  584. */
  585. if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) {
  586. dev_err(&client->dev, "Invalid unlock key(%d)\n",
  587. kpad->unlock_keys[i]);
  588. return -EINVAL;
  589. }
  590. /*
  591. * Firmware properties keys start from 0 but on the device they
  592. * start from 1.
  593. */
  594. kpad->unlock_keys[i] += 1;
  595. }
  596. return 0;
  597. }
  598. static int adp5588_probe(struct i2c_client *client)
  599. {
  600. struct adp5588_kpad *kpad;
  601. struct input_dev *input;
  602. struct gpio_desc *gpio;
  603. unsigned int revid;
  604. int ret;
  605. int error;
  606. if (!i2c_check_functionality(client->adapter,
  607. I2C_FUNC_SMBUS_BYTE_DATA)) {
  608. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  609. return -EIO;
  610. }
  611. kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
  612. if (!kpad)
  613. return -ENOMEM;
  614. input = devm_input_allocate_device(&client->dev);
  615. if (!input)
  616. return -ENOMEM;
  617. kpad->client = client;
  618. kpad->input = input;
  619. error = adp5588_fw_parse(kpad);
  620. if (error)
  621. return error;
  622. error = devm_regulator_get_enable(&client->dev, "vcc");
  623. if (error)
  624. return error;
  625. gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
  626. if (IS_ERR(gpio))
  627. return PTR_ERR(gpio);
  628. if (gpio) {
  629. fsleep(30);
  630. gpiod_set_value_cansleep(gpio, 0);
  631. fsleep(60);
  632. }
  633. ret = adp5588_read(client, DEV_ID);
  634. if (ret < 0)
  635. return ret;
  636. revid = ret & ADP5588_DEVICE_ID_MASK;
  637. if (WA_DELAYED_READOUT_REVID(revid))
  638. kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
  639. input->name = client->name;
  640. input->phys = "adp5588-keys/input0";
  641. input_set_drvdata(input, kpad);
  642. input->id.bustype = BUS_I2C;
  643. input->id.vendor = 0x0001;
  644. input->id.product = 0x0001;
  645. input->id.version = revid;
  646. error = input_register_device(input);
  647. if (error) {
  648. dev_err(&client->dev, "unable to register input device: %d\n",
  649. error);
  650. return error;
  651. }
  652. error = adp5588_setup(kpad);
  653. if (error)
  654. return error;
  655. error = adp5588_gpio_add(kpad);
  656. if (error)
  657. return error;
  658. if (client->irq) {
  659. error = devm_request_threaded_irq(&client->dev, client->irq,
  660. adp5588_hard_irq, adp5588_thread_irq,
  661. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  662. client->dev.driver->name, kpad);
  663. if (error) {
  664. dev_err(&client->dev, "failed to request irq %d: %d\n",
  665. client->irq, error);
  666. return error;
  667. }
  668. }
  669. dev_info(&client->dev, "Rev.%d controller\n", revid);
  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. if (client->irq)
  681. disable_irq(client->irq);
  682. return 0;
  683. }
  684. static int adp5588_resume(struct device *dev)
  685. {
  686. struct i2c_client *client = to_i2c_client(dev);
  687. if (client->irq)
  688. enable_irq(client->irq);
  689. return 0;
  690. }
  691. static DEFINE_SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
  692. static const struct i2c_device_id adp5588_id[] = {
  693. { "adp5588-keys" },
  694. { "adp5587-keys" },
  695. { }
  696. };
  697. MODULE_DEVICE_TABLE(i2c, adp5588_id);
  698. static const struct of_device_id adp5588_of_match[] = {
  699. { .compatible = "adi,adp5588" },
  700. { .compatible = "adi,adp5587" },
  701. {}
  702. };
  703. MODULE_DEVICE_TABLE(of, adp5588_of_match);
  704. static struct i2c_driver adp5588_driver = {
  705. .driver = {
  706. .name = KBUILD_MODNAME,
  707. .of_match_table = adp5588_of_match,
  708. .pm = pm_sleep_ptr(&adp5588_dev_pm_ops),
  709. },
  710. .probe = adp5588_probe,
  711. .remove = adp5588_remove,
  712. .id_table = adp5588_id,
  713. };
  714. module_i2c_driver(adp5588_driver);
  715. MODULE_LICENSE("GPL");
  716. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  717. MODULE_DESCRIPTION("ADP5588/87 Keypad driver");