gpio_keypad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google, Inc. nor the names of its contributors
  17. * may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  30. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <assert.h>
  34. #include <bits.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <dev/keys.h>
  38. #include <dev/gpio.h>
  39. #include <dev/gpio_keypad.h>
  40. #include <dev/ssbi.h>
  41. #include <kernel/event.h>
  42. #include <kernel/timer.h>
  43. #include <reg.h>
  44. #include <platform/iomap.h>
  45. #include <platform/timer.h>
  46. #include <platform.h>
  47. #define LINUX_MACHTYPE_8660_QT 3298
  48. struct gpio_kp {
  49. struct gpio_keypad_info *keypad_info;
  50. struct timer timer;
  51. event_t full_scan;
  52. int current_output;
  53. unsigned int some_keys_pressed:2;
  54. unsigned long keys_pressed[0];
  55. };
  56. struct gpio_qwerty_kp {
  57. struct qwerty_keypad_info *keypad_info;
  58. struct timer timer;
  59. event_t full_scan;
  60. unsigned int some_keys_pressed:2;
  61. unsigned long keys_pressed[0];
  62. };
  63. static struct gpio_qwerty_kp *qwerty_keypad;
  64. /* TODO: Support multiple keypads? */
  65. static struct gpio_kp *keypad;
  66. static void check_output(struct gpio_kp *kp, int out, int polarity)
  67. {
  68. struct gpio_keypad_info *kpinfo = kp->keypad_info;
  69. int key_index;
  70. int in;
  71. int gpio;
  72. int changed = 0;
  73. key_index = out * kpinfo->ninputs;
  74. for (in = 0; in < kpinfo->ninputs; in++, key_index++) {
  75. gpio = kpinfo->input_gpios[in];
  76. changed = 0;
  77. if (gpio_get(gpio) ^ !polarity) {
  78. if (kp->some_keys_pressed < 3)
  79. kp->some_keys_pressed++;
  80. changed = !bitmap_set(kp->keys_pressed, key_index);
  81. } else {
  82. changed = bitmap_clear(kp->keys_pressed, key_index);
  83. }
  84. if (changed) {
  85. int state = bitmap_test(kp->keys_pressed, key_index);
  86. keys_post_event(kpinfo->keymap[key_index], state);
  87. }
  88. }
  89. /* sets up the right state for the next poll cycle */
  90. gpio = kpinfo->output_gpios[out];
  91. if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
  92. gpio_set(gpio, !polarity);
  93. else
  94. gpio_config(gpio, GPIO_INPUT);
  95. }
  96. static enum handler_return
  97. gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg)
  98. {
  99. struct gpio_kp *kp = keypad;
  100. struct gpio_keypad_info *kpinfo = kp->keypad_info;
  101. int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH);
  102. int out;
  103. int gpio;
  104. out = kp->current_output;
  105. if (out == kpinfo->noutputs) {
  106. out = 0;
  107. kp->some_keys_pressed = 0;
  108. } else {
  109. check_output(kp, out, polarity);
  110. out++;
  111. }
  112. kp->current_output = out;
  113. if (out < kpinfo->noutputs) {
  114. gpio = kpinfo->output_gpios[out];
  115. if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
  116. gpio_set(gpio, polarity);
  117. else
  118. gpio_config(gpio, polarity ? GPIO_OUTPUT : 0);
  119. timer_set_oneshot(timer, kpinfo->settle_time,
  120. gpio_keypad_timer_func, NULL);
  121. goto done;
  122. }
  123. if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) {
  124. event_signal(&kp->full_scan, false);
  125. timer_set_oneshot(timer, kpinfo->poll_time,
  126. gpio_keypad_timer_func, NULL);
  127. goto done;
  128. }
  129. done:
  130. return INT_RESCHEDULE;
  131. }
  132. void gpio_keypad_init(struct gpio_keypad_info *kpinfo)
  133. {
  134. int key_count;
  135. int output_val;
  136. int output_cfg;
  137. int i;
  138. int len;
  139. ASSERT(kpinfo->keymap && kpinfo->input_gpios && kpinfo->output_gpios);
  140. key_count = kpinfo->ninputs * kpinfo->noutputs;
  141. len = sizeof(struct gpio_kp) + (sizeof(unsigned long) *
  142. BITMAP_NUM_WORDS(key_count));
  143. keypad = malloc(len);
  144. ASSERT(keypad);
  145. memset(keypad, 0, len);
  146. keypad->keypad_info = kpinfo;
  147. output_val = (!!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH)) ^
  148. (!!(kpinfo->flags & GPIOKPF_DRIVE_INACTIVE));
  149. output_cfg = kpinfo->flags & GPIOKPF_DRIVE_INACTIVE ? GPIO_OUTPUT : 0;
  150. for (i = 0; i < kpinfo->noutputs; i++) {
  151. gpio_set(kpinfo->output_gpios[i], output_val);
  152. gpio_config(kpinfo->output_gpios[i], output_cfg);
  153. }
  154. for (i = 0; i < kpinfo->ninputs; i++)
  155. gpio_config(kpinfo->input_gpios[i], GPIO_INPUT);
  156. keypad->current_output = kpinfo->noutputs;
  157. event_init(&keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
  158. timer_initialize(&keypad->timer);
  159. timer_set_oneshot(&keypad->timer, 0, gpio_keypad_timer_func, NULL);
  160. /* wait for the keypad to complete one full scan */
  161. event_wait(&keypad->full_scan);
  162. }
  163. int pm8058_gpio_config(int gpio, struct pm8058_gpio *param)
  164. {
  165. int rc;
  166. write_func wr_function = (qwerty_keypad->keypad_info)->wr_func;
  167. unsigned char bank[8];
  168. static int dir_map[] = {
  169. PM8058_GPIO_MODE_OFF,
  170. PM8058_GPIO_MODE_OUTPUT,
  171. PM8058_GPIO_MODE_INPUT,
  172. PM8058_GPIO_MODE_BOTH,
  173. };
  174. if (param == 0) {
  175. dprintf (INFO, "pm8058_gpio struct not defined\n");
  176. return -1;
  177. }
  178. /* Select banks and configure the gpio */
  179. bank[0] = PM8058_GPIO_WRITE |
  180. ((param->vin_sel << PM8058_GPIO_VIN_SHIFT) &
  181. PM8058_GPIO_VIN_MASK) |
  182. PM8058_GPIO_MODE_ENABLE;
  183. bank[1] = PM8058_GPIO_WRITE |
  184. ((1 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
  185. ((dir_map[param->direction] << PM8058_GPIO_MODE_SHIFT) &
  186. PM8058_GPIO_MODE_MASK) |
  187. ((param->direction & PM_GPIO_DIR_OUT) ?
  188. PM8058_GPIO_OUT_BUFFER : 0);
  189. bank[2] = PM8058_GPIO_WRITE |
  190. ((2 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
  191. ((param->pull << PM8058_GPIO_PULL_SHIFT) &
  192. PM8058_GPIO_PULL_MASK);
  193. bank[3] = PM8058_GPIO_WRITE |
  194. ((3 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
  195. ((param->out_strength << PM8058_GPIO_OUT_STRENGTH_SHIFT) &
  196. PM8058_GPIO_OUT_STRENGTH_MASK);
  197. bank[4] = PM8058_GPIO_WRITE |
  198. ((4 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
  199. ((param->function << PM8058_GPIO_FUNC_SHIFT) &
  200. PM8058_GPIO_FUNC_MASK);
  201. rc = (*wr_function)(bank, 5, SSBI_REG_ADDR_GPIO(gpio));
  202. if (rc) {
  203. dprintf(INFO, "Failed on 1st ssbi_write(): rc=%d.\n", rc);
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. int pm8058_gpio_config_kypd_drv(int gpio_start, int num_gpios, unsigned mach_id)
  209. {
  210. int rc;
  211. struct pm8058_gpio kypd_drv = {
  212. .direction = PM_GPIO_DIR_OUT,
  213. .pull = PM_GPIO_PULL_NO,
  214. .vin_sel = 2,
  215. .out_strength = PM_GPIO_STRENGTH_LOW,
  216. .function = PM_GPIO_FUNC_1,
  217. .inv_int_pol = 1,
  218. };
  219. if(mach_id == LINUX_MACHTYPE_8660_QT)
  220. kypd_drv.function = PM_GPIO_FUNC_NORMAL;
  221. while (num_gpios--) {
  222. rc = pm8058_gpio_config(gpio_start++, &kypd_drv);
  223. if (rc) {
  224. dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc);
  225. return rc;
  226. }
  227. }
  228. return 0;
  229. }
  230. int pm8058_gpio_config_kypd_sns(int gpio_start, int num_gpios)
  231. {
  232. int rc;
  233. struct pm8058_gpio kypd_sns = {
  234. .direction = PM_GPIO_DIR_IN,
  235. .pull = PM_GPIO_PULL_UP1,
  236. .vin_sel = 2,
  237. .out_strength = PM_GPIO_STRENGTH_NO,
  238. .function = PM_GPIO_FUNC_NORMAL,
  239. .inv_int_pol = 1,
  240. };
  241. while (num_gpios--) {
  242. rc = pm8058_gpio_config(gpio_start++, &kypd_sns);
  243. if (rc) {
  244. dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc);
  245. return rc;
  246. }
  247. }
  248. return 0;
  249. }
  250. static void ssbi_gpio_init(unsigned int mach_id)
  251. {
  252. unsigned char kypd_cntl_init;
  253. unsigned char kypd_scan_init = 0x20;
  254. int rows = (qwerty_keypad->keypad_info)->rows;
  255. int columns = (qwerty_keypad->keypad_info)->columns;
  256. write_func wr_function = (qwerty_keypad->keypad_info)->wr_func;
  257. unsigned char drv_bits[] = {
  258. 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7 };
  259. unsigned char sns_bits[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3 };
  260. kypd_cntl_init = ((drv_bits[rows] << 2) | (sns_bits[columns] << 5) | (1 << 7));
  261. if ((*wr_function)(&kypd_cntl_init, 1, SSBI_REG_KYPD_CNTL_ADDR))
  262. dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
  263. if ((*wr_function)(&kypd_scan_init, 1, SSBI_REG_KYPD_SCAN_ADDR))
  264. dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_SCAN register\n");
  265. if(mach_id == LINUX_MACHTYPE_8660_QT)
  266. {
  267. pm8058_gpio_config_kypd_sns(QT_PMIC_GPIO_KYPD_SNS, rows);
  268. pm8058_gpio_config_kypd_drv(QT_PMIC_GPIO_KYPD_DRV, columns, mach_id);
  269. }
  270. else
  271. {
  272. pm8058_gpio_config_kypd_sns(SSBI_OFFSET_ADDR_GPIO_KYPD_SNS, columns);
  273. pm8058_gpio_config_kypd_drv(SSBI_OFFSET_ADDR_GPIO_KYPD_DRV, rows, mach_id);
  274. }
  275. }
  276. static enum handler_return
  277. scan_qwerty_keypad(struct timer *timer, time_t now, void *arg)
  278. {
  279. unsigned int rows = (qwerty_keypad->keypad_info)->rows;
  280. unsigned int columns = (qwerty_keypad->keypad_info)->columns;
  281. unsigned int num_of_ssbi_reads = (qwerty_keypad->keypad_info)->num_of_reads;
  282. read_func rd_function = (qwerty_keypad->keypad_info)->rd_func;
  283. unsigned char column_new_keys = 0x00;
  284. unsigned char column_old_keys = 0x00;
  285. int shift = 0;
  286. static int key_detected = -1;
  287. if ((*rd_function)((qwerty_keypad->keypad_info)->rec_keys, num_of_ssbi_reads,
  288. SSBI_REG_KYPD_REC_DATA_ADDR))
  289. dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
  290. if ((*rd_function)((qwerty_keypad->keypad_info)->old_keys, num_of_ssbi_reads,
  291. SSBI_REG_KYPD_OLD_DATA_ADDR))
  292. dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
  293. while (rows--) {
  294. columns = qwerty_keypad->keypad_info->columns;
  295. if (((qwerty_keypad->keypad_info)->rec_keys[rows]
  296. != (qwerty_keypad->keypad_info)->old_keys[rows])
  297. && ((qwerty_keypad->keypad_info)->rec_keys[rows] != 0x00)
  298. && ((qwerty_keypad->keypad_info)->old_keys[rows] != 0x00)) {
  299. while (columns--) {
  300. column_new_keys = ((qwerty_keypad->keypad_info)->rec_keys[rows]);
  301. column_old_keys = ((qwerty_keypad->keypad_info)->old_keys[rows]);
  302. if (((0x01 << columns) & (~column_new_keys))
  303. && !((0x01 << columns) & (~column_old_keys))) {
  304. shift = (rows * 8) + columns;
  305. if ((qwerty_keypad->keypad_info)->keymap[shift]) {
  306. if (shift != key_detected) {
  307. key_detected = shift;
  308. keys_post_event((qwerty_keypad->keypad_info)->keymap[shift], 1);
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. event_signal(&qwerty_keypad->full_scan, false);
  316. return INT_RESCHEDULE;
  317. }
  318. static enum handler_return
  319. scan_qt_keypad(struct timer *timer, time_t now, void *arg)
  320. {
  321. unsigned int gpio;
  322. unsigned int last_state=0;
  323. unsigned int new_state=0;
  324. unsigned int bits_changed;
  325. static int key_detected=-1;
  326. /* Row GPIOs 8,9,10 are used for sensing here */
  327. for (gpio=8;gpio<=10;gpio++)
  328. {
  329. bool status;
  330. status = pm8058_gpio_get(gpio);
  331. if (status == 0)
  332. new_state |= (1<<(gpio-8));
  333. }
  334. bits_changed = last_state ^ new_state;
  335. if (bits_changed)
  336. {
  337. int shift = 0;
  338. for (unsigned int rows = 0; rows < (qwerty_keypad->keypad_info)->rows; rows++)
  339. {
  340. if ((bits_changed & (1<<rows)) == 0)
  341. continue;
  342. shift = rows * 8 + 3;
  343. }
  344. if ((qwerty_keypad->keypad_info)->keymap[shift])
  345. {
  346. if (shift != key_detected)
  347. {
  348. key_detected = shift;
  349. keys_post_event((qwerty_keypad->keypad_info)->keymap[shift], 1);
  350. timer_set_oneshot(timer,(qwerty_keypad->keypad_info)->poll_time,
  351. scan_qt_keypad, NULL);
  352. return INT_RESCHEDULE;
  353. }
  354. }
  355. }
  356. event_signal(&qwerty_keypad->full_scan, false);
  357. return INT_RESCHEDULE;
  358. }
  359. void ssbi_keypad_init(struct qwerty_keypad_info *qwerty_kp)
  360. {
  361. unsigned int mach_id;
  362. int len;
  363. len = sizeof(struct gpio_qwerty_kp);
  364. qwerty_keypad = malloc(len);
  365. ASSERT(qwerty_keypad);
  366. memset(qwerty_keypad, 0, len);
  367. qwerty_keypad->keypad_info = qwerty_kp;
  368. event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
  369. timer_initialize(&qwerty_keypad->timer);
  370. mach_id = board_machtype();
  371. ssbi_gpio_init(mach_id);
  372. if(mach_id == LINUX_MACHTYPE_8660_QT)
  373. {
  374. mdelay((qwerty_keypad->keypad_info)->settle_time);
  375. #ifdef QT_8660_KEYPAD_HW_BUG
  376. timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qt_keypad, NULL);
  377. #endif
  378. }
  379. else
  380. timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qwerty_keypad, NULL);
  381. /* wait for the keypad to complete one full scan */
  382. event_wait(&qwerty_keypad->full_scan);
  383. }
  384. static enum handler_return
  385. scan_qwerty_gpio_keypad(struct timer *timer, time_t now, void *arg)
  386. {
  387. int i=0;
  388. int num =0;
  389. uint8_t key_status;
  390. struct qwerty_keypad_info *keypad = qwerty_keypad->keypad_info;
  391. num = keypad->mapsize;
  392. for(i=0; i < num; i++)
  393. {
  394. /*continue if not interested in key*/
  395. if(!keypad->keymap[i])
  396. continue;
  397. /*read key gpio*/
  398. keypad->key_gpio_get(keypad->gpiomap[i], &key_status);
  399. /*Post event if key pressed*/
  400. if(key_status)
  401. {
  402. keys_post_event(keypad->keymap[i], 1);
  403. goto done;
  404. }
  405. }
  406. done:
  407. event_signal(&qwerty_keypad->full_scan, false);
  408. return INT_RESCHEDULE;
  409. }
  410. void ssbi_gpio_keypad_init(struct qwerty_keypad_info *qwerty_kp)
  411. {
  412. int len;
  413. len = sizeof(struct gpio_qwerty_kp);
  414. qwerty_keypad = malloc(len);
  415. ASSERT(qwerty_keypad);
  416. memset(qwerty_keypad, 0, len);
  417. qwerty_keypad->keypad_info = qwerty_kp;
  418. event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
  419. timer_initialize(&qwerty_keypad->timer);
  420. timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qwerty_gpio_keypad, NULL);
  421. /* wait for the keypad to complete one full scan */
  422. event_wait(&qwerty_keypad->full_scan);
  423. }
  424. void pmic_write(unsigned address, unsigned data)
  425. {
  426. write_func wr_function = &i2c_ssbi_write_bytes;
  427. if (wr_function == NULL)
  428. return;
  429. if ((*wr_function)((unsigned char *) &data, 1, address))
  430. dprintf (CRITICAL, "Error in initializing register\n");
  431. }
  432. void toshiba_pmic_gpio_init(unsigned gpio)
  433. {
  434. pmic_write(gpio,0x85);
  435. pmic_write(gpio,0x98);
  436. pmic_write(gpio,0xB8);
  437. pmic_write(gpio,0xC6);
  438. }