adp5589-keys.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Description: keypad driver for ADP5589
  3. * I2C QWERTY Keypad and IO Expander
  4. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (C) 2010-2011 Analog Devices Inc.
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/errno.h>
  15. #include <linux/pm.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/input.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio.h>
  20. #include <linux/slab.h>
  21. #include <linux/input/adp5589.h>
  22. /* GENERAL_CFG Register */
  23. #define OSC_EN (1 << 7)
  24. #define CORE_CLK(x) (((x) & 0x3) << 5)
  25. #define LCK_TRK_LOGIC (1 << 4)
  26. #define LCK_TRK_GPI (1 << 3)
  27. #define INT_CFG (1 << 1)
  28. #define RST_CFG (1 << 0)
  29. /* INT_EN Register */
  30. #define LOGIC2_IEN (1 << 5)
  31. #define LOGIC1_IEN (1 << 4)
  32. #define LOCK_IEN (1 << 3)
  33. #define OVRFLOW_IEN (1 << 2)
  34. #define GPI_IEN (1 << 1)
  35. #define EVENT_IEN (1 << 0)
  36. /* Interrupt Status Register */
  37. #define LOGIC2_INT (1 << 5)
  38. #define LOGIC1_INT (1 << 4)
  39. #define LOCK_INT (1 << 3)
  40. #define OVRFLOW_INT (1 << 2)
  41. #define GPI_INT (1 << 1)
  42. #define EVENT_INT (1 << 0)
  43. /* STATUS Register */
  44. #define LOGIC2_STAT (1 << 7)
  45. #define LOGIC1_STAT (1 << 6)
  46. #define LOCK_STAT (1 << 5)
  47. #define KEC 0xF
  48. /* PIN_CONFIG_D Register */
  49. #define C4_EXTEND_CFG (1 << 6) /* RESET2 */
  50. #define R4_EXTEND_CFG (1 << 5) /* RESET1 */
  51. /* LOCK_CFG */
  52. #define LOCK_EN (1 << 0)
  53. #define PTIME_MASK 0x3
  54. #define LTIME_MASK 0x3
  55. /* Key Event Register xy */
  56. #define KEY_EV_PRESSED (1 << 7)
  57. #define KEY_EV_MASK (0x7F)
  58. #define KEYP_MAX_EVENT 16
  59. #define MAXGPIO 19
  60. #define ADP_BANK(offs) ((offs) >> 3)
  61. #define ADP_BIT(offs) (1u << ((offs) & 0x7))
  62. struct adp5589_kpad {
  63. struct i2c_client *client;
  64. struct input_dev *input;
  65. unsigned short keycode[ADP5589_KEYMAPSIZE];
  66. const struct adp5589_gpi_map *gpimap;
  67. unsigned short gpimapsize;
  68. unsigned extend_cfg;
  69. #ifdef CONFIG_GPIOLIB
  70. unsigned char gpiomap[MAXGPIO];
  71. bool export_gpio;
  72. struct gpio_chip gc;
  73. struct mutex gpio_lock; /* Protect cached dir, dat_out */
  74. u8 dat_out[3];
  75. u8 dir[3];
  76. #endif
  77. };
  78. static int adp5589_read(struct i2c_client *client, u8 reg)
  79. {
  80. int ret = i2c_smbus_read_byte_data(client, reg);
  81. if (ret < 0)
  82. dev_err(&client->dev, "Read Error\n");
  83. return ret;
  84. }
  85. static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
  86. {
  87. return i2c_smbus_write_byte_data(client, reg, val);
  88. }
  89. #ifdef CONFIG_GPIOLIB
  90. static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
  91. {
  92. struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
  93. unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
  94. unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
  95. return !!(adp5589_read(kpad->client, ADP5589_GPI_STATUS_A + bank) &
  96. bit);
  97. }
  98. static void adp5589_gpio_set_value(struct gpio_chip *chip,
  99. unsigned off, int val)
  100. {
  101. struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
  102. unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
  103. unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
  104. mutex_lock(&kpad->gpio_lock);
  105. if (val)
  106. kpad->dat_out[bank] |= bit;
  107. else
  108. kpad->dat_out[bank] &= ~bit;
  109. adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
  110. kpad->dat_out[bank]);
  111. mutex_unlock(&kpad->gpio_lock);
  112. }
  113. static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  114. {
  115. struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
  116. unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
  117. unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
  118. int ret;
  119. mutex_lock(&kpad->gpio_lock);
  120. kpad->dir[bank] &= ~bit;
  121. ret = adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
  122. kpad->dir[bank]);
  123. mutex_unlock(&kpad->gpio_lock);
  124. return ret;
  125. }
  126. static int adp5589_gpio_direction_output(struct gpio_chip *chip,
  127. unsigned off, int val)
  128. {
  129. struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
  130. unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
  131. unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
  132. int ret;
  133. mutex_lock(&kpad->gpio_lock);
  134. kpad->dir[bank] |= bit;
  135. if (val)
  136. kpad->dat_out[bank] |= bit;
  137. else
  138. kpad->dat_out[bank] &= ~bit;
  139. ret = adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
  140. kpad->dat_out[bank]);
  141. ret |= adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
  142. kpad->dir[bank]);
  143. mutex_unlock(&kpad->gpio_lock);
  144. return ret;
  145. }
  146. static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
  147. const struct adp5589_kpad_platform_data *pdata)
  148. {
  149. bool pin_used[MAXGPIO];
  150. int n_unused = 0;
  151. int i;
  152. memset(pin_used, false, sizeof(pin_used));
  153. for (i = 0; i < MAXGPIO; i++)
  154. if (pdata->keypad_en_mask & (1 << i))
  155. pin_used[i] = true;
  156. for (i = 0; i < kpad->gpimapsize; i++)
  157. pin_used[kpad->gpimap[i].pin - ADP5589_GPI_PIN_BASE] = true;
  158. if (kpad->extend_cfg & R4_EXTEND_CFG)
  159. pin_used[4] = true;
  160. if (kpad->extend_cfg & C4_EXTEND_CFG)
  161. pin_used[12] = true;
  162. for (i = 0; i < MAXGPIO; i++)
  163. if (!pin_used[i])
  164. kpad->gpiomap[n_unused++] = i;
  165. return n_unused;
  166. }
  167. static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
  168. {
  169. struct device *dev = &kpad->client->dev;
  170. const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
  171. const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
  172. int i, error;
  173. if (!gpio_data)
  174. return 0;
  175. kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
  176. if (kpad->gc.ngpio == 0) {
  177. dev_info(dev, "No unused gpios left to export\n");
  178. return 0;
  179. }
  180. kpad->export_gpio = true;
  181. kpad->gc.direction_input = adp5589_gpio_direction_input;
  182. kpad->gc.direction_output = adp5589_gpio_direction_output;
  183. kpad->gc.get = adp5589_gpio_get_value;
  184. kpad->gc.set = adp5589_gpio_set_value;
  185. kpad->gc.can_sleep = 1;
  186. kpad->gc.base = gpio_data->gpio_start;
  187. kpad->gc.label = kpad->client->name;
  188. kpad->gc.owner = THIS_MODULE;
  189. mutex_init(&kpad->gpio_lock);
  190. error = gpiochip_add(&kpad->gc);
  191. if (error) {
  192. dev_err(dev, "gpiochip_add failed, err: %d\n", error);
  193. return error;
  194. }
  195. for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
  196. kpad->dat_out[i] = adp5589_read(kpad->client,
  197. ADP5589_GPO_DATA_OUT_A + i);
  198. kpad->dir[i] = adp5589_read(kpad->client,
  199. ADP5589_GPIO_DIRECTION_A + i);
  200. }
  201. if (gpio_data->setup) {
  202. error = gpio_data->setup(kpad->client,
  203. kpad->gc.base, kpad->gc.ngpio,
  204. gpio_data->context);
  205. if (error)
  206. dev_warn(dev, "setup failed, %d\n", error);
  207. }
  208. return 0;
  209. }
  210. static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
  211. {
  212. struct device *dev = &kpad->client->dev;
  213. const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
  214. const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
  215. int error;
  216. if (!kpad->export_gpio)
  217. return;
  218. if (gpio_data->teardown) {
  219. error = gpio_data->teardown(kpad->client,
  220. kpad->gc.base, kpad->gc.ngpio,
  221. gpio_data->context);
  222. if (error)
  223. dev_warn(dev, "teardown failed %d\n", error);
  224. }
  225. error = gpiochip_remove(&kpad->gc);
  226. if (error)
  227. dev_warn(dev, "gpiochip_remove failed %d\n", error);
  228. }
  229. #else
  230. static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
  231. {
  232. return 0;
  233. }
  234. static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
  235. {
  236. }
  237. #endif
  238. static void adp5589_report_switches(struct adp5589_kpad *kpad,
  239. int key, int key_val)
  240. {
  241. int i;
  242. for (i = 0; i < kpad->gpimapsize; i++) {
  243. if (key_val == kpad->gpimap[i].pin) {
  244. input_report_switch(kpad->input,
  245. kpad->gpimap[i].sw_evt,
  246. key & KEY_EV_PRESSED);
  247. break;
  248. }
  249. }
  250. }
  251. static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
  252. {
  253. int i;
  254. for (i = 0; i < ev_cnt; i++) {
  255. int key = adp5589_read(kpad->client, ADP5589_FIFO_1 + i);
  256. int key_val = key & KEY_EV_MASK;
  257. if (key_val >= ADP5589_GPI_PIN_BASE &&
  258. key_val <= ADP5589_GPI_PIN_END) {
  259. adp5589_report_switches(kpad, key, key_val);
  260. } else {
  261. input_report_key(kpad->input,
  262. kpad->keycode[key_val - 1],
  263. key & KEY_EV_PRESSED);
  264. }
  265. }
  266. }
  267. static irqreturn_t adp5589_irq(int irq, void *handle)
  268. {
  269. struct adp5589_kpad *kpad = handle;
  270. struct i2c_client *client = kpad->client;
  271. int status, ev_cnt;
  272. status = adp5589_read(client, ADP5589_INT_STATUS);
  273. if (status & OVRFLOW_INT) /* Unlikely and should never happen */
  274. dev_err(&client->dev, "Event Overflow Error\n");
  275. if (status & EVENT_INT) {
  276. ev_cnt = adp5589_read(client, ADP5589_STATUS) & KEC;
  277. if (ev_cnt) {
  278. adp5589_report_events(kpad, ev_cnt);
  279. input_sync(kpad->input);
  280. }
  281. }
  282. adp5589_write(client, ADP5589_INT_STATUS, status); /* Status is W1C */
  283. return IRQ_HANDLED;
  284. }
  285. static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
  286. {
  287. int i;
  288. for (i = 0; i < ADP5589_KEYMAPSIZE; i++)
  289. if (key == kpad->keycode[i])
  290. return (i + 1) | KEY_EV_PRESSED;
  291. dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
  292. return -EINVAL;
  293. }
  294. static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
  295. {
  296. struct i2c_client *client = kpad->client;
  297. const struct adp5589_kpad_platform_data *pdata =
  298. client->dev.platform_data;
  299. int i, ret;
  300. unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
  301. unsigned char pull_mask = 0;
  302. ret = adp5589_write(client, ADP5589_PIN_CONFIG_A,
  303. pdata->keypad_en_mask & 0xFF);
  304. ret |= adp5589_write(client, ADP5589_PIN_CONFIG_B,
  305. (pdata->keypad_en_mask >> 8) & 0xFF);
  306. ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
  307. (pdata->keypad_en_mask >> 16) & 0xFF);
  308. if (pdata->en_keylock) {
  309. ret |= adp5589_write(client, ADP5589_UNLOCK1,
  310. pdata->unlock_key1);
  311. ret |= adp5589_write(client, ADP5589_UNLOCK2,
  312. pdata->unlock_key2);
  313. ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
  314. pdata->unlock_timer & LTIME_MASK);
  315. ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
  316. }
  317. for (i = 0; i < KEYP_MAX_EVENT; i++)
  318. ret |= adp5589_read(client, ADP5589_FIFO_1 + i);
  319. for (i = 0; i < pdata->gpimapsize; i++) {
  320. unsigned short pin = pdata->gpimap[i].pin;
  321. if (pin <= ADP5589_GPI_PIN_ROW_END) {
  322. evt_mode1 |= (1 << (pin - ADP5589_GPI_PIN_ROW_BASE));
  323. } else {
  324. evt_mode2 |=
  325. ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) & 0xFF);
  326. evt_mode3 |=
  327. ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) >> 8);
  328. }
  329. }
  330. if (pdata->gpimapsize) {
  331. ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_A, evt_mode1);
  332. ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_B, evt_mode2);
  333. ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_C, evt_mode3);
  334. }
  335. if (pdata->pull_dis_mask & pdata->pullup_en_100k &
  336. pdata->pullup_en_300k & pdata->pulldown_en_300k)
  337. dev_warn(&client->dev, "Conflicting pull resistor config\n");
  338. for (i = 0; i < MAXGPIO; i++) {
  339. unsigned val = 0;
  340. if (pdata->pullup_en_300k & (1 << i))
  341. val = 0;
  342. else if (pdata->pulldown_en_300k & (1 << i))
  343. val = 1;
  344. else if (pdata->pullup_en_100k & (1 << i))
  345. val = 2;
  346. else if (pdata->pull_dis_mask & (1 << i))
  347. val = 3;
  348. pull_mask |= val << (2 * (i & 0x3));
  349. if ((i & 0x3) == 0x3 || i == MAXGPIO - 1) {
  350. ret |= adp5589_write(client,
  351. ADP5589_RPULL_CONFIG_A + (i >> 2),
  352. pull_mask);
  353. pull_mask = 0;
  354. }
  355. }
  356. if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
  357. ret |= adp5589_write(client, ADP5589_RESET1_EVENT_A,
  358. adp5589_get_evcode(kpad,
  359. pdata->reset1_key_1));
  360. ret |= adp5589_write(client, ADP5589_RESET1_EVENT_B,
  361. adp5589_get_evcode(kpad,
  362. pdata->reset1_key_2));
  363. ret |= adp5589_write(client, ADP5589_RESET1_EVENT_C,
  364. adp5589_get_evcode(kpad,
  365. pdata->reset1_key_3));
  366. kpad->extend_cfg |= R4_EXTEND_CFG;
  367. }
  368. if (pdata->reset2_key_1 && pdata->reset2_key_2) {
  369. ret |= adp5589_write(client, ADP5589_RESET2_EVENT_A,
  370. adp5589_get_evcode(kpad,
  371. pdata->reset2_key_1));
  372. ret |= adp5589_write(client, ADP5589_RESET2_EVENT_B,
  373. adp5589_get_evcode(kpad,
  374. pdata->reset2_key_2));
  375. kpad->extend_cfg |= C4_EXTEND_CFG;
  376. }
  377. if (kpad->extend_cfg) {
  378. ret |= adp5589_write(client, ADP5589_RESET_CFG,
  379. pdata->reset_cfg);
  380. ret |= adp5589_write(client, ADP5589_PIN_CONFIG_D,
  381. kpad->extend_cfg);
  382. }
  383. for (i = 0; i <= ADP_BANK(MAXGPIO); i++)
  384. ret |= adp5589_write(client, ADP5589_DEBOUNCE_DIS_A + i,
  385. pdata->debounce_dis_mask >> (i * 8));
  386. ret |= adp5589_write(client, ADP5589_POLL_PTIME_CFG,
  387. pdata->scan_cycle_time & PTIME_MASK);
  388. ret |= adp5589_write(client, ADP5589_INT_STATUS, LOGIC2_INT |
  389. LOGIC1_INT | OVRFLOW_INT | LOCK_INT |
  390. GPI_INT | EVENT_INT); /* Status is W1C */
  391. ret |= adp5589_write(client, ADP5589_GENERAL_CFG,
  392. INT_CFG | OSC_EN | CORE_CLK(3));
  393. ret |= adp5589_write(client, ADP5589_INT_EN,
  394. OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
  395. if (ret < 0) {
  396. dev_err(&client->dev, "Write Error\n");
  397. return ret;
  398. }
  399. return 0;
  400. }
  401. static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
  402. {
  403. int gpi_stat1 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_A);
  404. int gpi_stat2 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_B);
  405. int gpi_stat3 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_C);
  406. int gpi_stat_tmp, pin_loc;
  407. int i;
  408. for (i = 0; i < kpad->gpimapsize; i++) {
  409. unsigned short pin = kpad->gpimap[i].pin;
  410. if (pin <= ADP5589_GPI_PIN_ROW_END) {
  411. gpi_stat_tmp = gpi_stat1;
  412. pin_loc = pin - ADP5589_GPI_PIN_ROW_BASE;
  413. } else if ((pin - ADP5589_GPI_PIN_COL_BASE) < 8) {
  414. gpi_stat_tmp = gpi_stat2;
  415. pin_loc = pin - ADP5589_GPI_PIN_COL_BASE;
  416. } else {
  417. gpi_stat_tmp = gpi_stat3;
  418. pin_loc = pin - ADP5589_GPI_PIN_COL_BASE - 8;
  419. }
  420. if (gpi_stat_tmp < 0) {
  421. dev_err(&kpad->client->dev,
  422. "Can't read GPIO_DAT_STAT switch"
  423. " %d default to OFF\n", pin);
  424. gpi_stat_tmp = 0;
  425. }
  426. input_report_switch(kpad->input,
  427. kpad->gpimap[i].sw_evt,
  428. !(gpi_stat_tmp & (1 << pin_loc)));
  429. }
  430. input_sync(kpad->input);
  431. }
  432. static int __devinit adp5589_probe(struct i2c_client *client,
  433. const struct i2c_device_id *id)
  434. {
  435. struct adp5589_kpad *kpad;
  436. const struct adp5589_kpad_platform_data *pdata;
  437. struct input_dev *input;
  438. unsigned int revid;
  439. int ret, i;
  440. int error;
  441. if (!i2c_check_functionality(client->adapter,
  442. I2C_FUNC_SMBUS_BYTE_DATA)) {
  443. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  444. return -EIO;
  445. }
  446. pdata = client->dev.platform_data;
  447. if (!pdata) {
  448. dev_err(&client->dev, "no platform data?\n");
  449. return -EINVAL;
  450. }
  451. if (!((pdata->keypad_en_mask & 0xFF) &&
  452. (pdata->keypad_en_mask >> 8)) || !pdata->keymap) {
  453. dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
  454. return -EINVAL;
  455. }
  456. if (pdata->keymapsize != ADP5589_KEYMAPSIZE) {
  457. dev_err(&client->dev, "invalid keymapsize\n");
  458. return -EINVAL;
  459. }
  460. if (!pdata->gpimap && pdata->gpimapsize) {
  461. dev_err(&client->dev, "invalid gpimap from pdata\n");
  462. return -EINVAL;
  463. }
  464. if (pdata->gpimapsize > ADP5589_GPIMAPSIZE_MAX) {
  465. dev_err(&client->dev, "invalid gpimapsize\n");
  466. return -EINVAL;
  467. }
  468. for (i = 0; i < pdata->gpimapsize; i++) {
  469. unsigned short pin = pdata->gpimap[i].pin;
  470. if (pin < ADP5589_GPI_PIN_BASE || pin > ADP5589_GPI_PIN_END) {
  471. dev_err(&client->dev, "invalid gpi pin data\n");
  472. return -EINVAL;
  473. }
  474. if ((1 << (pin - ADP5589_GPI_PIN_ROW_BASE)) &
  475. pdata->keypad_en_mask) {
  476. dev_err(&client->dev, "invalid gpi row/col data\n");
  477. return -EINVAL;
  478. }
  479. }
  480. if (!client->irq) {
  481. dev_err(&client->dev, "no IRQ?\n");
  482. return -EINVAL;
  483. }
  484. kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
  485. input = input_allocate_device();
  486. if (!kpad || !input) {
  487. error = -ENOMEM;
  488. goto err_free_mem;
  489. }
  490. kpad->client = client;
  491. kpad->input = input;
  492. ret = adp5589_read(client, ADP5589_ID);
  493. if (ret < 0) {
  494. error = ret;
  495. goto err_free_mem;
  496. }
  497. revid = (u8) ret & ADP5589_DEVICE_ID_MASK;
  498. input->name = client->name;
  499. input->phys = "adp5589-keys/input0";
  500. input->dev.parent = &client->dev;
  501. input_set_drvdata(input, kpad);
  502. input->id.bustype = BUS_I2C;
  503. input->id.vendor = 0x0001;
  504. input->id.product = 0x0001;
  505. input->id.version = revid;
  506. input->keycodesize = sizeof(kpad->keycode[0]);
  507. input->keycodemax = pdata->keymapsize;
  508. input->keycode = kpad->keycode;
  509. memcpy(kpad->keycode, pdata->keymap,
  510. pdata->keymapsize * input->keycodesize);
  511. kpad->gpimap = pdata->gpimap;
  512. kpad->gpimapsize = pdata->gpimapsize;
  513. /* setup input device */
  514. __set_bit(EV_KEY, input->evbit);
  515. if (pdata->repeat)
  516. __set_bit(EV_REP, input->evbit);
  517. for (i = 0; i < input->keycodemax; i++)
  518. __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
  519. __clear_bit(KEY_RESERVED, input->keybit);
  520. if (kpad->gpimapsize)
  521. __set_bit(EV_SW, input->evbit);
  522. for (i = 0; i < kpad->gpimapsize; i++)
  523. __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
  524. error = input_register_device(input);
  525. if (error) {
  526. dev_err(&client->dev, "unable to register input device\n");
  527. goto err_free_mem;
  528. }
  529. error = request_threaded_irq(client->irq, NULL, adp5589_irq,
  530. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  531. client->dev.driver->name, kpad);
  532. if (error) {
  533. dev_err(&client->dev, "irq %d busy?\n", client->irq);
  534. goto err_unreg_dev;
  535. }
  536. error = adp5589_setup(kpad);
  537. if (error)
  538. goto err_free_irq;
  539. if (kpad->gpimapsize)
  540. adp5589_report_switch_state(kpad);
  541. error = adp5589_gpio_add(kpad);
  542. if (error)
  543. goto err_free_irq;
  544. device_init_wakeup(&client->dev, 1);
  545. i2c_set_clientdata(client, kpad);
  546. dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
  547. return 0;
  548. err_free_irq:
  549. free_irq(client->irq, kpad);
  550. err_unreg_dev:
  551. input_unregister_device(input);
  552. input = NULL;
  553. err_free_mem:
  554. input_free_device(input);
  555. kfree(kpad);
  556. return error;
  557. }
  558. static int __devexit adp5589_remove(struct i2c_client *client)
  559. {
  560. struct adp5589_kpad *kpad = i2c_get_clientdata(client);
  561. adp5589_write(client, ADP5589_GENERAL_CFG, 0);
  562. free_irq(client->irq, kpad);
  563. input_unregister_device(kpad->input);
  564. adp5589_gpio_remove(kpad);
  565. kfree(kpad);
  566. return 0;
  567. }
  568. #ifdef CONFIG_PM_SLEEP
  569. static int adp5589_suspend(struct device *dev)
  570. {
  571. struct adp5589_kpad *kpad = dev_get_drvdata(dev);
  572. struct i2c_client *client = kpad->client;
  573. disable_irq(client->irq);
  574. if (device_may_wakeup(&client->dev))
  575. enable_irq_wake(client->irq);
  576. return 0;
  577. }
  578. static int adp5589_resume(struct device *dev)
  579. {
  580. struct adp5589_kpad *kpad = dev_get_drvdata(dev);
  581. struct i2c_client *client = kpad->client;
  582. if (device_may_wakeup(&client->dev))
  583. disable_irq_wake(client->irq);
  584. enable_irq(client->irq);
  585. return 0;
  586. }
  587. #endif
  588. static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
  589. static const struct i2c_device_id adp5589_id[] = {
  590. {"adp5589-keys", 0},
  591. {}
  592. };
  593. MODULE_DEVICE_TABLE(i2c, adp5589_id);
  594. static struct i2c_driver adp5589_driver = {
  595. .driver = {
  596. .name = KBUILD_MODNAME,
  597. .owner = THIS_MODULE,
  598. .pm = &adp5589_dev_pm_ops,
  599. },
  600. .probe = adp5589_probe,
  601. .remove = __devexit_p(adp5589_remove),
  602. .id_table = adp5589_id,
  603. };
  604. static int __init adp5589_init(void)
  605. {
  606. return i2c_add_driver(&adp5589_driver);
  607. }
  608. module_init(adp5589_init);
  609. static void __exit adp5589_exit(void)
  610. {
  611. i2c_del_driver(&adp5589_driver);
  612. }
  613. module_exit(adp5589_exit);
  614. MODULE_LICENSE("GPL");
  615. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  616. MODULE_DESCRIPTION("ADP5589 Keypad driver");