zinitix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/delay.h>
  3. #include <linux/i2c.h>
  4. #include <linux/input.h>
  5. #include <linux/input/mt.h>
  6. #include <linux/input/touchscreen.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/property.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/slab.h>
  15. /* Register Map */
  16. #define ZINITIX_SWRESET_CMD 0x0000
  17. #define ZINITIX_WAKEUP_CMD 0x0001
  18. #define ZINITIX_IDLE_CMD 0x0004
  19. #define ZINITIX_SLEEP_CMD 0x0005
  20. #define ZINITIX_CLEAR_INT_STATUS_CMD 0x0003
  21. #define ZINITIX_CALIBRATE_CMD 0x0006
  22. #define ZINITIX_SAVE_STATUS_CMD 0x0007
  23. #define ZINITIX_SAVE_CALIBRATION_CMD 0x0008
  24. #define ZINITIX_RECALL_FACTORY_CMD 0x000f
  25. #define ZINITIX_THRESHOLD 0x0020
  26. #define ZINITIX_LARGE_PALM_REJECT_AREA_TH 0x003F
  27. #define ZINITIX_DEBUG_REG 0x0115 /* 0~7 */
  28. #define ZINITIX_TOUCH_MODE 0x0010
  29. #define ZINITIX_CHIP_REVISION 0x0011
  30. #define ZINITIX_CHIP_BTX0X_MASK 0xF0F0
  31. #define ZINITIX_CHIP_BT4X2 0x4020
  32. #define ZINITIX_CHIP_BT4X3 0x4030
  33. #define ZINITIX_CHIP_BT4X4 0x4040
  34. #define ZINITIX_FIRMWARE_VERSION 0x0012
  35. #define ZINITIX_USB_DETECT 0x116
  36. #define ZINITIX_MINOR_FW_VERSION 0x0121
  37. #define ZINITIX_VENDOR_ID 0x001C
  38. #define ZINITIX_HW_ID 0x0014
  39. #define ZINITIX_DATA_VERSION_REG 0x0013
  40. #define ZINITIX_SUPPORTED_FINGER_NUM 0x0015
  41. #define ZINITIX_EEPROM_INFO 0x0018
  42. #define ZINITIX_INITIAL_TOUCH_MODE 0x0019
  43. #define ZINITIX_TOTAL_NUMBER_OF_X 0x0060
  44. #define ZINITIX_TOTAL_NUMBER_OF_Y 0x0061
  45. #define ZINITIX_DELAY_RAW_FOR_HOST 0x007f
  46. #define ZINITIX_BUTTON_SUPPORTED_NUM 0x00B0
  47. #define ZINITIX_BUTTON_SENSITIVITY 0x00B2
  48. #define ZINITIX_DUMMY_BUTTON_SENSITIVITY 0X00C8
  49. #define ZINITIX_X_RESOLUTION 0x00C0
  50. #define ZINITIX_Y_RESOLUTION 0x00C1
  51. #define ZINITIX_POINT_STATUS_REG 0x0080
  52. #define ZINITIX_BT4X2_ICON_STATUS_REG 0x009A
  53. #define ZINITIX_BT4X3_ICON_STATUS_REG 0x00A0
  54. #define ZINITIX_BT4X4_ICON_STATUS_REG 0x00A0
  55. #define ZINITIX_BT5XX_ICON_STATUS_REG 0x00AA
  56. #define ZINITIX_POINT_COORD_REG (ZINITIX_POINT_STATUS_REG + 2)
  57. #define ZINITIX_AFE_FREQUENCY 0x0100
  58. #define ZINITIX_DND_N_COUNT 0x0122
  59. #define ZINITIX_DND_U_COUNT 0x0135
  60. #define ZINITIX_RAWDATA_REG 0x0200
  61. #define ZINITIX_EEPROM_INFO_REG 0x0018
  62. #define ZINITIX_INT_ENABLE_FLAG 0x00f0
  63. #define ZINITIX_PERIODICAL_INTERRUPT_INTERVAL 0x00f1
  64. #define ZINITIX_BTN_WIDTH 0x016d
  65. #define ZINITIX_CHECKSUM_RESULT 0x012c
  66. #define ZINITIX_INIT_FLASH 0x01d0
  67. #define ZINITIX_WRITE_FLASH 0x01d1
  68. #define ZINITIX_READ_FLASH 0x01d2
  69. #define ZINITIX_INTERNAL_FLAG_02 0x011e
  70. #define ZINITIX_INTERNAL_FLAG_03 0x011f
  71. #define ZINITIX_I2C_CHECKSUM_WCNT 0x016a
  72. #define ZINITIX_I2C_CHECKSUM_RESULT 0x016c
  73. /* Interrupt & status register flags */
  74. #define BIT_PT_CNT_CHANGE BIT(0)
  75. #define BIT_DOWN BIT(1)
  76. #define BIT_MOVE BIT(2)
  77. #define BIT_UP BIT(3)
  78. #define BIT_PALM BIT(4)
  79. #define BIT_PALM_REJECT BIT(5)
  80. #define BIT_RESERVED_0 BIT(6)
  81. #define BIT_RESERVED_1 BIT(7)
  82. #define BIT_WEIGHT_CHANGE BIT(8)
  83. #define BIT_PT_NO_CHANGE BIT(9)
  84. #define BIT_REJECT BIT(10)
  85. #define BIT_PT_EXIST BIT(11)
  86. #define BIT_RESERVED_2 BIT(12)
  87. #define BIT_ERROR BIT(13)
  88. #define BIT_DEBUG BIT(14)
  89. #define BIT_ICON_EVENT BIT(15)
  90. #define SUB_BIT_EXIST BIT(0)
  91. #define SUB_BIT_DOWN BIT(1)
  92. #define SUB_BIT_MOVE BIT(2)
  93. #define SUB_BIT_UP BIT(3)
  94. #define SUB_BIT_UPDATE BIT(4)
  95. #define SUB_BIT_WAIT BIT(5)
  96. #define DEFAULT_TOUCH_POINT_MODE 2
  97. #define MAX_SUPPORTED_FINGER_NUM 5
  98. #define MAX_SUPPORTED_BUTTON_NUM 8
  99. #define CHIP_ON_DELAY 15 // ms
  100. #define FIRMWARE_ON_DELAY 40 // ms
  101. struct point_coord {
  102. __le16 x;
  103. __le16 y;
  104. u8 width;
  105. u8 sub_status;
  106. // currently unused, but needed as padding:
  107. u8 minor_width;
  108. u8 angle;
  109. };
  110. struct touch_event {
  111. __le16 status;
  112. u8 finger_mask;
  113. u8 time_stamp;
  114. struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
  115. };
  116. struct bt541_ts_data {
  117. struct i2c_client *client;
  118. struct input_dev *input_dev;
  119. struct touchscreen_properties prop;
  120. struct regulator_bulk_data supplies[2];
  121. u32 zinitix_mode;
  122. u32 keycodes[MAX_SUPPORTED_BUTTON_NUM];
  123. int num_keycodes;
  124. bool have_versioninfo;
  125. u16 chip_revision;
  126. u16 firmware_version;
  127. u16 regdata_version;
  128. u16 icon_status_reg;
  129. };
  130. static int zinitix_read_data(struct i2c_client *client,
  131. u16 reg, void *values, size_t length)
  132. {
  133. __le16 reg_le = cpu_to_le16(reg);
  134. int ret;
  135. /* A single i2c_transfer() transaction does not work here. */
  136. ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
  137. if (ret != sizeof(reg_le))
  138. return ret < 0 ? ret : -EIO;
  139. ret = i2c_master_recv(client, (u8 *)values, length);
  140. if (ret != length)
  141. return ret < 0 ? ret : -EIO;
  142. return 0;
  143. }
  144. static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
  145. {
  146. __le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
  147. int ret;
  148. ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
  149. if (ret != sizeof(packet))
  150. return ret < 0 ? ret : -EIO;
  151. return 0;
  152. }
  153. static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
  154. {
  155. __le16 reg_le = cpu_to_le16(reg);
  156. int ret;
  157. ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
  158. if (ret != sizeof(reg_le))
  159. return ret < 0 ? ret : -EIO;
  160. return 0;
  161. }
  162. static u16 zinitix_get_u16_reg(struct bt541_ts_data *bt541, u16 vreg)
  163. {
  164. struct i2c_client *client = bt541->client;
  165. int error;
  166. __le16 val;
  167. error = zinitix_read_data(client, vreg, (void *)&val, 2);
  168. if (error)
  169. return U8_MAX;
  170. return le16_to_cpu(val);
  171. }
  172. static int zinitix_init_touch(struct bt541_ts_data *bt541)
  173. {
  174. struct i2c_client *client = bt541->client;
  175. int i;
  176. int error;
  177. u16 int_flags;
  178. error = zinitix_write_cmd(client, ZINITIX_SWRESET_CMD);
  179. if (error) {
  180. dev_err(&client->dev, "Failed to write reset command\n");
  181. return error;
  182. }
  183. /*
  184. * Read and cache the chip revision and firmware version the first time
  185. * we get here.
  186. */
  187. if (!bt541->have_versioninfo) {
  188. bt541->chip_revision = zinitix_get_u16_reg(bt541,
  189. ZINITIX_CHIP_REVISION);
  190. bt541->firmware_version = zinitix_get_u16_reg(bt541,
  191. ZINITIX_FIRMWARE_VERSION);
  192. bt541->regdata_version = zinitix_get_u16_reg(bt541,
  193. ZINITIX_DATA_VERSION_REG);
  194. bt541->have_versioninfo = true;
  195. dev_dbg(&client->dev,
  196. "chip revision %04x firmware version %04x regdata version %04x\n",
  197. bt541->chip_revision, bt541->firmware_version,
  198. bt541->regdata_version);
  199. /*
  200. * Determine the "icon" status register which varies by the
  201. * chip.
  202. */
  203. switch (bt541->chip_revision & ZINITIX_CHIP_BTX0X_MASK) {
  204. case ZINITIX_CHIP_BT4X2:
  205. bt541->icon_status_reg = ZINITIX_BT4X2_ICON_STATUS_REG;
  206. break;
  207. case ZINITIX_CHIP_BT4X3:
  208. bt541->icon_status_reg = ZINITIX_BT4X3_ICON_STATUS_REG;
  209. break;
  210. case ZINITIX_CHIP_BT4X4:
  211. bt541->icon_status_reg = ZINITIX_BT4X4_ICON_STATUS_REG;
  212. break;
  213. default:
  214. bt541->icon_status_reg = ZINITIX_BT5XX_ICON_STATUS_REG;
  215. break;
  216. }
  217. }
  218. error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, 0x0);
  219. if (error) {
  220. dev_err(&client->dev,
  221. "Failed to reset interrupt enable flag\n");
  222. return error;
  223. }
  224. /* initialize */
  225. error = zinitix_write_u16(client, ZINITIX_X_RESOLUTION,
  226. bt541->prop.max_x);
  227. if (error)
  228. return error;
  229. error = zinitix_write_u16(client, ZINITIX_Y_RESOLUTION,
  230. bt541->prop.max_y);
  231. if (error)
  232. return error;
  233. error = zinitix_write_u16(client, ZINITIX_SUPPORTED_FINGER_NUM,
  234. MAX_SUPPORTED_FINGER_NUM);
  235. if (error)
  236. return error;
  237. error = zinitix_write_u16(client, ZINITIX_BUTTON_SUPPORTED_NUM,
  238. bt541->num_keycodes);
  239. if (error)
  240. return error;
  241. error = zinitix_write_u16(client, ZINITIX_INITIAL_TOUCH_MODE,
  242. bt541->zinitix_mode);
  243. if (error)
  244. return error;
  245. error = zinitix_write_u16(client, ZINITIX_TOUCH_MODE,
  246. bt541->zinitix_mode);
  247. if (error)
  248. return error;
  249. int_flags = BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE | BIT_UP;
  250. if (bt541->num_keycodes)
  251. int_flags |= BIT_ICON_EVENT;
  252. error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, int_flags);
  253. if (error)
  254. return error;
  255. /* clear queue */
  256. for (i = 0; i < 10; i++) {
  257. zinitix_write_cmd(client, ZINITIX_CLEAR_INT_STATUS_CMD);
  258. udelay(10);
  259. }
  260. return 0;
  261. }
  262. static int zinitix_init_regulators(struct bt541_ts_data *bt541)
  263. {
  264. struct device *dev = &bt541->client->dev;
  265. int error;
  266. /*
  267. * Some older device trees have erroneous names for the regulators,
  268. * so check if "vddo" is present and in that case use these names.
  269. * Else use the proper supply names on the component.
  270. */
  271. if (of_property_present(dev->of_node, "vddo-supply")) {
  272. bt541->supplies[0].supply = "vdd";
  273. bt541->supplies[1].supply = "vddo";
  274. } else {
  275. /* Else use the proper supply names */
  276. bt541->supplies[0].supply = "vcca";
  277. bt541->supplies[1].supply = "vdd";
  278. }
  279. error = devm_regulator_bulk_get(dev,
  280. ARRAY_SIZE(bt541->supplies),
  281. bt541->supplies);
  282. if (error < 0) {
  283. dev_err(dev, "Failed to get regulators: %d\n", error);
  284. return error;
  285. }
  286. return 0;
  287. }
  288. static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
  289. {
  290. int error;
  291. struct i2c_client *client = bt541->client;
  292. error = zinitix_write_u16(client, 0xc000, 0x0001);
  293. if (error) {
  294. dev_err(&client->dev,
  295. "Failed to send power sequence(vendor cmd enable)\n");
  296. return error;
  297. }
  298. udelay(10);
  299. error = zinitix_write_cmd(client, 0xc004);
  300. if (error) {
  301. dev_err(&client->dev,
  302. "Failed to send power sequence (intn clear)\n");
  303. return error;
  304. }
  305. udelay(10);
  306. error = zinitix_write_u16(client, 0xc002, 0x0001);
  307. if (error) {
  308. dev_err(&client->dev,
  309. "Failed to send power sequence (nvm init)\n");
  310. return error;
  311. }
  312. mdelay(2);
  313. error = zinitix_write_u16(client, 0xc001, 0x0001);
  314. if (error) {
  315. dev_err(&client->dev,
  316. "Failed to send power sequence (program start)\n");
  317. return error;
  318. }
  319. msleep(FIRMWARE_ON_DELAY);
  320. return 0;
  321. }
  322. static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
  323. const struct point_coord *p)
  324. {
  325. u16 x, y;
  326. if (unlikely(!(p->sub_status &
  327. (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) {
  328. dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n",
  329. p->sub_status);
  330. return;
  331. }
  332. x = le16_to_cpu(p->x);
  333. y = le16_to_cpu(p->y);
  334. input_mt_slot(bt541->input_dev, slot);
  335. if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER,
  336. !(p->sub_status & SUB_BIT_UP))) {
  337. touchscreen_report_pos(bt541->input_dev,
  338. &bt541->prop, x, y, true);
  339. input_report_abs(bt541->input_dev,
  340. ABS_MT_TOUCH_MAJOR, p->width);
  341. dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n",
  342. slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move",
  343. x, y);
  344. } else {
  345. dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n",
  346. slot, x, y);
  347. }
  348. }
  349. static void zinitix_report_keys(struct bt541_ts_data *bt541, u16 icon_events)
  350. {
  351. int i;
  352. for (i = 0; i < bt541->num_keycodes; i++)
  353. input_report_key(bt541->input_dev,
  354. bt541->keycodes[i], icon_events & BIT(i));
  355. }
  356. static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
  357. {
  358. struct bt541_ts_data *bt541 = bt541_handler;
  359. struct i2c_client *client = bt541->client;
  360. struct touch_event touch_event;
  361. unsigned long finger_mask;
  362. __le16 icon_events;
  363. int error;
  364. int i;
  365. memset(&touch_event, 0, sizeof(struct touch_event));
  366. error = zinitix_read_data(bt541->client, ZINITIX_POINT_STATUS_REG,
  367. &touch_event, sizeof(struct touch_event));
  368. if (error) {
  369. dev_err(&client->dev, "Failed to read in touchpoint struct\n");
  370. goto out;
  371. }
  372. if (le16_to_cpu(touch_event.status) & BIT_ICON_EVENT) {
  373. error = zinitix_read_data(bt541->client, bt541->icon_status_reg,
  374. &icon_events, sizeof(icon_events));
  375. if (error) {
  376. dev_err(&client->dev, "Failed to read icon events\n");
  377. goto out;
  378. }
  379. zinitix_report_keys(bt541, le16_to_cpu(icon_events));
  380. }
  381. finger_mask = touch_event.finger_mask;
  382. for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) {
  383. const struct point_coord *p = &touch_event.point_coord[i];
  384. /* Only process contacts that are actually reported */
  385. if (p->sub_status & SUB_BIT_EXIST)
  386. zinitix_report_finger(bt541, i, p);
  387. }
  388. input_mt_sync_frame(bt541->input_dev);
  389. input_sync(bt541->input_dev);
  390. out:
  391. zinitix_write_cmd(bt541->client, ZINITIX_CLEAR_INT_STATUS_CMD);
  392. return IRQ_HANDLED;
  393. }
  394. static int zinitix_start(struct bt541_ts_data *bt541)
  395. {
  396. int error;
  397. error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
  398. bt541->supplies);
  399. if (error) {
  400. dev_err(&bt541->client->dev,
  401. "Failed to enable regulators: %d\n", error);
  402. return error;
  403. }
  404. msleep(CHIP_ON_DELAY);
  405. error = zinitix_send_power_on_sequence(bt541);
  406. if (error) {
  407. dev_err(&bt541->client->dev,
  408. "Error while sending power-on sequence: %d\n", error);
  409. return error;
  410. }
  411. error = zinitix_init_touch(bt541);
  412. if (error) {
  413. dev_err(&bt541->client->dev,
  414. "Error while configuring touch IC\n");
  415. return error;
  416. }
  417. enable_irq(bt541->client->irq);
  418. return 0;
  419. }
  420. static int zinitix_stop(struct bt541_ts_data *bt541)
  421. {
  422. int error;
  423. disable_irq(bt541->client->irq);
  424. error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
  425. bt541->supplies);
  426. if (error) {
  427. dev_err(&bt541->client->dev,
  428. "Failed to disable regulators: %d\n", error);
  429. return error;
  430. }
  431. return 0;
  432. }
  433. static int zinitix_input_open(struct input_dev *dev)
  434. {
  435. struct bt541_ts_data *bt541 = input_get_drvdata(dev);
  436. return zinitix_start(bt541);
  437. }
  438. static void zinitix_input_close(struct input_dev *dev)
  439. {
  440. struct bt541_ts_data *bt541 = input_get_drvdata(dev);
  441. zinitix_stop(bt541);
  442. }
  443. static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
  444. {
  445. struct input_dev *input_dev;
  446. int error;
  447. int i;
  448. input_dev = devm_input_allocate_device(&bt541->client->dev);
  449. if (!input_dev) {
  450. dev_err(&bt541->client->dev,
  451. "Failed to allocate input device.");
  452. return -ENOMEM;
  453. }
  454. input_set_drvdata(input_dev, bt541);
  455. bt541->input_dev = input_dev;
  456. input_dev->name = "Zinitix Capacitive TouchScreen";
  457. input_dev->phys = "input/ts";
  458. input_dev->id.bustype = BUS_I2C;
  459. input_dev->open = zinitix_input_open;
  460. input_dev->close = zinitix_input_close;
  461. if (bt541->num_keycodes) {
  462. input_dev->keycode = bt541->keycodes;
  463. input_dev->keycodemax = bt541->num_keycodes;
  464. input_dev->keycodesize = sizeof(bt541->keycodes[0]);
  465. for (i = 0; i < bt541->num_keycodes; i++)
  466. input_set_capability(input_dev, EV_KEY, bt541->keycodes[i]);
  467. }
  468. input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
  469. input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
  470. input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  471. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  472. touchscreen_parse_properties(input_dev, true, &bt541->prop);
  473. if (!bt541->prop.max_x || !bt541->prop.max_y) {
  474. dev_err(&bt541->client->dev,
  475. "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
  476. return -EINVAL;
  477. }
  478. error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
  479. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  480. if (error) {
  481. dev_err(&bt541->client->dev,
  482. "Failed to initialize MT slots: %d", error);
  483. return error;
  484. }
  485. error = input_register_device(input_dev);
  486. if (error) {
  487. dev_err(&bt541->client->dev,
  488. "Failed to register input device: %d", error);
  489. return error;
  490. }
  491. return 0;
  492. }
  493. static int zinitix_ts_probe(struct i2c_client *client)
  494. {
  495. struct bt541_ts_data *bt541;
  496. int error;
  497. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  498. dev_err(&client->dev,
  499. "Failed to assert adapter's support for plain I2C.\n");
  500. return -ENXIO;
  501. }
  502. bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
  503. if (!bt541)
  504. return -ENOMEM;
  505. bt541->client = client;
  506. i2c_set_clientdata(client, bt541);
  507. error = zinitix_init_regulators(bt541);
  508. if (error) {
  509. dev_err(&client->dev,
  510. "Failed to initialize regulators: %d\n", error);
  511. return error;
  512. }
  513. error = devm_request_threaded_irq(&client->dev, client->irq,
  514. NULL, zinitix_ts_irq_handler,
  515. IRQF_ONESHOT | IRQF_NO_AUTOEN,
  516. client->name, bt541);
  517. if (error) {
  518. dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
  519. return error;
  520. }
  521. if (device_property_present(&client->dev, "linux,keycodes")) {
  522. bt541->num_keycodes = device_property_count_u32(&client->dev,
  523. "linux,keycodes");
  524. if (bt541->num_keycodes < 0) {
  525. dev_err(&client->dev, "Failed to count keys (%d)\n",
  526. bt541->num_keycodes);
  527. return bt541->num_keycodes;
  528. } else if (bt541->num_keycodes > ARRAY_SIZE(bt541->keycodes)) {
  529. dev_err(&client->dev, "Too many keys defined (%d)\n",
  530. bt541->num_keycodes);
  531. return -EINVAL;
  532. }
  533. error = device_property_read_u32_array(&client->dev,
  534. "linux,keycodes",
  535. bt541->keycodes,
  536. bt541->num_keycodes);
  537. if (error) {
  538. dev_err(&client->dev,
  539. "Unable to parse \"linux,keycodes\" property: %d\n",
  540. error);
  541. return error;
  542. }
  543. }
  544. error = zinitix_init_input_dev(bt541);
  545. if (error) {
  546. dev_err(&client->dev,
  547. "Failed to initialize input device: %d\n", error);
  548. return error;
  549. }
  550. error = device_property_read_u32(&client->dev, "zinitix,mode",
  551. &bt541->zinitix_mode);
  552. if (error < 0) {
  553. /* fall back to mode 2 */
  554. bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
  555. }
  556. if (bt541->zinitix_mode != 2) {
  557. /*
  558. * If there are devices that don't support mode 2, support
  559. * for other modes (0, 1) will be needed.
  560. */
  561. dev_err(&client->dev,
  562. "Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
  563. bt541->zinitix_mode);
  564. return -EINVAL;
  565. }
  566. return 0;
  567. }
  568. static int zinitix_suspend(struct device *dev)
  569. {
  570. struct i2c_client *client = to_i2c_client(dev);
  571. struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
  572. mutex_lock(&bt541->input_dev->mutex);
  573. if (input_device_enabled(bt541->input_dev))
  574. zinitix_stop(bt541);
  575. mutex_unlock(&bt541->input_dev->mutex);
  576. return 0;
  577. }
  578. static int zinitix_resume(struct device *dev)
  579. {
  580. struct i2c_client *client = to_i2c_client(dev);
  581. struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
  582. int ret = 0;
  583. mutex_lock(&bt541->input_dev->mutex);
  584. if (input_device_enabled(bt541->input_dev))
  585. ret = zinitix_start(bt541);
  586. mutex_unlock(&bt541->input_dev->mutex);
  587. return ret;
  588. }
  589. static DEFINE_SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
  590. #ifdef CONFIG_OF
  591. static const struct of_device_id zinitix_of_match[] = {
  592. { .compatible = "zinitix,bt402" },
  593. { .compatible = "zinitix,bt403" },
  594. { .compatible = "zinitix,bt404" },
  595. { .compatible = "zinitix,bt412" },
  596. { .compatible = "zinitix,bt413" },
  597. { .compatible = "zinitix,bt431" },
  598. { .compatible = "zinitix,bt432" },
  599. { .compatible = "zinitix,bt531" },
  600. { .compatible = "zinitix,bt532" },
  601. { .compatible = "zinitix,bt538" },
  602. { .compatible = "zinitix,bt541" },
  603. { .compatible = "zinitix,bt548" },
  604. { .compatible = "zinitix,bt554" },
  605. { .compatible = "zinitix,at100" },
  606. { }
  607. };
  608. MODULE_DEVICE_TABLE(of, zinitix_of_match);
  609. #endif
  610. static struct i2c_driver zinitix_ts_driver = {
  611. .probe = zinitix_ts_probe,
  612. .driver = {
  613. .name = "Zinitix-TS",
  614. .pm = pm_sleep_ptr(&zinitix_pm_ops),
  615. .of_match_table = of_match_ptr(zinitix_of_match),
  616. },
  617. };
  618. module_i2c_driver(zinitix_ts_driver);
  619. MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
  620. MODULE_DESCRIPTION("Zinitix touchscreen driver");
  621. MODULE_LICENSE("GPL v2");