tm2-touchkey.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TM2 touchkey device driver
  4. *
  5. * Copyright 2005 Phil Blundell
  6. * Copyright 2016 Samsung Electronics Co., Ltd.
  7. *
  8. * Author: Beomho Seo <beomho.seo@samsung.com>
  9. * Author: Jaechul Lee <jcsing.lee@samsung.com>
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/leds.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/pm.h>
  23. #include <linux/regulator/consumer.h>
  24. #define TM2_TOUCHKEY_DEV_NAME "tm2-touchkey"
  25. #define ARIES_TOUCHKEY_CMD_LED_ON 0x1
  26. #define ARIES_TOUCHKEY_CMD_LED_OFF 0x2
  27. #define TM2_TOUCHKEY_CMD_LED_ON 0x10
  28. #define TM2_TOUCHKEY_CMD_LED_OFF 0x20
  29. #define TM2_TOUCHKEY_BIT_PRESS_EV BIT(3)
  30. #define TM2_TOUCHKEY_BIT_KEYCODE GENMASK(2, 0)
  31. #define TM2_TOUCHKEY_LED_VOLTAGE_MIN 2500000
  32. #define TM2_TOUCHKEY_LED_VOLTAGE_MAX 3300000
  33. struct touchkey_variant {
  34. u8 keycode_reg;
  35. u8 base_reg;
  36. u8 cmd_led_on;
  37. u8 cmd_led_off;
  38. bool no_reg;
  39. bool fixed_regulator;
  40. };
  41. struct tm2_touchkey_data {
  42. struct i2c_client *client;
  43. struct input_dev *input_dev;
  44. struct led_classdev led_dev;
  45. struct regulator *vdd;
  46. struct regulator_bulk_data regulators[2];
  47. const struct touchkey_variant *variant;
  48. u32 keycodes[4];
  49. int num_keycodes;
  50. };
  51. static const struct touchkey_variant tm2_touchkey_variant = {
  52. .keycode_reg = 0x03,
  53. .base_reg = 0x00,
  54. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  55. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  56. };
  57. static const struct touchkey_variant midas_touchkey_variant = {
  58. .keycode_reg = 0x00,
  59. .base_reg = 0x00,
  60. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  61. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  62. };
  63. static struct touchkey_variant aries_touchkey_variant = {
  64. .no_reg = true,
  65. .fixed_regulator = true,
  66. .cmd_led_on = ARIES_TOUCHKEY_CMD_LED_ON,
  67. .cmd_led_off = ARIES_TOUCHKEY_CMD_LED_OFF,
  68. };
  69. static const struct touchkey_variant tc360_touchkey_variant = {
  70. .keycode_reg = 0x00,
  71. .base_reg = 0x00,
  72. .fixed_regulator = true,
  73. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  74. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  75. };
  76. static int tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
  77. enum led_brightness brightness)
  78. {
  79. struct tm2_touchkey_data *touchkey =
  80. container_of(led_dev, struct tm2_touchkey_data, led_dev);
  81. u32 volt;
  82. u8 data;
  83. if (brightness == LED_OFF) {
  84. volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
  85. data = touchkey->variant->cmd_led_off;
  86. } else {
  87. volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
  88. data = touchkey->variant->cmd_led_on;
  89. }
  90. if (!touchkey->variant->fixed_regulator)
  91. regulator_set_voltage(touchkey->vdd, volt, volt);
  92. return touchkey->variant->no_reg ?
  93. i2c_smbus_write_byte(touchkey->client, data) :
  94. i2c_smbus_write_byte_data(touchkey->client,
  95. touchkey->variant->base_reg, data);
  96. }
  97. static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
  98. {
  99. int error;
  100. error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
  101. touchkey->regulators);
  102. if (error)
  103. return error;
  104. /* waiting for device initialization, at least 150ms */
  105. msleep(150);
  106. return 0;
  107. }
  108. static void tm2_touchkey_power_disable(void *data)
  109. {
  110. struct tm2_touchkey_data *touchkey = data;
  111. regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
  112. touchkey->regulators);
  113. }
  114. static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
  115. {
  116. struct tm2_touchkey_data *touchkey = devid;
  117. int data;
  118. int index;
  119. int i;
  120. if (touchkey->variant->no_reg)
  121. data = i2c_smbus_read_byte(touchkey->client);
  122. else
  123. data = i2c_smbus_read_byte_data(touchkey->client,
  124. touchkey->variant->keycode_reg);
  125. if (data < 0) {
  126. dev_err(&touchkey->client->dev,
  127. "failed to read i2c data: %d\n", data);
  128. goto out;
  129. }
  130. index = (data & TM2_TOUCHKEY_BIT_KEYCODE) - 1;
  131. if (index < 0 || index >= touchkey->num_keycodes) {
  132. dev_warn(&touchkey->client->dev,
  133. "invalid keycode index %d\n", index);
  134. goto out;
  135. }
  136. if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
  137. for (i = 0; i < touchkey->num_keycodes; i++)
  138. input_report_key(touchkey->input_dev,
  139. touchkey->keycodes[i], 0);
  140. } else {
  141. input_report_key(touchkey->input_dev,
  142. touchkey->keycodes[index], 1);
  143. }
  144. input_sync(touchkey->input_dev);
  145. out:
  146. if (touchkey->variant->fixed_regulator &&
  147. data & TM2_TOUCHKEY_BIT_PRESS_EV) {
  148. /* touch turns backlight on, so make sure we're in sync */
  149. if (touchkey->led_dev.brightness == LED_OFF)
  150. tm2_touchkey_led_brightness_set(&touchkey->led_dev,
  151. LED_OFF);
  152. }
  153. return IRQ_HANDLED;
  154. }
  155. static int tm2_touchkey_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct device_node *np = client->dev.of_node;
  159. struct tm2_touchkey_data *touchkey;
  160. int error;
  161. int i;
  162. if (!i2c_check_functionality(client->adapter,
  163. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
  164. dev_err(&client->dev, "incompatible I2C adapter\n");
  165. return -EIO;
  166. }
  167. touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
  168. if (!touchkey)
  169. return -ENOMEM;
  170. touchkey->client = client;
  171. i2c_set_clientdata(client, touchkey);
  172. touchkey->variant = of_device_get_match_data(&client->dev);
  173. touchkey->regulators[0].supply = "vcc";
  174. touchkey->regulators[1].supply = "vdd";
  175. error = devm_regulator_bulk_get(&client->dev,
  176. ARRAY_SIZE(touchkey->regulators),
  177. touchkey->regulators);
  178. if (error) {
  179. dev_err(&client->dev, "failed to get regulators: %d\n", error);
  180. return error;
  181. }
  182. /* Save VDD for easy access */
  183. touchkey->vdd = touchkey->regulators[1].consumer;
  184. touchkey->num_keycodes = of_property_read_variable_u32_array(np,
  185. "linux,keycodes", touchkey->keycodes, 0,
  186. ARRAY_SIZE(touchkey->keycodes));
  187. if (touchkey->num_keycodes <= 0) {
  188. /* default keycodes */
  189. touchkey->keycodes[0] = KEY_PHONE;
  190. touchkey->keycodes[1] = KEY_BACK;
  191. touchkey->num_keycodes = 2;
  192. }
  193. error = tm2_touchkey_power_enable(touchkey);
  194. if (error) {
  195. dev_err(&client->dev, "failed to power up device: %d\n", error);
  196. return error;
  197. }
  198. error = devm_add_action_or_reset(&client->dev,
  199. tm2_touchkey_power_disable, touchkey);
  200. if (error) {
  201. dev_err(&client->dev,
  202. "failed to install poweroff handler: %d\n", error);
  203. return error;
  204. }
  205. /* input device */
  206. touchkey->input_dev = devm_input_allocate_device(&client->dev);
  207. if (!touchkey->input_dev) {
  208. dev_err(&client->dev, "failed to allocate input device\n");
  209. return -ENOMEM;
  210. }
  211. touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
  212. touchkey->input_dev->id.bustype = BUS_I2C;
  213. for (i = 0; i < touchkey->num_keycodes; i++)
  214. input_set_capability(touchkey->input_dev, EV_KEY,
  215. touchkey->keycodes[i]);
  216. error = input_register_device(touchkey->input_dev);
  217. if (error) {
  218. dev_err(&client->dev,
  219. "failed to register input device: %d\n", error);
  220. return error;
  221. }
  222. error = devm_request_threaded_irq(&client->dev, client->irq,
  223. NULL, tm2_touchkey_irq_handler,
  224. IRQF_ONESHOT,
  225. TM2_TOUCHKEY_DEV_NAME, touchkey);
  226. if (error) {
  227. dev_err(&client->dev,
  228. "failed to request threaded irq: %d\n", error);
  229. return error;
  230. }
  231. /* led device */
  232. touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
  233. touchkey->led_dev.brightness = LED_ON;
  234. touchkey->led_dev.max_brightness = LED_ON;
  235. touchkey->led_dev.brightness_set_blocking =
  236. tm2_touchkey_led_brightness_set;
  237. error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
  238. if (error) {
  239. dev_err(&client->dev,
  240. "failed to register touchkey led: %d\n", error);
  241. return error;
  242. }
  243. if (touchkey->variant->fixed_regulator)
  244. tm2_touchkey_led_brightness_set(&touchkey->led_dev, LED_ON);
  245. return 0;
  246. }
  247. static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
  248. {
  249. struct i2c_client *client = to_i2c_client(dev);
  250. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  251. disable_irq(client->irq);
  252. tm2_touchkey_power_disable(touchkey);
  253. return 0;
  254. }
  255. static int __maybe_unused tm2_touchkey_resume(struct device *dev)
  256. {
  257. struct i2c_client *client = to_i2c_client(dev);
  258. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  259. int ret;
  260. enable_irq(client->irq);
  261. ret = tm2_touchkey_power_enable(touchkey);
  262. if (ret)
  263. dev_err(dev, "failed to enable power: %d\n", ret);
  264. return ret;
  265. }
  266. static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
  267. tm2_touchkey_suspend, tm2_touchkey_resume);
  268. static const struct i2c_device_id tm2_touchkey_id_table[] = {
  269. { TM2_TOUCHKEY_DEV_NAME, 0 },
  270. { },
  271. };
  272. MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
  273. static const struct of_device_id tm2_touchkey_of_match[] = {
  274. {
  275. .compatible = "cypress,tm2-touchkey",
  276. .data = &tm2_touchkey_variant,
  277. }, {
  278. .compatible = "cypress,midas-touchkey",
  279. .data = &midas_touchkey_variant,
  280. }, {
  281. .compatible = "cypress,aries-touchkey",
  282. .data = &aries_touchkey_variant,
  283. }, {
  284. .compatible = "coreriver,tc360-touchkey",
  285. .data = &tc360_touchkey_variant,
  286. },
  287. { },
  288. };
  289. MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
  290. static struct i2c_driver tm2_touchkey_driver = {
  291. .driver = {
  292. .name = TM2_TOUCHKEY_DEV_NAME,
  293. .pm = &tm2_touchkey_pm_ops,
  294. .of_match_table = of_match_ptr(tm2_touchkey_of_match),
  295. },
  296. .probe = tm2_touchkey_probe,
  297. .id_table = tm2_touchkey_id_table,
  298. };
  299. module_i2c_driver(tm2_touchkey_driver);
  300. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  301. MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
  302. MODULE_DESCRIPTION("Samsung touchkey driver");
  303. MODULE_LICENSE("GPL v2");