silead.c 22 KB

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