adp5589-keys.c 19 KB

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