tm2-touchkey.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * TM2 touchkey device driver
  3. *
  4. * Copyright 2005 Phil Blundell
  5. * Copyright 2016 Samsung Electronics Co., Ltd.
  6. *
  7. * Author: Beomho Seo <beomho.seo@samsung.com>
  8. * Author: Jaechul Lee <jcsing.lee@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/leds.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/pm.h>
  25. #include <linux/regulator/consumer.h>
  26. #define TM2_TOUCHKEY_DEV_NAME "tm2-touchkey"
  27. #define TM2_TOUCHKEY_KEYCODE_REG 0x03
  28. #define TM2_TOUCHKEY_BASE_REG 0x00
  29. #define TM2_TOUCHKEY_CMD_LED_ON 0x10
  30. #define TM2_TOUCHKEY_CMD_LED_OFF 0x20
  31. #define TM2_TOUCHKEY_BIT_PRESS_EV BIT(3)
  32. #define TM2_TOUCHKEY_BIT_KEYCODE GENMASK(2, 0)
  33. #define TM2_TOUCHKEY_LED_VOLTAGE_MIN 2500000
  34. #define TM2_TOUCHKEY_LED_VOLTAGE_MAX 3300000
  35. enum {
  36. TM2_TOUCHKEY_KEY_MENU = 0x1,
  37. TM2_TOUCHKEY_KEY_BACK,
  38. };
  39. struct tm2_touchkey_data {
  40. struct i2c_client *client;
  41. struct input_dev *input_dev;
  42. struct led_classdev led_dev;
  43. struct regulator *vdd;
  44. struct regulator_bulk_data regulators[2];
  45. };
  46. static void tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
  47. enum led_brightness brightness)
  48. {
  49. struct tm2_touchkey_data *touchkey =
  50. container_of(led_dev, struct tm2_touchkey_data, led_dev);
  51. u32 volt;
  52. u8 data;
  53. if (brightness == LED_OFF) {
  54. volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
  55. data = TM2_TOUCHKEY_CMD_LED_OFF;
  56. } else {
  57. volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
  58. data = TM2_TOUCHKEY_CMD_LED_ON;
  59. }
  60. regulator_set_voltage(touchkey->vdd, volt, volt);
  61. i2c_smbus_write_byte_data(touchkey->client,
  62. TM2_TOUCHKEY_BASE_REG, data);
  63. }
  64. static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
  65. {
  66. int error;
  67. error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
  68. touchkey->regulators);
  69. if (error)
  70. return error;
  71. /* waiting for device initialization, at least 150ms */
  72. msleep(150);
  73. return 0;
  74. }
  75. static void tm2_touchkey_power_disable(void *data)
  76. {
  77. struct tm2_touchkey_data *touchkey = data;
  78. regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
  79. touchkey->regulators);
  80. }
  81. static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
  82. {
  83. struct tm2_touchkey_data *touchkey = devid;
  84. int data;
  85. int key;
  86. data = i2c_smbus_read_byte_data(touchkey->client,
  87. TM2_TOUCHKEY_KEYCODE_REG);
  88. if (data < 0) {
  89. dev_err(&touchkey->client->dev,
  90. "failed to read i2c data: %d\n", data);
  91. goto out;
  92. }
  93. switch (data & TM2_TOUCHKEY_BIT_KEYCODE) {
  94. case TM2_TOUCHKEY_KEY_MENU:
  95. key = KEY_PHONE;
  96. break;
  97. case TM2_TOUCHKEY_KEY_BACK:
  98. key = KEY_BACK;
  99. break;
  100. default:
  101. dev_warn(&touchkey->client->dev,
  102. "unhandled keycode, data %#02x\n", data);
  103. goto out;
  104. }
  105. if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
  106. input_report_key(touchkey->input_dev, KEY_PHONE, 0);
  107. input_report_key(touchkey->input_dev, KEY_BACK, 0);
  108. } else {
  109. input_report_key(touchkey->input_dev, key, 1);
  110. }
  111. input_sync(touchkey->input_dev);
  112. out:
  113. return IRQ_HANDLED;
  114. }
  115. static int tm2_touchkey_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id)
  117. {
  118. struct tm2_touchkey_data *touchkey;
  119. int error;
  120. if (!i2c_check_functionality(client->adapter,
  121. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
  122. dev_err(&client->dev, "incompatible I2C adapter\n");
  123. return -EIO;
  124. }
  125. touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
  126. if (!touchkey)
  127. return -ENOMEM;
  128. touchkey->client = client;
  129. i2c_set_clientdata(client, touchkey);
  130. touchkey->regulators[0].supply = "vcc";
  131. touchkey->regulators[1].supply = "vdd";
  132. error = devm_regulator_bulk_get(&client->dev,
  133. ARRAY_SIZE(touchkey->regulators),
  134. touchkey->regulators);
  135. if (error) {
  136. dev_err(&client->dev, "failed to get regulators: %d\n", error);
  137. return error;
  138. }
  139. /* Save VDD for easy access */
  140. touchkey->vdd = touchkey->regulators[1].consumer;
  141. error = tm2_touchkey_power_enable(touchkey);
  142. if (error) {
  143. dev_err(&client->dev, "failed to power up device: %d\n", error);
  144. return error;
  145. }
  146. error = devm_add_action_or_reset(&client->dev,
  147. tm2_touchkey_power_disable, touchkey);
  148. if (error) {
  149. dev_err(&client->dev,
  150. "failed to install poweroff handler: %d\n", error);
  151. return error;
  152. }
  153. /* input device */
  154. touchkey->input_dev = devm_input_allocate_device(&client->dev);
  155. if (!touchkey->input_dev) {
  156. dev_err(&client->dev, "failed to allocate input device\n");
  157. return -ENOMEM;
  158. }
  159. touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
  160. touchkey->input_dev->id.bustype = BUS_I2C;
  161. input_set_capability(touchkey->input_dev, EV_KEY, KEY_PHONE);
  162. input_set_capability(touchkey->input_dev, EV_KEY, KEY_BACK);
  163. error = input_register_device(touchkey->input_dev);
  164. if (error) {
  165. dev_err(&client->dev,
  166. "failed to register input device: %d\n", error);
  167. return error;
  168. }
  169. error = devm_request_threaded_irq(&client->dev, client->irq,
  170. NULL, tm2_touchkey_irq_handler,
  171. IRQF_ONESHOT,
  172. TM2_TOUCHKEY_DEV_NAME, touchkey);
  173. if (error) {
  174. dev_err(&client->dev,
  175. "failed to request threaded irq: %d\n", error);
  176. return error;
  177. }
  178. /* led device */
  179. touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
  180. touchkey->led_dev.brightness = LED_FULL;
  181. touchkey->led_dev.max_brightness = LED_ON;
  182. touchkey->led_dev.brightness_set = tm2_touchkey_led_brightness_set;
  183. error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
  184. if (error) {
  185. dev_err(&client->dev,
  186. "failed to register touchkey led: %d\n", error);
  187. return error;
  188. }
  189. return 0;
  190. }
  191. static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
  192. {
  193. struct i2c_client *client = to_i2c_client(dev);
  194. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  195. disable_irq(client->irq);
  196. tm2_touchkey_power_disable(touchkey);
  197. return 0;
  198. }
  199. static int __maybe_unused tm2_touchkey_resume(struct device *dev)
  200. {
  201. struct i2c_client *client = to_i2c_client(dev);
  202. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  203. int ret;
  204. enable_irq(client->irq);
  205. ret = tm2_touchkey_power_enable(touchkey);
  206. if (ret)
  207. dev_err(dev, "failed to enable power: %d\n", ret);
  208. return ret;
  209. }
  210. static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
  211. tm2_touchkey_suspend, tm2_touchkey_resume);
  212. static const struct i2c_device_id tm2_touchkey_id_table[] = {
  213. { TM2_TOUCHKEY_DEV_NAME, 0 },
  214. { },
  215. };
  216. MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
  217. static const struct of_device_id tm2_touchkey_of_match[] = {
  218. { .compatible = "cypress,tm2-touchkey", },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
  222. static struct i2c_driver tm2_touchkey_driver = {
  223. .driver = {
  224. .name = TM2_TOUCHKEY_DEV_NAME,
  225. .pm = &tm2_touchkey_pm_ops,
  226. .of_match_table = of_match_ptr(tm2_touchkey_of_match),
  227. },
  228. .probe = tm2_touchkey_probe,
  229. .id_table = tm2_touchkey_id_table,
  230. };
  231. module_i2c_driver(tm2_touchkey_driver);
  232. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  233. MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
  234. MODULE_DESCRIPTION("Samsung touchkey driver");
  235. MODULE_LICENSE("GPL v2");