spear-keyboard.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * SPEAr Keyboard Driver
  3. * Based on omap-keypad driver
  4. *
  5. * Copyright (C) 2010 ST Microelectronics
  6. * Rajeev Kumar <rajeevkumar.linux@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/errno.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/input.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_wakeup.h>
  23. #include <linux/slab.h>
  24. #include <linux/types.h>
  25. #include <linux/platform_data/keyboard-spear.h>
  26. /* Keyboard Registers */
  27. #define MODE_CTL_REG 0x00
  28. #define STATUS_REG 0x0C
  29. #define DATA_REG 0x10
  30. #define INTR_MASK 0x54
  31. /* Register Values */
  32. #define NUM_ROWS 16
  33. #define NUM_COLS 16
  34. #define MODE_CTL_PCLK_FREQ_SHIFT 9
  35. #define MODE_CTL_PCLK_FREQ_MSK 0x7F
  36. #define MODE_CTL_KEYBOARD (0x2 << 0)
  37. #define MODE_CTL_SCAN_RATE_10 (0x0 << 2)
  38. #define MODE_CTL_SCAN_RATE_20 (0x1 << 2)
  39. #define MODE_CTL_SCAN_RATE_40 (0x2 << 2)
  40. #define MODE_CTL_SCAN_RATE_80 (0x3 << 2)
  41. #define MODE_CTL_KEYNUM_SHIFT 6
  42. #define MODE_CTL_START_SCAN (0x1 << 8)
  43. #define STATUS_DATA_AVAIL (0x1 << 1)
  44. #define DATA_ROW_MASK 0xF0
  45. #define DATA_COLUMN_MASK 0x0F
  46. #define ROW_SHIFT 4
  47. struct spear_kbd {
  48. struct input_dev *input;
  49. void __iomem *io_base;
  50. struct clk *clk;
  51. unsigned int irq;
  52. unsigned int mode;
  53. unsigned int suspended_rate;
  54. unsigned short last_key;
  55. unsigned short keycodes[NUM_ROWS * NUM_COLS];
  56. bool rep;
  57. bool irq_wake_enabled;
  58. u32 mode_ctl_reg;
  59. };
  60. static irqreturn_t spear_kbd_interrupt(int irq, void *dev_id)
  61. {
  62. struct spear_kbd *kbd = dev_id;
  63. struct input_dev *input = kbd->input;
  64. unsigned int key;
  65. u32 sts, val;
  66. sts = readl_relaxed(kbd->io_base + STATUS_REG);
  67. if (!(sts & STATUS_DATA_AVAIL))
  68. return IRQ_NONE;
  69. if (kbd->last_key != KEY_RESERVED) {
  70. input_report_key(input, kbd->last_key, 0);
  71. kbd->last_key = KEY_RESERVED;
  72. }
  73. /* following reads active (row, col) pair */
  74. val = readl_relaxed(kbd->io_base + DATA_REG) &
  75. (DATA_ROW_MASK | DATA_COLUMN_MASK);
  76. key = kbd->keycodes[val];
  77. input_event(input, EV_MSC, MSC_SCAN, val);
  78. input_report_key(input, key, 1);
  79. input_sync(input);
  80. kbd->last_key = key;
  81. /* clear interrupt */
  82. writel_relaxed(0, kbd->io_base + STATUS_REG);
  83. return IRQ_HANDLED;
  84. }
  85. static int spear_kbd_open(struct input_dev *dev)
  86. {
  87. struct spear_kbd *kbd = input_get_drvdata(dev);
  88. int error;
  89. u32 val;
  90. kbd->last_key = KEY_RESERVED;
  91. error = clk_enable(kbd->clk);
  92. if (error)
  93. return error;
  94. /* keyboard rate to be programmed is input clock (in MHz) - 1 */
  95. val = clk_get_rate(kbd->clk) / 1000000 - 1;
  96. val = (val & MODE_CTL_PCLK_FREQ_MSK) << MODE_CTL_PCLK_FREQ_SHIFT;
  97. /* program keyboard */
  98. val = MODE_CTL_SCAN_RATE_80 | MODE_CTL_KEYBOARD | val |
  99. (kbd->mode << MODE_CTL_KEYNUM_SHIFT);
  100. writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
  101. writel_relaxed(1, kbd->io_base + STATUS_REG);
  102. /* start key scan */
  103. val = readl_relaxed(kbd->io_base + MODE_CTL_REG);
  104. val |= MODE_CTL_START_SCAN;
  105. writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
  106. return 0;
  107. }
  108. static void spear_kbd_close(struct input_dev *dev)
  109. {
  110. struct spear_kbd *kbd = input_get_drvdata(dev);
  111. u32 val;
  112. /* stop key scan */
  113. val = readl_relaxed(kbd->io_base + MODE_CTL_REG);
  114. val &= ~MODE_CTL_START_SCAN;
  115. writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
  116. clk_disable(kbd->clk);
  117. kbd->last_key = KEY_RESERVED;
  118. }
  119. #ifdef CONFIG_OF
  120. static int spear_kbd_parse_dt(struct platform_device *pdev,
  121. struct spear_kbd *kbd)
  122. {
  123. struct device_node *np = pdev->dev.of_node;
  124. int error;
  125. u32 val, suspended_rate;
  126. if (!np) {
  127. dev_err(&pdev->dev, "Missing DT data\n");
  128. return -EINVAL;
  129. }
  130. if (of_property_read_bool(np, "autorepeat"))
  131. kbd->rep = true;
  132. if (of_property_read_u32(np, "suspended_rate", &suspended_rate))
  133. kbd->suspended_rate = suspended_rate;
  134. error = of_property_read_u32(np, "st,mode", &val);
  135. if (error) {
  136. dev_err(&pdev->dev, "DT: Invalid or missing mode\n");
  137. return error;
  138. }
  139. kbd->mode = val;
  140. return 0;
  141. }
  142. #else
  143. static inline int spear_kbd_parse_dt(struct platform_device *pdev,
  144. struct spear_kbd *kbd)
  145. {
  146. return -ENOSYS;
  147. }
  148. #endif
  149. static int spear_kbd_probe(struct platform_device *pdev)
  150. {
  151. struct kbd_platform_data *pdata = dev_get_platdata(&pdev->dev);
  152. const struct matrix_keymap_data *keymap = pdata ? pdata->keymap : NULL;
  153. struct spear_kbd *kbd;
  154. struct input_dev *input_dev;
  155. struct resource *res;
  156. int irq;
  157. int error;
  158. irq = platform_get_irq(pdev, 0);
  159. if (irq < 0) {
  160. dev_err(&pdev->dev, "not able to get irq for the device\n");
  161. return irq;
  162. }
  163. kbd = devm_kzalloc(&pdev->dev, sizeof(*kbd), GFP_KERNEL);
  164. if (!kbd) {
  165. dev_err(&pdev->dev, "not enough memory for driver data\n");
  166. return -ENOMEM;
  167. }
  168. input_dev = devm_input_allocate_device(&pdev->dev);
  169. if (!input_dev) {
  170. dev_err(&pdev->dev, "unable to allocate input device\n");
  171. return -ENOMEM;
  172. }
  173. kbd->input = input_dev;
  174. kbd->irq = irq;
  175. if (!pdata) {
  176. error = spear_kbd_parse_dt(pdev, kbd);
  177. if (error)
  178. return error;
  179. } else {
  180. kbd->mode = pdata->mode;
  181. kbd->rep = pdata->rep;
  182. kbd->suspended_rate = pdata->suspended_rate;
  183. }
  184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. kbd->io_base = devm_ioremap_resource(&pdev->dev, res);
  186. if (IS_ERR(kbd->io_base))
  187. return PTR_ERR(kbd->io_base);
  188. kbd->clk = devm_clk_get(&pdev->dev, NULL);
  189. if (IS_ERR(kbd->clk))
  190. return PTR_ERR(kbd->clk);
  191. input_dev->name = "Spear Keyboard";
  192. input_dev->phys = "keyboard/input0";
  193. input_dev->id.bustype = BUS_HOST;
  194. input_dev->id.vendor = 0x0001;
  195. input_dev->id.product = 0x0001;
  196. input_dev->id.version = 0x0100;
  197. input_dev->open = spear_kbd_open;
  198. input_dev->close = spear_kbd_close;
  199. error = matrix_keypad_build_keymap(keymap, NULL, NUM_ROWS, NUM_COLS,
  200. kbd->keycodes, input_dev);
  201. if (error) {
  202. dev_err(&pdev->dev, "Failed to build keymap\n");
  203. return error;
  204. }
  205. if (kbd->rep)
  206. __set_bit(EV_REP, input_dev->evbit);
  207. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  208. input_set_drvdata(input_dev, kbd);
  209. error = devm_request_irq(&pdev->dev, irq, spear_kbd_interrupt, 0,
  210. "keyboard", kbd);
  211. if (error) {
  212. dev_err(&pdev->dev, "request_irq failed\n");
  213. return error;
  214. }
  215. error = clk_prepare(kbd->clk);
  216. if (error)
  217. return error;
  218. error = input_register_device(input_dev);
  219. if (error) {
  220. dev_err(&pdev->dev, "Unable to register keyboard device\n");
  221. clk_unprepare(kbd->clk);
  222. return error;
  223. }
  224. device_init_wakeup(&pdev->dev, 1);
  225. platform_set_drvdata(pdev, kbd);
  226. return 0;
  227. }
  228. static int spear_kbd_remove(struct platform_device *pdev)
  229. {
  230. struct spear_kbd *kbd = platform_get_drvdata(pdev);
  231. input_unregister_device(kbd->input);
  232. clk_unprepare(kbd->clk);
  233. device_init_wakeup(&pdev->dev, 0);
  234. return 0;
  235. }
  236. #ifdef CONFIG_PM
  237. static int spear_kbd_suspend(struct device *dev)
  238. {
  239. struct platform_device *pdev = to_platform_device(dev);
  240. struct spear_kbd *kbd = platform_get_drvdata(pdev);
  241. struct input_dev *input_dev = kbd->input;
  242. unsigned int rate = 0, mode_ctl_reg, val;
  243. mutex_lock(&input_dev->mutex);
  244. /* explicitly enable clock as we may program device */
  245. clk_enable(kbd->clk);
  246. mode_ctl_reg = readl_relaxed(kbd->io_base + MODE_CTL_REG);
  247. if (device_may_wakeup(&pdev->dev)) {
  248. if (!enable_irq_wake(kbd->irq))
  249. kbd->irq_wake_enabled = true;
  250. /*
  251. * reprogram the keyboard operating frequency as on some
  252. * platform it may change during system suspended
  253. */
  254. if (kbd->suspended_rate)
  255. rate = kbd->suspended_rate / 1000000 - 1;
  256. else
  257. rate = clk_get_rate(kbd->clk) / 1000000 - 1;
  258. val = mode_ctl_reg &
  259. ~(MODE_CTL_PCLK_FREQ_MSK << MODE_CTL_PCLK_FREQ_SHIFT);
  260. val |= (rate & MODE_CTL_PCLK_FREQ_MSK)
  261. << MODE_CTL_PCLK_FREQ_SHIFT;
  262. writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
  263. } else {
  264. if (input_dev->users) {
  265. writel_relaxed(mode_ctl_reg & ~MODE_CTL_START_SCAN,
  266. kbd->io_base + MODE_CTL_REG);
  267. clk_disable(kbd->clk);
  268. }
  269. }
  270. /* store current configuration */
  271. if (input_dev->users)
  272. kbd->mode_ctl_reg = mode_ctl_reg;
  273. /* restore previous clk state */
  274. clk_disable(kbd->clk);
  275. mutex_unlock(&input_dev->mutex);
  276. return 0;
  277. }
  278. static int spear_kbd_resume(struct device *dev)
  279. {
  280. struct platform_device *pdev = to_platform_device(dev);
  281. struct spear_kbd *kbd = platform_get_drvdata(pdev);
  282. struct input_dev *input_dev = kbd->input;
  283. mutex_lock(&input_dev->mutex);
  284. if (device_may_wakeup(&pdev->dev)) {
  285. if (kbd->irq_wake_enabled) {
  286. kbd->irq_wake_enabled = false;
  287. disable_irq_wake(kbd->irq);
  288. }
  289. } else {
  290. if (input_dev->users)
  291. clk_enable(kbd->clk);
  292. }
  293. /* restore current configuration */
  294. if (input_dev->users)
  295. writel_relaxed(kbd->mode_ctl_reg, kbd->io_base + MODE_CTL_REG);
  296. mutex_unlock(&input_dev->mutex);
  297. return 0;
  298. }
  299. #endif
  300. static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops, spear_kbd_suspend, spear_kbd_resume);
  301. #ifdef CONFIG_OF
  302. static const struct of_device_id spear_kbd_id_table[] = {
  303. { .compatible = "st,spear300-kbd" },
  304. {}
  305. };
  306. MODULE_DEVICE_TABLE(of, spear_kbd_id_table);
  307. #endif
  308. static struct platform_driver spear_kbd_driver = {
  309. .probe = spear_kbd_probe,
  310. .remove = spear_kbd_remove,
  311. .driver = {
  312. .name = "keyboard",
  313. .pm = &spear_kbd_pm_ops,
  314. .of_match_table = of_match_ptr(spear_kbd_id_table),
  315. },
  316. };
  317. module_platform_driver(spear_kbd_driver);
  318. MODULE_AUTHOR("Rajeev Kumar");
  319. MODULE_DESCRIPTION("SPEAr Keyboard Driver");
  320. MODULE_LICENSE("GPL");