tm2-touchkey.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/pm.h>
  22. #include <linux/regulator/consumer.h>
  23. #define TM2_TOUCHKEY_DEV_NAME "tm2-touchkey"
  24. #define ARIES_TOUCHKEY_CMD_LED_ON 0x1
  25. #define ARIES_TOUCHKEY_CMD_LED_OFF 0x2
  26. #define TM2_TOUCHKEY_CMD_LED_ON 0x10
  27. #define TM2_TOUCHKEY_CMD_LED_OFF 0x20
  28. #define TM2_TOUCHKEY_BIT_PRESS_EV BIT(3)
  29. #define TM2_TOUCHKEY_BIT_KEYCODE GENMASK(2, 0)
  30. #define TM2_TOUCHKEY_LED_VOLTAGE_MIN 2500000
  31. #define TM2_TOUCHKEY_LED_VOLTAGE_MAX 3300000
  32. struct touchkey_variant {
  33. u8 keycode_reg;
  34. u8 base_reg;
  35. u8 cmd_led_on;
  36. u8 cmd_led_off;
  37. bool no_reg;
  38. bool fixed_regulator;
  39. };
  40. struct tm2_touchkey_data {
  41. struct i2c_client *client;
  42. struct input_dev *input_dev;
  43. struct led_classdev led_dev;
  44. struct regulator *vdd;
  45. struct regulator_bulk_data regulators[3];
  46. const struct touchkey_variant *variant;
  47. u32 keycodes[4];
  48. int num_keycodes;
  49. };
  50. static const struct touchkey_variant tm2_touchkey_variant = {
  51. .keycode_reg = 0x03,
  52. .base_reg = 0x00,
  53. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  54. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  55. };
  56. static const struct touchkey_variant midas_touchkey_variant = {
  57. .keycode_reg = 0x00,
  58. .base_reg = 0x00,
  59. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  60. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  61. };
  62. static struct touchkey_variant aries_touchkey_variant = {
  63. .no_reg = true,
  64. .fixed_regulator = true,
  65. .cmd_led_on = ARIES_TOUCHKEY_CMD_LED_ON,
  66. .cmd_led_off = ARIES_TOUCHKEY_CMD_LED_OFF,
  67. };
  68. static const struct touchkey_variant tc360_touchkey_variant = {
  69. .keycode_reg = 0x00,
  70. .base_reg = 0x00,
  71. .fixed_regulator = true,
  72. .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
  73. .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
  74. };
  75. static int tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
  76. enum led_brightness brightness)
  77. {
  78. struct tm2_touchkey_data *touchkey =
  79. container_of(led_dev, struct tm2_touchkey_data, led_dev);
  80. u32 volt;
  81. u8 data;
  82. if (brightness == LED_OFF) {
  83. volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
  84. data = touchkey->variant->cmd_led_off;
  85. } else {
  86. volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
  87. data = touchkey->variant->cmd_led_on;
  88. }
  89. if (!touchkey->variant->fixed_regulator)
  90. regulator_set_voltage(touchkey->vdd, volt, volt);
  91. return touchkey->variant->no_reg ?
  92. i2c_smbus_write_byte(touchkey->client, data) :
  93. i2c_smbus_write_byte_data(touchkey->client,
  94. touchkey->variant->base_reg, data);
  95. }
  96. static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
  97. {
  98. int error;
  99. error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
  100. touchkey->regulators);
  101. if (error)
  102. return error;
  103. /* waiting for device initialization, at least 150ms */
  104. msleep(150);
  105. return 0;
  106. }
  107. static void tm2_touchkey_power_disable(void *data)
  108. {
  109. struct tm2_touchkey_data *touchkey = data;
  110. regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
  111. touchkey->regulators);
  112. }
  113. static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
  114. {
  115. struct tm2_touchkey_data *touchkey = devid;
  116. int data;
  117. int index;
  118. int i;
  119. if (touchkey->variant->no_reg)
  120. data = i2c_smbus_read_byte(touchkey->client);
  121. else
  122. data = i2c_smbus_read_byte_data(touchkey->client,
  123. touchkey->variant->keycode_reg);
  124. if (data < 0) {
  125. dev_err(&touchkey->client->dev,
  126. "failed to read i2c data: %d\n", data);
  127. goto out;
  128. }
  129. index = (data & TM2_TOUCHKEY_BIT_KEYCODE) - 1;
  130. if (index < 0 || index >= touchkey->num_keycodes) {
  131. dev_warn(&touchkey->client->dev,
  132. "invalid keycode index %d\n", index);
  133. goto out;
  134. }
  135. input_event(touchkey->input_dev, EV_MSC, MSC_SCAN, index);
  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. {
  157. struct device_node *np = client->dev.of_node;
  158. struct tm2_touchkey_data *touchkey;
  159. int error;
  160. int i;
  161. if (!i2c_check_functionality(client->adapter,
  162. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
  163. dev_err(&client->dev, "incompatible I2C adapter\n");
  164. return -EIO;
  165. }
  166. touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
  167. if (!touchkey)
  168. return -ENOMEM;
  169. touchkey->client = client;
  170. i2c_set_clientdata(client, touchkey);
  171. touchkey->variant = of_device_get_match_data(&client->dev);
  172. touchkey->regulators[0].supply = "vcc";
  173. touchkey->regulators[1].supply = "vdd";
  174. touchkey->regulators[2].supply = "vddio";
  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. touchkey->input_dev->keycode = touchkey->keycodes;
  214. touchkey->input_dev->keycodemax = touchkey->num_keycodes;
  215. touchkey->input_dev->keycodesize = sizeof(touchkey->keycodes[0]);
  216. input_set_capability(touchkey->input_dev, EV_MSC, MSC_SCAN);
  217. for (i = 0; i < touchkey->num_keycodes; i++)
  218. input_set_capability(touchkey->input_dev, EV_KEY,
  219. touchkey->keycodes[i]);
  220. error = input_register_device(touchkey->input_dev);
  221. if (error) {
  222. dev_err(&client->dev,
  223. "failed to register input device: %d\n", error);
  224. return error;
  225. }
  226. error = devm_request_threaded_irq(&client->dev, client->irq,
  227. NULL, tm2_touchkey_irq_handler,
  228. IRQF_ONESHOT,
  229. TM2_TOUCHKEY_DEV_NAME, touchkey);
  230. if (error) {
  231. dev_err(&client->dev,
  232. "failed to request threaded irq: %d\n", error);
  233. return error;
  234. }
  235. /* led device */
  236. touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
  237. touchkey->led_dev.brightness = LED_ON;
  238. touchkey->led_dev.max_brightness = LED_ON;
  239. touchkey->led_dev.brightness_set_blocking =
  240. tm2_touchkey_led_brightness_set;
  241. error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
  242. if (error) {
  243. dev_err(&client->dev,
  244. "failed to register touchkey led: %d\n", error);
  245. return error;
  246. }
  247. if (touchkey->variant->fixed_regulator)
  248. tm2_touchkey_led_brightness_set(&touchkey->led_dev, LED_ON);
  249. return 0;
  250. }
  251. static int tm2_touchkey_suspend(struct device *dev)
  252. {
  253. struct i2c_client *client = to_i2c_client(dev);
  254. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  255. disable_irq(client->irq);
  256. tm2_touchkey_power_disable(touchkey);
  257. return 0;
  258. }
  259. static int tm2_touchkey_resume(struct device *dev)
  260. {
  261. struct i2c_client *client = to_i2c_client(dev);
  262. struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
  263. int ret;
  264. enable_irq(client->irq);
  265. ret = tm2_touchkey_power_enable(touchkey);
  266. if (ret)
  267. dev_err(dev, "failed to enable power: %d\n", ret);
  268. return ret;
  269. }
  270. static DEFINE_SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
  271. tm2_touchkey_suspend, tm2_touchkey_resume);
  272. static const struct i2c_device_id tm2_touchkey_id_table[] = {
  273. { TM2_TOUCHKEY_DEV_NAME },
  274. { }
  275. };
  276. MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
  277. static const struct of_device_id tm2_touchkey_of_match[] = {
  278. {
  279. .compatible = "cypress,tm2-touchkey",
  280. .data = &tm2_touchkey_variant,
  281. }, {
  282. .compatible = "cypress,midas-touchkey",
  283. .data = &midas_touchkey_variant,
  284. }, {
  285. .compatible = "cypress,aries-touchkey",
  286. .data = &aries_touchkey_variant,
  287. }, {
  288. .compatible = "coreriver,tc360-touchkey",
  289. .data = &tc360_touchkey_variant,
  290. },
  291. { },
  292. };
  293. MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
  294. static struct i2c_driver tm2_touchkey_driver = {
  295. .driver = {
  296. .name = TM2_TOUCHKEY_DEV_NAME,
  297. .pm = pm_sleep_ptr(&tm2_touchkey_pm_ops),
  298. .of_match_table = tm2_touchkey_of_match,
  299. },
  300. .probe = tm2_touchkey_probe,
  301. .id_table = tm2_touchkey_id_table,
  302. };
  303. module_i2c_driver(tm2_touchkey_driver);
  304. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  305. MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
  306. MODULE_DESCRIPTION("Samsung touchkey driver");
  307. MODULE_LICENSE("GPL v2");