tm2-touchkey.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <[email protected]>
  9. * Author: Jaechul Lee <[email protected]>
  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[3];
  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. input_event(touchkey->input_dev, EV_MSC, MSC_SCAN, index);
  137. if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
  138. for (i = 0; i < touchkey->num_keycodes; i++)
  139. input_report_key(touchkey->input_dev,
  140. touchkey->keycodes[i], 0);
  141. } else {
  142. input_report_key(touchkey->input_dev,
  143. touchkey->keycodes[index], 1);
  144. }
  145. input_sync(touchkey->input_dev);
  146. out:
  147. if (touchkey->variant->fixed_regulator &&
  148. data & TM2_TOUCHKEY_BIT_PRESS_EV) {
  149. /* touch turns backlight on, so make sure we're in sync */
  150. if (touchkey->led_dev.brightness == LED_OFF)
  151. tm2_touchkey_led_brightness_set(&touchkey->led_dev,
  152. LED_OFF);
  153. }
  154. return IRQ_HANDLED;
  155. }
  156. static int tm2_touchkey_probe(struct i2c_client *client,
  157. const struct i2c_device_id *id)
  158. {
  159. struct device_node *np = client->dev.of_node;
  160. struct tm2_touchkey_data *touchkey;
  161. int error;
  162. int i;
  163. if (!i2c_check_functionality(client->adapter,
  164. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
  165. dev_err(&client->dev, "incompatible I2C adapter\n");
  166. return -EIO;
  167. }
  168. touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
  169. if (!touchkey)
  170. return -ENOMEM;
  171. touchkey->client = client;
  172. i2c_set_clientdata(client, touchkey);
  173. touchkey->variant = of_device_get_match_data(&client->dev);
  174. touchkey->regulators[0].supply = "vcc";
  175. touchkey->regulators[1].supply = "vdd";
  176. touchkey->regulators[2].supply = "vddio";
  177. error = devm_regulator_bulk_get(&client->dev,
  178. ARRAY_SIZE(touchkey->regulators),
  179. touchkey->regulators);
  180. if (error) {
  181. dev_err(&client->dev, "failed to get regulators: %d\n", error);
  182. return error;
  183. }
  184. /* Save VDD for easy access */
  185. touchkey->vdd = touchkey->regulators[1].consumer;
  186. touchkey->num_keycodes = of_property_read_variable_u32_array(np,
  187. "linux,keycodes", touchkey->keycodes, 0,
  188. ARRAY_SIZE(touchkey->keycodes));
  189. if (touchkey->num_keycodes <= 0) {
  190. /* default keycodes */
  191. touchkey->keycodes[0] = KEY_PHONE;
  192. touchkey->keycodes[1] = KEY_BACK;
  193. touchkey->num_keycodes = 2;
  194. }
  195. error = tm2_touchkey_power_enable(touchkey);
  196. if (error) {
  197. dev_err(&client->dev, "failed to power up device: %d\n", error);
  198. return error;
  199. }
  200. error = devm_add_action_or_reset(&client->dev,
  201. tm2_touchkey_power_disable, touchkey);
  202. if (error) {
  203. dev_err(&client->dev,
  204. "failed to install poweroff handler: %d\n", error);
  205. return error;
  206. }
  207. /* input device */
  208. touchkey->input_dev = devm_input_allocate_device(&client->dev);
  209. if (!touchkey->input_dev) {
  210. dev_err(&client->dev, "failed to allocate input device\n");
  211. return -ENOMEM;
  212. }
  213. touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
  214. touchkey->input_dev->id.bustype = BUS_I2C;
  215. touchkey->input_dev->keycode = touchkey->keycodes;
  216. touchkey->input_dev->keycodemax = touchkey->num_keycodes;
  217. touchkey->input_dev->keycodesize = sizeof(touchkey->keycodes[0]);
  218. input_set_capability(touchkey->input_dev, EV_MSC, MSC_SCAN);
  219. for (i = 0; i < touchkey->num_keycodes; i++)
  220. input_set_capability(touchkey->input_dev, EV_KEY,
  221. touchkey->keycodes[i]);
  222. error = input_register_device(touchkey->input_dev);
  223. if (error) {
  224. dev_err(&client->dev,
  225. "failed to register input device: %d\n", error);
  226. return error;
  227. }
  228. error = devm_request_threaded_irq(&client->dev, client->irq,
  229. NULL, tm2_touchkey_irq_handler,
  230. IRQF_ONESHOT,
  231. TM2_TOUCHKEY_DEV_NAME, touchkey);
  232. if (error) {
  233. dev_err(&client->dev,
  234. "failed to request threaded irq: %d\n", error);
  235. return error;
  236. }
  237. /* led device */
  238. touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
  239. touchkey->led_dev.brightness = LED_ON;
  240. touchkey->led_dev.max_brightness = LED_ON;
  241. touchkey->led_dev.brightness_set_blocking =
  242. tm2_touchkey_led_brightness_set;
  243. error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
  244. if (error) {
  245. dev_err(&client->dev,
  246. "failed to register touchkey led: %d\n", error);
  247. return error;
  248. }
  249. if (touchkey->variant->fixed_regulator)
  250. tm2_touchkey_led_brightness_set(&touchkey->led_dev, LED_ON);
  251. return 0;
  252. }
  253. static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
  254. {
  255. struct i2c_client *client = to_i2c_client(dev);
  256. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  257. disable_irq(client->irq);
  258. tm2_touchkey_power_disable(touchkey);
  259. return 0;
  260. }
  261. static int __maybe_unused tm2_touchkey_resume(struct device *dev)
  262. {
  263. struct i2c_client *client = to_i2c_client(dev);
  264. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  265. int ret;
  266. enable_irq(client->irq);
  267. ret = tm2_touchkey_power_enable(touchkey);
  268. if (ret)
  269. dev_err(dev, "failed to enable power: %d\n", ret);
  270. return ret;
  271. }
  272. static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
  273. tm2_touchkey_suspend, tm2_touchkey_resume);
  274. static const struct i2c_device_id tm2_touchkey_id_table[] = {
  275. { TM2_TOUCHKEY_DEV_NAME, 0 },
  276. { },
  277. };
  278. MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
  279. static const struct of_device_id tm2_touchkey_of_match[] = {
  280. {
  281. .compatible = "cypress,tm2-touchkey",
  282. .data = &tm2_touchkey_variant,
  283. }, {
  284. .compatible = "cypress,midas-touchkey",
  285. .data = &midas_touchkey_variant,
  286. }, {
  287. .compatible = "cypress,aries-touchkey",
  288. .data = &aries_touchkey_variant,
  289. }, {
  290. .compatible = "coreriver,tc360-touchkey",
  291. .data = &tc360_touchkey_variant,
  292. },
  293. { },
  294. };
  295. MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
  296. static struct i2c_driver tm2_touchkey_driver = {
  297. .driver = {
  298. .name = TM2_TOUCHKEY_DEV_NAME,
  299. .pm = &tm2_touchkey_pm_ops,
  300. .of_match_table = of_match_ptr(tm2_touchkey_of_match),
  301. },
  302. .probe = tm2_touchkey_probe,
  303. .id_table = tm2_touchkey_id_table,
  304. };
  305. module_i2c_driver(tm2_touchkey_driver);
  306. MODULE_AUTHOR("Beomho Seo <[email protected]>");
  307. MODULE_AUTHOR("Jaechul Lee <[email protected]>");
  308. MODULE_DESCRIPTION("Samsung touchkey driver");
  309. MODULE_LICENSE("GPL v2");