silead.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -------------------------------------------------------------------------
  3. * Copyright (C) 2014-2015, Intel Corporation
  4. *
  5. * Derived from:
  6. * gslX68X.c
  7. * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
  8. *
  9. * -------------------------------------------------------------------------
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/acpi.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/delay.h>
  17. #include <linux/firmware.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/input/touchscreen.h>
  21. #include <linux/pm.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/irq.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <asm/unaligned.h>
  26. #define SILEAD_TS_NAME "silead_ts"
  27. #define SILEAD_REG_RESET 0xE0
  28. #define SILEAD_REG_DATA 0x80
  29. #define SILEAD_REG_TOUCH_NR 0x80
  30. #define SILEAD_REG_POWER 0xBC
  31. #define SILEAD_REG_CLOCK 0xE4
  32. #define SILEAD_REG_STATUS 0xB0
  33. #define SILEAD_REG_ID 0xFC
  34. #define SILEAD_REG_MEM_CHECK 0xB0
  35. #define SILEAD_STATUS_OK 0x5A5A5A5A
  36. #define SILEAD_TS_DATA_LEN 44
  37. #define SILEAD_CLOCK 0x04
  38. #define SILEAD_CMD_RESET 0x88
  39. #define SILEAD_CMD_START 0x00
  40. #define SILEAD_POINT_DATA_LEN 0x04
  41. #define SILEAD_POINT_Y_OFF 0x00
  42. #define SILEAD_POINT_Y_MSB_OFF 0x01
  43. #define SILEAD_POINT_X_OFF 0x02
  44. #define SILEAD_POINT_X_MSB_OFF 0x03
  45. #define SILEAD_EXTRA_DATA_MASK 0xF0
  46. #define SILEAD_CMD_SLEEP_MIN 10000
  47. #define SILEAD_CMD_SLEEP_MAX 20000
  48. #define SILEAD_POWER_SLEEP 20
  49. #define SILEAD_STARTUP_SLEEP 30
  50. #define SILEAD_MAX_FINGERS 10
  51. enum silead_ts_power {
  52. SILEAD_POWER_ON = 1,
  53. SILEAD_POWER_OFF = 0
  54. };
  55. struct silead_ts_data {
  56. struct i2c_client *client;
  57. struct gpio_desc *gpio_power;
  58. struct input_dev *input;
  59. struct input_dev *pen_input;
  60. struct regulator_bulk_data regulators[2];
  61. char fw_name[64];
  62. struct touchscreen_properties prop;
  63. u32 max_fingers;
  64. u32 chip_id;
  65. struct input_mt_pos pos[SILEAD_MAX_FINGERS];
  66. int slots[SILEAD_MAX_FINGERS];
  67. int id[SILEAD_MAX_FINGERS];
  68. u32 efi_fw_min_max[4];
  69. bool efi_fw_min_max_set;
  70. bool pen_supported;
  71. bool pen_down;
  72. u32 pen_x_res;
  73. u32 pen_y_res;
  74. int pen_up_count;
  75. };
  76. struct silead_fw_data {
  77. u32 offset;
  78. u32 val;
  79. };
  80. static void silead_apply_efi_fw_min_max(struct silead_ts_data *data)
  81. {
  82. struct input_absinfo *absinfo_x = &data->input->absinfo[ABS_MT_POSITION_X];
  83. struct input_absinfo *absinfo_y = &data->input->absinfo[ABS_MT_POSITION_Y];
  84. if (!data->efi_fw_min_max_set)
  85. return;
  86. absinfo_x->minimum = data->efi_fw_min_max[0];
  87. absinfo_x->maximum = data->efi_fw_min_max[1];
  88. absinfo_y->minimum = data->efi_fw_min_max[2];
  89. absinfo_y->maximum = data->efi_fw_min_max[3];
  90. if (data->prop.invert_x) {
  91. absinfo_x->maximum -= absinfo_x->minimum;
  92. absinfo_x->minimum = 0;
  93. }
  94. if (data->prop.invert_y) {
  95. absinfo_y->maximum -= absinfo_y->minimum;
  96. absinfo_y->minimum = 0;
  97. }
  98. if (data->prop.swap_x_y) {
  99. swap(absinfo_x->minimum, absinfo_y->minimum);
  100. swap(absinfo_x->maximum, absinfo_y->maximum);
  101. }
  102. }
  103. static int silead_ts_request_input_dev(struct silead_ts_data *data)
  104. {
  105. struct device *dev = &data->client->dev;
  106. int error;
  107. data->input = devm_input_allocate_device(dev);
  108. if (!data->input) {
  109. dev_err(dev,
  110. "Failed to allocate input device\n");
  111. return -ENOMEM;
  112. }
  113. input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
  114. input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
  115. touchscreen_parse_properties(data->input, true, &data->prop);
  116. silead_apply_efi_fw_min_max(data);
  117. input_mt_init_slots(data->input, data->max_fingers,
  118. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
  119. INPUT_MT_TRACK);
  120. if (device_property_read_bool(dev, "silead,home-button"))
  121. input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
  122. data->input->name = SILEAD_TS_NAME;
  123. data->input->phys = "input/ts";
  124. data->input->id.bustype = BUS_I2C;
  125. error = input_register_device(data->input);
  126. if (error) {
  127. dev_err(dev, "Failed to register input device: %d\n", error);
  128. return error;
  129. }
  130. return 0;
  131. }
  132. static int silead_ts_request_pen_input_dev(struct silead_ts_data *data)
  133. {
  134. struct device *dev = &data->client->dev;
  135. int error;
  136. if (!data->pen_supported)
  137. return 0;
  138. data->pen_input = devm_input_allocate_device(dev);
  139. if (!data->pen_input)
  140. return -ENOMEM;
  141. input_set_abs_params(data->pen_input, ABS_X, 0, 4095, 0, 0);
  142. input_set_abs_params(data->pen_input, ABS_Y, 0, 4095, 0, 0);
  143. input_set_capability(data->pen_input, EV_KEY, BTN_TOUCH);
  144. input_set_capability(data->pen_input, EV_KEY, BTN_TOOL_PEN);
  145. set_bit(INPUT_PROP_DIRECT, data->pen_input->propbit);
  146. touchscreen_parse_properties(data->pen_input, false, &data->prop);
  147. input_abs_set_res(data->pen_input, ABS_X, data->pen_x_res);
  148. input_abs_set_res(data->pen_input, ABS_Y, data->pen_y_res);
  149. data->pen_input->name = SILEAD_TS_NAME " pen";
  150. data->pen_input->phys = "input/pen";
  151. data->input->id.bustype = BUS_I2C;
  152. error = input_register_device(data->pen_input);
  153. if (error) {
  154. dev_err(dev, "Failed to register pen input device: %d\n", error);
  155. return error;
  156. }
  157. return 0;
  158. }
  159. static void silead_ts_set_power(struct i2c_client *client,
  160. enum silead_ts_power state)
  161. {
  162. struct silead_ts_data *data = i2c_get_clientdata(client);
  163. if (data->gpio_power) {
  164. gpiod_set_value_cansleep(data->gpio_power, state);
  165. msleep(SILEAD_POWER_SLEEP);
  166. }
  167. }
  168. static bool silead_ts_handle_pen_data(struct silead_ts_data *data, u8 *buf)
  169. {
  170. u8 *coord = buf + SILEAD_POINT_DATA_LEN;
  171. struct input_mt_pos pos;
  172. if (!data->pen_supported || buf[2] != 0x00 || buf[3] != 0x00)
  173. return false;
  174. if (buf[0] == 0x00 && buf[1] == 0x00 && data->pen_down) {
  175. data->pen_up_count++;
  176. if (data->pen_up_count == 6) {
  177. data->pen_down = false;
  178. goto sync;
  179. }
  180. return true;
  181. }
  182. if (buf[0] == 0x01 && buf[1] == 0x08) {
  183. touchscreen_set_mt_pos(&pos, &data->prop,
  184. get_unaligned_le16(&coord[SILEAD_POINT_X_OFF]) & 0xfff,
  185. get_unaligned_le16(&coord[SILEAD_POINT_Y_OFF]) & 0xfff);
  186. input_report_abs(data->pen_input, ABS_X, pos.x);
  187. input_report_abs(data->pen_input, ABS_Y, pos.y);
  188. data->pen_up_count = 0;
  189. data->pen_down = true;
  190. goto sync;
  191. }
  192. return false;
  193. sync:
  194. input_report_key(data->pen_input, BTN_TOOL_PEN, data->pen_down);
  195. input_report_key(data->pen_input, BTN_TOUCH, data->pen_down);
  196. input_sync(data->pen_input);
  197. return true;
  198. }
  199. static void silead_ts_read_data(struct i2c_client *client)
  200. {
  201. struct silead_ts_data *data = i2c_get_clientdata(client);
  202. struct input_dev *input = data->input;
  203. struct device *dev = &client->dev;
  204. u8 *bufp, buf[SILEAD_TS_DATA_LEN];
  205. int touch_nr, softbutton, error, i;
  206. bool softbutton_pressed = false;
  207. error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
  208. SILEAD_TS_DATA_LEN, buf);
  209. if (error < 0) {
  210. dev_err(dev, "Data read error %d\n", error);
  211. return;
  212. }
  213. if (buf[0] > data->max_fingers) {
  214. dev_warn(dev, "More touches reported then supported %d > %d\n",
  215. buf[0], data->max_fingers);
  216. buf[0] = data->max_fingers;
  217. }
  218. if (silead_ts_handle_pen_data(data, buf))
  219. goto sync; /* Pen is down, release all previous touches */
  220. touch_nr = 0;
  221. bufp = buf + SILEAD_POINT_DATA_LEN;
  222. for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
  223. softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
  224. SILEAD_EXTRA_DATA_MASK) >> 4;
  225. if (softbutton) {
  226. /*
  227. * For now only respond to softbutton == 0x01, some
  228. * tablets *without* a capacative button send 0x04
  229. * when crossing the edges of the screen.
  230. */
  231. if (softbutton == 0x01)
  232. softbutton_pressed = true;
  233. continue;
  234. }
  235. /*
  236. * Bits 4-7 are the touch id, note not all models have
  237. * hardware touch ids so atm we don't use these.
  238. */
  239. data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
  240. SILEAD_EXTRA_DATA_MASK) >> 4;
  241. touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
  242. get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
  243. get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
  244. touch_nr++;
  245. }
  246. input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
  247. for (i = 0; i < touch_nr; i++) {
  248. input_mt_slot(input, data->slots[i]);
  249. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  250. input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
  251. input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
  252. dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
  253. data->pos[i].y, data->id[i], data->slots[i]);
  254. }
  255. sync:
  256. input_mt_sync_frame(input);
  257. input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
  258. input_sync(input);
  259. }
  260. static int silead_ts_init(struct i2c_client *client)
  261. {
  262. struct silead_ts_data *data = i2c_get_clientdata(client);
  263. int error;
  264. error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
  265. SILEAD_CMD_RESET);
  266. if (error)
  267. goto i2c_write_err;
  268. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  269. error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
  270. data->max_fingers);
  271. if (error)
  272. goto i2c_write_err;
  273. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  274. error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
  275. SILEAD_CLOCK);
  276. if (error)
  277. goto i2c_write_err;
  278. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  279. error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
  280. SILEAD_CMD_START);
  281. if (error)
  282. goto i2c_write_err;
  283. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  284. return 0;
  285. i2c_write_err:
  286. dev_err(&client->dev, "Registers clear error %d\n", error);
  287. return error;
  288. }
  289. static int silead_ts_reset(struct i2c_client *client)
  290. {
  291. int error;
  292. error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
  293. SILEAD_CMD_RESET);
  294. if (error)
  295. goto i2c_write_err;
  296. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  297. error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
  298. SILEAD_CLOCK);
  299. if (error)
  300. goto i2c_write_err;
  301. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  302. error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
  303. SILEAD_CMD_START);
  304. if (error)
  305. goto i2c_write_err;
  306. usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
  307. return 0;
  308. i2c_write_err:
  309. dev_err(&client->dev, "Chip reset error %d\n", error);
  310. return error;
  311. }
  312. static int silead_ts_startup(struct i2c_client *client)
  313. {
  314. int error;
  315. error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
  316. if (error) {
  317. dev_err(&client->dev, "Startup error %d\n", error);
  318. return error;
  319. }
  320. msleep(SILEAD_STARTUP_SLEEP);
  321. return 0;
  322. }
  323. static int silead_ts_load_fw(struct i2c_client *client)
  324. {
  325. struct device *dev = &client->dev;
  326. struct silead_ts_data *data = i2c_get_clientdata(client);
  327. const struct firmware *fw = NULL;
  328. struct silead_fw_data *fw_data;
  329. unsigned int fw_size, i;
  330. int error;
  331. dev_dbg(dev, "Firmware file name: %s", data->fw_name);
  332. /*
  333. * Unfortunately, at the time of writing this comment, we have been unable to
  334. * get permission from Silead, or from device OEMs, to distribute the necessary
  335. * Silead firmware files in linux-firmware.
  336. *
  337. * On a whole bunch of devices the UEFI BIOS code contains a touchscreen driver,
  338. * which contains an embedded copy of the firmware. The fw-loader code has a
  339. * "platform" fallback mechanism, which together with info on the firmware
  340. * from drivers/platform/x86/touchscreen_dmi.c will use the firmware from the
  341. * UEFI driver when the firmware is missing from /lib/firmware. This makes the
  342. * touchscreen work OOTB without users needing to manually download the firmware.
  343. *
  344. * The firmware bundled with the original Windows/Android is usually newer then
  345. * the firmware in the UEFI driver and it is better calibrated. This better
  346. * calibration can lead to significant differences in the reported min/max
  347. * coordinates.
  348. *
  349. * To deal with this we first try to load the firmware without "platform"
  350. * fallback. If that fails we retry with "platform" fallback and if that
  351. * succeeds we apply an (optional) set of alternative min/max values from the
  352. * "silead,efi-fw-min-max" property.
  353. */
  354. error = firmware_request_nowarn(&fw, data->fw_name, dev);
  355. if (error) {
  356. error = firmware_request_platform(&fw, data->fw_name, dev);
  357. if (error) {
  358. dev_err(dev, "Firmware request error %d\n", error);
  359. return error;
  360. }
  361. error = device_property_read_u32_array(dev, "silead,efi-fw-min-max",
  362. data->efi_fw_min_max,
  363. ARRAY_SIZE(data->efi_fw_min_max));
  364. if (!error)
  365. data->efi_fw_min_max_set = true;
  366. /* The EFI (platform) embedded fw does not have pen support */
  367. if (data->pen_supported) {
  368. dev_warn(dev, "Warning loading '%s' from filesystem failed, using EFI embedded copy.\n",
  369. data->fw_name);
  370. dev_warn(dev, "Warning pen support is known to be broken in the EFI embedded fw version\n");
  371. data->pen_supported = false;
  372. }
  373. }
  374. fw_size = fw->size / sizeof(*fw_data);
  375. fw_data = (struct silead_fw_data *)fw->data;
  376. for (i = 0; i < fw_size; i++) {
  377. error = i2c_smbus_write_i2c_block_data(client,
  378. fw_data[i].offset,
  379. 4,
  380. (u8 *)&fw_data[i].val);
  381. if (error) {
  382. dev_err(dev, "Firmware load error %d\n", error);
  383. break;
  384. }
  385. }
  386. release_firmware(fw);
  387. return error ?: 0;
  388. }
  389. static u32 silead_ts_get_status(struct i2c_client *client)
  390. {
  391. int error;
  392. __le32 status;
  393. error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
  394. sizeof(status), (u8 *)&status);
  395. if (error < 0) {
  396. dev_err(&client->dev, "Status read error %d\n", error);
  397. return error;
  398. }
  399. return le32_to_cpu(status);
  400. }
  401. static int silead_ts_get_id(struct i2c_client *client)
  402. {
  403. struct silead_ts_data *data = i2c_get_clientdata(client);
  404. __le32 chip_id;
  405. int error;
  406. error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
  407. sizeof(chip_id), (u8 *)&chip_id);
  408. if (error < 0)
  409. return error;
  410. data->chip_id = le32_to_cpu(chip_id);
  411. dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
  412. return 0;
  413. }
  414. static int silead_ts_setup(struct i2c_client *client)
  415. {
  416. int error;
  417. u32 status;
  418. /*
  419. * Some buggy BIOS-es bring up the chip in a stuck state where it
  420. * blocks the I2C bus. The following steps are necessary to
  421. * unstuck the chip / bus:
  422. * 1. Turn off the Silead chip.
  423. * 2. Try to do an I2C transfer with the chip, this will fail in
  424. * response to which the I2C-bus-driver will call:
  425. * i2c_recover_bus() which will unstuck the I2C-bus. Note the
  426. * unstuck-ing of the I2C bus only works if we first drop the
  427. * chip off the bus by turning it off.
  428. * 3. Turn the chip back on.
  429. *
  430. * On the x86/ACPI systems were this problem is seen, step 1. and
  431. * 3. require making ACPI calls and dealing with ACPI Power
  432. * Resources. The workaround below runtime-suspends the chip to
  433. * turn it off, leaving it up to the ACPI subsystem to deal with
  434. * this.
  435. */
  436. if (device_property_read_bool(&client->dev,
  437. "silead,stuck-controller-bug")) {
  438. pm_runtime_set_active(&client->dev);
  439. pm_runtime_enable(&client->dev);
  440. pm_runtime_allow(&client->dev);
  441. pm_runtime_suspend(&client->dev);
  442. dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
  443. silead_ts_get_id(client);
  444. /* The forbid will also resume the device */
  445. pm_runtime_forbid(&client->dev);
  446. pm_runtime_disable(&client->dev);
  447. }
  448. silead_ts_set_power(client, SILEAD_POWER_OFF);
  449. silead_ts_set_power(client, SILEAD_POWER_ON);
  450. error = silead_ts_get_id(client);
  451. if (error) {
  452. dev_err(&client->dev, "Chip ID read error %d\n", error);
  453. return error;
  454. }
  455. error = silead_ts_init(client);
  456. if (error)
  457. return error;
  458. error = silead_ts_reset(client);
  459. if (error)
  460. return error;
  461. error = silead_ts_load_fw(client);
  462. if (error)
  463. return error;
  464. error = silead_ts_startup(client);
  465. if (error)
  466. return error;
  467. status = silead_ts_get_status(client);
  468. if (status != SILEAD_STATUS_OK) {
  469. dev_err(&client->dev,
  470. "Initialization error, status: 0x%X\n", status);
  471. return -ENODEV;
  472. }
  473. return 0;
  474. }
  475. static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
  476. {
  477. struct silead_ts_data *data = id;
  478. struct i2c_client *client = data->client;
  479. silead_ts_read_data(client);
  480. return IRQ_HANDLED;
  481. }
  482. static void silead_ts_read_props(struct i2c_client *client)
  483. {
  484. struct silead_ts_data *data = i2c_get_clientdata(client);
  485. struct device *dev = &client->dev;
  486. const char *str;
  487. int error;
  488. error = device_property_read_u32(dev, "silead,max-fingers",
  489. &data->max_fingers);
  490. if (error) {
  491. dev_dbg(dev, "Max fingers read error %d\n", error);
  492. data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
  493. }
  494. error = device_property_read_string(dev, "firmware-name", &str);
  495. if (!error)
  496. snprintf(data->fw_name, sizeof(data->fw_name),
  497. "silead/%s", str);
  498. else
  499. dev_dbg(dev, "Firmware file name read error. Using default.");
  500. data->pen_supported = device_property_read_bool(dev, "silead,pen-supported");
  501. device_property_read_u32(dev, "silead,pen-resolution-x", &data->pen_x_res);
  502. device_property_read_u32(dev, "silead,pen-resolution-y", &data->pen_y_res);
  503. }
  504. #ifdef CONFIG_ACPI
  505. static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
  506. const struct i2c_device_id *id)
  507. {
  508. const struct acpi_device_id *acpi_id;
  509. struct device *dev = &data->client->dev;
  510. int i;
  511. if (ACPI_HANDLE(dev)) {
  512. acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
  513. if (!acpi_id)
  514. return -ENODEV;
  515. snprintf(data->fw_name, sizeof(data->fw_name),
  516. "silead/%s.fw", acpi_id->id);
  517. for (i = 0; i < strlen(data->fw_name); i++)
  518. data->fw_name[i] = tolower(data->fw_name[i]);
  519. } else {
  520. snprintf(data->fw_name, sizeof(data->fw_name),
  521. "silead/%s.fw", id->name);
  522. }
  523. return 0;
  524. }
  525. #else
  526. static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
  527. const struct i2c_device_id *id)
  528. {
  529. snprintf(data->fw_name, sizeof(data->fw_name),
  530. "silead/%s.fw", id->name);
  531. return 0;
  532. }
  533. #endif
  534. static void silead_disable_regulator(void *arg)
  535. {
  536. struct silead_ts_data *data = arg;
  537. regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
  538. }
  539. static int silead_ts_probe(struct i2c_client *client,
  540. const struct i2c_device_id *id)
  541. {
  542. struct silead_ts_data *data;
  543. struct device *dev = &client->dev;
  544. int error;
  545. if (!i2c_check_functionality(client->adapter,
  546. I2C_FUNC_I2C |
  547. I2C_FUNC_SMBUS_READ_I2C_BLOCK |
  548. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  549. dev_err(dev, "I2C functionality check failed\n");
  550. return -ENXIO;
  551. }
  552. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  553. if (!data)
  554. return -ENOMEM;
  555. i2c_set_clientdata(client, data);
  556. data->client = client;
  557. error = silead_ts_set_default_fw_name(data, id);
  558. if (error)
  559. return error;
  560. silead_ts_read_props(client);
  561. /* We must have the IRQ provided by DT or ACPI subsystem */
  562. if (client->irq <= 0)
  563. return -ENODEV;
  564. data->regulators[0].supply = "vddio";
  565. data->regulators[1].supply = "avdd";
  566. error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
  567. data->regulators);
  568. if (error)
  569. return error;
  570. /*
  571. * Enable regulators at probe and disable them at remove, we need
  572. * to keep the chip powered otherwise it forgets its firmware.
  573. */
  574. error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
  575. data->regulators);
  576. if (error)
  577. return error;
  578. error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
  579. if (error)
  580. return error;
  581. /* Power GPIO pin */
  582. data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
  583. if (IS_ERR(data->gpio_power)) {
  584. if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
  585. dev_err(dev, "Shutdown GPIO request failed\n");
  586. return PTR_ERR(data->gpio_power);
  587. }
  588. error = silead_ts_setup(client);
  589. if (error)
  590. return error;
  591. error = silead_ts_request_input_dev(data);
  592. if (error)
  593. return error;
  594. error = silead_ts_request_pen_input_dev(data);
  595. if (error)
  596. return error;
  597. error = devm_request_threaded_irq(dev, client->irq,
  598. NULL, silead_ts_threaded_irq_handler,
  599. IRQF_ONESHOT, client->name, data);
  600. if (error) {
  601. if (error != -EPROBE_DEFER)
  602. dev_err(dev, "IRQ request failed %d\n", error);
  603. return error;
  604. }
  605. return 0;
  606. }
  607. static int __maybe_unused silead_ts_suspend(struct device *dev)
  608. {
  609. struct i2c_client *client = to_i2c_client(dev);
  610. disable_irq(client->irq);
  611. silead_ts_set_power(client, SILEAD_POWER_OFF);
  612. return 0;
  613. }
  614. static int __maybe_unused silead_ts_resume(struct device *dev)
  615. {
  616. struct i2c_client *client = to_i2c_client(dev);
  617. bool second_try = false;
  618. int error, status;
  619. silead_ts_set_power(client, SILEAD_POWER_ON);
  620. retry:
  621. error = silead_ts_reset(client);
  622. if (error)
  623. return error;
  624. if (second_try) {
  625. error = silead_ts_load_fw(client);
  626. if (error)
  627. return error;
  628. }
  629. error = silead_ts_startup(client);
  630. if (error)
  631. return error;
  632. status = silead_ts_get_status(client);
  633. if (status != SILEAD_STATUS_OK) {
  634. if (!second_try) {
  635. second_try = true;
  636. dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
  637. goto retry;
  638. }
  639. dev_err(dev, "Resume error, status: 0x%02x\n", status);
  640. return -ENODEV;
  641. }
  642. enable_irq(client->irq);
  643. return 0;
  644. }
  645. static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
  646. static const struct i2c_device_id silead_ts_id[] = {
  647. { "gsl1680", 0 },
  648. { "gsl1688", 0 },
  649. { "gsl3670", 0 },
  650. { "gsl3675", 0 },
  651. { "gsl3692", 0 },
  652. { "mssl1680", 0 },
  653. { }
  654. };
  655. MODULE_DEVICE_TABLE(i2c, silead_ts_id);
  656. #ifdef CONFIG_ACPI
  657. static const struct acpi_device_id silead_ts_acpi_match[] = {
  658. { "GSL1680", 0 },
  659. { "GSL1688", 0 },
  660. { "GSL3670", 0 },
  661. { "GSL3675", 0 },
  662. { "GSL3692", 0 },
  663. { "MSSL1680", 0 },
  664. { "MSSL0001", 0 },
  665. { "MSSL0002", 0 },
  666. { "MSSL0017", 0 },
  667. { }
  668. };
  669. MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
  670. #endif
  671. #ifdef CONFIG_OF
  672. static const struct of_device_id silead_ts_of_match[] = {
  673. { .compatible = "silead,gsl1680" },
  674. { .compatible = "silead,gsl1688" },
  675. { .compatible = "silead,gsl3670" },
  676. { .compatible = "silead,gsl3675" },
  677. { .compatible = "silead,gsl3692" },
  678. { },
  679. };
  680. MODULE_DEVICE_TABLE(of, silead_ts_of_match);
  681. #endif
  682. static struct i2c_driver silead_ts_driver = {
  683. .probe = silead_ts_probe,
  684. .id_table = silead_ts_id,
  685. .driver = {
  686. .name = SILEAD_TS_NAME,
  687. .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
  688. .of_match_table = of_match_ptr(silead_ts_of_match),
  689. .pm = &silead_ts_pm,
  690. },
  691. };
  692. module_i2c_driver(silead_ts_driver);
  693. MODULE_AUTHOR("Robert Dolca <[email protected]>");
  694. MODULE_DESCRIPTION("Silead I2C touchscreen driver");
  695. MODULE_LICENSE("GPL");