hycon-hy46xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021
  4. * Author(s): Giulio Benetti <[email protected]>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/i2c.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/input.h>
  11. #include <linux/input/mt.h>
  12. #include <linux/input/touchscreen.h>
  13. #include <linux/irq.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/regmap.h>
  16. #include <asm/unaligned.h>
  17. #define HY46XX_CHKSUM_CODE 0x1
  18. #define HY46XX_FINGER_NUM 0x2
  19. #define HY46XX_CHKSUM_LEN 0x7
  20. #define HY46XX_THRESHOLD 0x80
  21. #define HY46XX_GLOVE_EN 0x84
  22. #define HY46XX_REPORT_SPEED 0x88
  23. #define HY46XX_PWR_NOISE_EN 0x89
  24. #define HY46XX_FILTER_DATA 0x8A
  25. #define HY46XX_GAIN 0x92
  26. #define HY46XX_EDGE_OFFSET 0x93
  27. #define HY46XX_RX_NR_USED 0x94
  28. #define HY46XX_TX_NR_USED 0x95
  29. #define HY46XX_PWR_MODE 0xA5
  30. #define HY46XX_FW_VERSION 0xA6
  31. #define HY46XX_LIB_VERSION 0xA7
  32. #define HY46XX_TP_INFO 0xA8
  33. #define HY46XX_TP_CHIP_ID 0xA9
  34. #define HY46XX_BOOT_VER 0xB0
  35. #define HY46XX_TPLEN 0x6
  36. #define HY46XX_REPORT_PKT_LEN 0x44
  37. #define HY46XX_MAX_SUPPORTED_POINTS 11
  38. #define TOUCH_EVENT_DOWN 0x00
  39. #define TOUCH_EVENT_UP 0x01
  40. #define TOUCH_EVENT_CONTACT 0x02
  41. #define TOUCH_EVENT_RESERVED 0x03
  42. struct hycon_hy46xx_data {
  43. struct i2c_client *client;
  44. struct input_dev *input;
  45. struct touchscreen_properties prop;
  46. struct regulator *vcc;
  47. struct gpio_desc *reset_gpio;
  48. struct mutex mutex;
  49. struct regmap *regmap;
  50. int threshold;
  51. bool glove_enable;
  52. int report_speed;
  53. bool noise_filter_enable;
  54. int filter_data;
  55. int gain;
  56. int edge_offset;
  57. int rx_number_used;
  58. int tx_number_used;
  59. int power_mode;
  60. int fw_version;
  61. int lib_version;
  62. int tp_information;
  63. int tp_chip_id;
  64. int bootloader_version;
  65. };
  66. static const struct regmap_config hycon_hy46xx_i2c_regmap_config = {
  67. .reg_bits = 8,
  68. .val_bits = 8,
  69. };
  70. static bool hycon_hy46xx_check_checksum(struct hycon_hy46xx_data *tsdata, u8 *buf)
  71. {
  72. u8 chksum = 0;
  73. int i;
  74. for (i = 2; i < buf[HY46XX_CHKSUM_LEN]; i++)
  75. chksum += buf[i];
  76. if (chksum == buf[HY46XX_CHKSUM_CODE])
  77. return true;
  78. dev_err_ratelimited(&tsdata->client->dev,
  79. "checksum error: 0x%02x expected, got 0x%02x\n",
  80. chksum, buf[HY46XX_CHKSUM_CODE]);
  81. return false;
  82. }
  83. static irqreturn_t hycon_hy46xx_isr(int irq, void *dev_id)
  84. {
  85. struct hycon_hy46xx_data *tsdata = dev_id;
  86. struct device *dev = &tsdata->client->dev;
  87. u8 rdbuf[HY46XX_REPORT_PKT_LEN];
  88. int i, x, y, id;
  89. int error;
  90. memset(rdbuf, 0, sizeof(rdbuf));
  91. error = regmap_bulk_read(tsdata->regmap, 0, rdbuf, sizeof(rdbuf));
  92. if (error) {
  93. dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
  94. error);
  95. goto out;
  96. }
  97. if (!hycon_hy46xx_check_checksum(tsdata, rdbuf))
  98. goto out;
  99. for (i = 0; i < HY46XX_MAX_SUPPORTED_POINTS; i++) {
  100. u8 *buf = &rdbuf[3 + (HY46XX_TPLEN * i)];
  101. int type = buf[0] >> 6;
  102. if (type == TOUCH_EVENT_RESERVED)
  103. continue;
  104. x = get_unaligned_be16(buf) & 0x0fff;
  105. y = get_unaligned_be16(buf + 2) & 0x0fff;
  106. id = buf[2] >> 4;
  107. input_mt_slot(tsdata->input, id);
  108. if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
  109. type != TOUCH_EVENT_UP))
  110. touchscreen_report_pos(tsdata->input, &tsdata->prop,
  111. x, y, true);
  112. }
  113. input_mt_report_pointer_emulation(tsdata->input, false);
  114. input_sync(tsdata->input);
  115. out:
  116. return IRQ_HANDLED;
  117. }
  118. struct hycon_hy46xx_attribute {
  119. struct device_attribute dattr;
  120. size_t field_offset;
  121. u8 address;
  122. u8 limit_low;
  123. u8 limit_high;
  124. };
  125. #define HYCON_ATTR_U8(_field, _mode, _address, _limit_low, _limit_high) \
  126. struct hycon_hy46xx_attribute hycon_hy46xx_attr_##_field = { \
  127. .dattr = __ATTR(_field, _mode, \
  128. hycon_hy46xx_setting_show, \
  129. hycon_hy46xx_setting_store), \
  130. .field_offset = offsetof(struct hycon_hy46xx_data, _field), \
  131. .address = _address, \
  132. .limit_low = _limit_low, \
  133. .limit_high = _limit_high, \
  134. }
  135. #define HYCON_ATTR_BOOL(_field, _mode, _address) \
  136. struct hycon_hy46xx_attribute hycon_hy46xx_attr_##_field = { \
  137. .dattr = __ATTR(_field, _mode, \
  138. hycon_hy46xx_setting_show, \
  139. hycon_hy46xx_setting_store), \
  140. .field_offset = offsetof(struct hycon_hy46xx_data, _field), \
  141. .address = _address, \
  142. .limit_low = false, \
  143. .limit_high = true, \
  144. }
  145. static ssize_t hycon_hy46xx_setting_show(struct device *dev,
  146. struct device_attribute *dattr, char *buf)
  147. {
  148. struct i2c_client *client = to_i2c_client(dev);
  149. struct hycon_hy46xx_data *tsdata = i2c_get_clientdata(client);
  150. struct hycon_hy46xx_attribute *attr =
  151. container_of(dattr, struct hycon_hy46xx_attribute, dattr);
  152. u8 *field = (u8 *)tsdata + attr->field_offset;
  153. size_t count = 0;
  154. int error = 0;
  155. int val;
  156. mutex_lock(&tsdata->mutex);
  157. error = regmap_read(tsdata->regmap, attr->address, &val);
  158. if (error < 0) {
  159. dev_err(&tsdata->client->dev,
  160. "Failed to fetch attribute %s, error %d\n",
  161. dattr->attr.name, error);
  162. goto out;
  163. }
  164. if (val != *field) {
  165. dev_warn(&tsdata->client->dev,
  166. "%s: read (%d) and stored value (%d) differ\n",
  167. dattr->attr.name, val, *field);
  168. *field = val;
  169. }
  170. count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
  171. out:
  172. mutex_unlock(&tsdata->mutex);
  173. return error ?: count;
  174. }
  175. static ssize_t hycon_hy46xx_setting_store(struct device *dev,
  176. struct device_attribute *dattr,
  177. const char *buf, size_t count)
  178. {
  179. struct i2c_client *client = to_i2c_client(dev);
  180. struct hycon_hy46xx_data *tsdata = i2c_get_clientdata(client);
  181. struct hycon_hy46xx_attribute *attr =
  182. container_of(dattr, struct hycon_hy46xx_attribute, dattr);
  183. u8 *field = (u8 *)tsdata + attr->field_offset;
  184. unsigned int val;
  185. int error;
  186. mutex_lock(&tsdata->mutex);
  187. error = kstrtouint(buf, 0, &val);
  188. if (error)
  189. goto out;
  190. if (val < attr->limit_low || val > attr->limit_high) {
  191. error = -ERANGE;
  192. goto out;
  193. }
  194. error = regmap_write(tsdata->regmap, attr->address, val);
  195. if (error < 0) {
  196. dev_err(&tsdata->client->dev,
  197. "Failed to update attribute %s, error: %d\n",
  198. dattr->attr.name, error);
  199. goto out;
  200. }
  201. *field = val;
  202. out:
  203. mutex_unlock(&tsdata->mutex);
  204. return error ?: count;
  205. }
  206. static HYCON_ATTR_U8(threshold, 0644, HY46XX_THRESHOLD, 0, 255);
  207. static HYCON_ATTR_BOOL(glove_enable, 0644, HY46XX_GLOVE_EN);
  208. static HYCON_ATTR_U8(report_speed, 0644, HY46XX_REPORT_SPEED, 0, 255);
  209. static HYCON_ATTR_BOOL(noise_filter_enable, 0644, HY46XX_PWR_NOISE_EN);
  210. static HYCON_ATTR_U8(filter_data, 0644, HY46XX_FILTER_DATA, 0, 5);
  211. static HYCON_ATTR_U8(gain, 0644, HY46XX_GAIN, 0, 5);
  212. static HYCON_ATTR_U8(edge_offset, 0644, HY46XX_EDGE_OFFSET, 0, 5);
  213. static HYCON_ATTR_U8(fw_version, 0444, HY46XX_FW_VERSION, 0, 255);
  214. static HYCON_ATTR_U8(lib_version, 0444, HY46XX_LIB_VERSION, 0, 255);
  215. static HYCON_ATTR_U8(tp_information, 0444, HY46XX_TP_INFO, 0, 255);
  216. static HYCON_ATTR_U8(tp_chip_id, 0444, HY46XX_TP_CHIP_ID, 0, 255);
  217. static HYCON_ATTR_U8(bootloader_version, 0444, HY46XX_BOOT_VER, 0, 255);
  218. static struct attribute *hycon_hy46xx_attrs[] = {
  219. &hycon_hy46xx_attr_threshold.dattr.attr,
  220. &hycon_hy46xx_attr_glove_enable.dattr.attr,
  221. &hycon_hy46xx_attr_report_speed.dattr.attr,
  222. &hycon_hy46xx_attr_noise_filter_enable.dattr.attr,
  223. &hycon_hy46xx_attr_filter_data.dattr.attr,
  224. &hycon_hy46xx_attr_gain.dattr.attr,
  225. &hycon_hy46xx_attr_edge_offset.dattr.attr,
  226. &hycon_hy46xx_attr_fw_version.dattr.attr,
  227. &hycon_hy46xx_attr_lib_version.dattr.attr,
  228. &hycon_hy46xx_attr_tp_information.dattr.attr,
  229. &hycon_hy46xx_attr_tp_chip_id.dattr.attr,
  230. &hycon_hy46xx_attr_bootloader_version.dattr.attr,
  231. NULL
  232. };
  233. static const struct attribute_group hycon_hy46xx_attr_group = {
  234. .attrs = hycon_hy46xx_attrs,
  235. };
  236. static void hycon_hy46xx_get_defaults(struct device *dev, struct hycon_hy46xx_data *tsdata)
  237. {
  238. bool val_bool;
  239. int error;
  240. u32 val;
  241. error = device_property_read_u32(dev, "hycon,threshold", &val);
  242. if (!error) {
  243. error = regmap_write(tsdata->regmap, HY46XX_THRESHOLD, val);
  244. if (error < 0)
  245. goto out;
  246. tsdata->threshold = val;
  247. }
  248. val_bool = device_property_read_bool(dev, "hycon,glove-enable");
  249. error = regmap_write(tsdata->regmap, HY46XX_GLOVE_EN, val_bool);
  250. if (error < 0)
  251. goto out;
  252. tsdata->glove_enable = val_bool;
  253. error = device_property_read_u32(dev, "hycon,report-speed-hz", &val);
  254. if (!error) {
  255. error = regmap_write(tsdata->regmap, HY46XX_REPORT_SPEED, val);
  256. if (error < 0)
  257. goto out;
  258. tsdata->report_speed = val;
  259. }
  260. val_bool = device_property_read_bool(dev, "hycon,noise-filter-enable");
  261. error = regmap_write(tsdata->regmap, HY46XX_PWR_NOISE_EN, val_bool);
  262. if (error < 0)
  263. goto out;
  264. tsdata->noise_filter_enable = val_bool;
  265. error = device_property_read_u32(dev, "hycon,filter-data", &val);
  266. if (!error) {
  267. error = regmap_write(tsdata->regmap, HY46XX_FILTER_DATA, val);
  268. if (error < 0)
  269. goto out;
  270. tsdata->filter_data = val;
  271. }
  272. error = device_property_read_u32(dev, "hycon,gain", &val);
  273. if (!error) {
  274. error = regmap_write(tsdata->regmap, HY46XX_GAIN, val);
  275. if (error < 0)
  276. goto out;
  277. tsdata->gain = val;
  278. }
  279. error = device_property_read_u32(dev, "hycon,edge-offset", &val);
  280. if (!error) {
  281. error = regmap_write(tsdata->regmap, HY46XX_EDGE_OFFSET, val);
  282. if (error < 0)
  283. goto out;
  284. tsdata->edge_offset = val;
  285. }
  286. return;
  287. out:
  288. dev_err(&tsdata->client->dev, "Failed to set default settings");
  289. }
  290. static void hycon_hy46xx_get_parameters(struct hycon_hy46xx_data *tsdata)
  291. {
  292. int error;
  293. u32 val;
  294. error = regmap_read(tsdata->regmap, HY46XX_THRESHOLD, &val);
  295. if (error < 0)
  296. goto out;
  297. tsdata->threshold = val;
  298. error = regmap_read(tsdata->regmap, HY46XX_GLOVE_EN, &val);
  299. if (error < 0)
  300. goto out;
  301. tsdata->glove_enable = val;
  302. error = regmap_read(tsdata->regmap, HY46XX_REPORT_SPEED, &val);
  303. if (error < 0)
  304. goto out;
  305. tsdata->report_speed = val;
  306. error = regmap_read(tsdata->regmap, HY46XX_PWR_NOISE_EN, &val);
  307. if (error < 0)
  308. goto out;
  309. tsdata->noise_filter_enable = val;
  310. error = regmap_read(tsdata->regmap, HY46XX_FILTER_DATA, &val);
  311. if (error < 0)
  312. goto out;
  313. tsdata->filter_data = val;
  314. error = regmap_read(tsdata->regmap, HY46XX_GAIN, &val);
  315. if (error < 0)
  316. goto out;
  317. tsdata->gain = val;
  318. error = regmap_read(tsdata->regmap, HY46XX_EDGE_OFFSET, &val);
  319. if (error < 0)
  320. goto out;
  321. tsdata->edge_offset = val;
  322. error = regmap_read(tsdata->regmap, HY46XX_RX_NR_USED, &val);
  323. if (error < 0)
  324. goto out;
  325. tsdata->rx_number_used = val;
  326. error = regmap_read(tsdata->regmap, HY46XX_TX_NR_USED, &val);
  327. if (error < 0)
  328. goto out;
  329. tsdata->tx_number_used = val;
  330. error = regmap_read(tsdata->regmap, HY46XX_PWR_MODE, &val);
  331. if (error < 0)
  332. goto out;
  333. tsdata->power_mode = val;
  334. error = regmap_read(tsdata->regmap, HY46XX_FW_VERSION, &val);
  335. if (error < 0)
  336. goto out;
  337. tsdata->fw_version = val;
  338. error = regmap_read(tsdata->regmap, HY46XX_LIB_VERSION, &val);
  339. if (error < 0)
  340. goto out;
  341. tsdata->lib_version = val;
  342. error = regmap_read(tsdata->regmap, HY46XX_TP_INFO, &val);
  343. if (error < 0)
  344. goto out;
  345. tsdata->tp_information = val;
  346. error = regmap_read(tsdata->regmap, HY46XX_TP_CHIP_ID, &val);
  347. if (error < 0)
  348. goto out;
  349. tsdata->tp_chip_id = val;
  350. error = regmap_read(tsdata->regmap, HY46XX_BOOT_VER, &val);
  351. if (error < 0)
  352. goto out;
  353. tsdata->bootloader_version = val;
  354. return;
  355. out:
  356. dev_err(&tsdata->client->dev, "Failed to read default settings");
  357. }
  358. static void hycon_hy46xx_disable_regulator(void *arg)
  359. {
  360. struct hycon_hy46xx_data *data = arg;
  361. regulator_disable(data->vcc);
  362. }
  363. static int hycon_hy46xx_probe(struct i2c_client *client,
  364. const struct i2c_device_id *id)
  365. {
  366. struct hycon_hy46xx_data *tsdata;
  367. struct input_dev *input;
  368. int error;
  369. dev_dbg(&client->dev, "probing for HYCON HY46XX I2C\n");
  370. tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
  371. if (!tsdata)
  372. return -ENOMEM;
  373. tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
  374. if (IS_ERR(tsdata->vcc)) {
  375. error = PTR_ERR(tsdata->vcc);
  376. if (error != -EPROBE_DEFER)
  377. dev_err(&client->dev,
  378. "failed to request regulator: %d\n", error);
  379. return error;
  380. }
  381. error = regulator_enable(tsdata->vcc);
  382. if (error < 0) {
  383. dev_err(&client->dev, "failed to enable vcc: %d\n", error);
  384. return error;
  385. }
  386. error = devm_add_action_or_reset(&client->dev,
  387. hycon_hy46xx_disable_regulator,
  388. tsdata);
  389. if (error)
  390. return error;
  391. tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
  392. "reset", GPIOD_OUT_LOW);
  393. if (IS_ERR(tsdata->reset_gpio)) {
  394. error = PTR_ERR(tsdata->reset_gpio);
  395. dev_err(&client->dev,
  396. "Failed to request GPIO reset pin, error %d\n", error);
  397. return error;
  398. }
  399. if (tsdata->reset_gpio) {
  400. usleep_range(5000, 6000);
  401. gpiod_set_value_cansleep(tsdata->reset_gpio, 1);
  402. usleep_range(5000, 6000);
  403. gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
  404. msleep(1000);
  405. }
  406. input = devm_input_allocate_device(&client->dev);
  407. if (!input) {
  408. dev_err(&client->dev, "failed to allocate input device.\n");
  409. return -ENOMEM;
  410. }
  411. mutex_init(&tsdata->mutex);
  412. tsdata->client = client;
  413. tsdata->input = input;
  414. tsdata->regmap = devm_regmap_init_i2c(client,
  415. &hycon_hy46xx_i2c_regmap_config);
  416. if (IS_ERR(tsdata->regmap)) {
  417. dev_err(&client->dev, "regmap allocation failed\n");
  418. return PTR_ERR(tsdata->regmap);
  419. }
  420. hycon_hy46xx_get_defaults(&client->dev, tsdata);
  421. hycon_hy46xx_get_parameters(tsdata);
  422. input->name = "Hycon Capacitive Touch";
  423. input->id.bustype = BUS_I2C;
  424. input->dev.parent = &client->dev;
  425. input_set_abs_params(input, ABS_MT_POSITION_X, 0, -1, 0, 0);
  426. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, -1, 0, 0);
  427. touchscreen_parse_properties(input, true, &tsdata->prop);
  428. error = input_mt_init_slots(input, HY46XX_MAX_SUPPORTED_POINTS,
  429. INPUT_MT_DIRECT);
  430. if (error) {
  431. dev_err(&client->dev, "Unable to init MT slots.\n");
  432. return error;
  433. }
  434. i2c_set_clientdata(client, tsdata);
  435. error = devm_request_threaded_irq(&client->dev, client->irq,
  436. NULL, hycon_hy46xx_isr, IRQF_ONESHOT,
  437. client->name, tsdata);
  438. if (error) {
  439. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  440. return error;
  441. }
  442. error = devm_device_add_group(&client->dev, &hycon_hy46xx_attr_group);
  443. if (error)
  444. return error;
  445. error = input_register_device(input);
  446. if (error)
  447. return error;
  448. dev_dbg(&client->dev,
  449. "HYCON HY46XX initialized: IRQ %d, Reset pin %d.\n",
  450. client->irq,
  451. tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
  452. return 0;
  453. }
  454. static const struct i2c_device_id hycon_hy46xx_id[] = {
  455. { .name = "hy4613" },
  456. { .name = "hy4614" },
  457. { .name = "hy4621" },
  458. { .name = "hy4623" },
  459. { .name = "hy4633" },
  460. { .name = "hy4635" },
  461. { /* sentinel */ }
  462. };
  463. MODULE_DEVICE_TABLE(i2c, hycon_hy46xx_id);
  464. static const struct of_device_id hycon_hy46xx_of_match[] = {
  465. { .compatible = "hycon,hy4613" },
  466. { .compatible = "hycon,hy4614" },
  467. { .compatible = "hycon,hy4621" },
  468. { .compatible = "hycon,hy4623" },
  469. { .compatible = "hycon,hy4633" },
  470. { .compatible = "hycon,hy4635" },
  471. { /* sentinel */ }
  472. };
  473. MODULE_DEVICE_TABLE(of, hycon_hy46xx_of_match);
  474. static struct i2c_driver hycon_hy46xx_driver = {
  475. .driver = {
  476. .name = "hycon_hy46xx",
  477. .of_match_table = hycon_hy46xx_of_match,
  478. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  479. },
  480. .id_table = hycon_hy46xx_id,
  481. .probe = hycon_hy46xx_probe,
  482. };
  483. module_i2c_driver(hycon_hy46xx_driver);
  484. MODULE_AUTHOR("Giulio Benetti <[email protected]>");
  485. MODULE_DESCRIPTION("HYCON HY46XX I2C Touchscreen Driver");
  486. MODULE_LICENSE("GPL v2");