stmfts.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // SPDX-License-Identifier: GPL-2.0
  2. // STMicroelectronics FTS Touchscreen device driver
  3. //
  4. // Copyright (c) 2017 Samsung Electronics Co., Ltd.
  5. // Copyright (c) 2017 Andi Shyti <[email protected]>
  6. #include <linux/delay.h>
  7. #include <linux/i2c.h>
  8. #include <linux/input/mt.h>
  9. #include <linux/input/touchscreen.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regulator/consumer.h>
  16. /* I2C commands */
  17. #define STMFTS_READ_INFO 0x80
  18. #define STMFTS_READ_STATUS 0x84
  19. #define STMFTS_READ_ONE_EVENT 0x85
  20. #define STMFTS_READ_ALL_EVENT 0x86
  21. #define STMFTS_LATEST_EVENT 0x87
  22. #define STMFTS_SLEEP_IN 0x90
  23. #define STMFTS_SLEEP_OUT 0x91
  24. #define STMFTS_MS_MT_SENSE_OFF 0x92
  25. #define STMFTS_MS_MT_SENSE_ON 0x93
  26. #define STMFTS_SS_HOVER_SENSE_OFF 0x94
  27. #define STMFTS_SS_HOVER_SENSE_ON 0x95
  28. #define STMFTS_MS_KEY_SENSE_OFF 0x9a
  29. #define STMFTS_MS_KEY_SENSE_ON 0x9b
  30. #define STMFTS_SYSTEM_RESET 0xa0
  31. #define STMFTS_CLEAR_EVENT_STACK 0xa1
  32. #define STMFTS_FULL_FORCE_CALIBRATION 0xa2
  33. #define STMFTS_MS_CX_TUNING 0xa3
  34. #define STMFTS_SS_CX_TUNING 0xa4
  35. /* events */
  36. #define STMFTS_EV_NO_EVENT 0x00
  37. #define STMFTS_EV_MULTI_TOUCH_DETECTED 0x02
  38. #define STMFTS_EV_MULTI_TOUCH_ENTER 0x03
  39. #define STMFTS_EV_MULTI_TOUCH_LEAVE 0x04
  40. #define STMFTS_EV_MULTI_TOUCH_MOTION 0x05
  41. #define STMFTS_EV_HOVER_ENTER 0x07
  42. #define STMFTS_EV_HOVER_LEAVE 0x08
  43. #define STMFTS_EV_HOVER_MOTION 0x09
  44. #define STMFTS_EV_KEY_STATUS 0x0e
  45. #define STMFTS_EV_ERROR 0x0f
  46. #define STMFTS_EV_CONTROLLER_READY 0x10
  47. #define STMFTS_EV_SLEEP_OUT_CONTROLLER_READY 0x11
  48. #define STMFTS_EV_STATUS 0x16
  49. #define STMFTS_EV_DEBUG 0xdb
  50. /* multi touch related event masks */
  51. #define STMFTS_MASK_EVENT_ID 0x0f
  52. #define STMFTS_MASK_TOUCH_ID 0xf0
  53. #define STMFTS_MASK_LEFT_EVENT 0x0f
  54. #define STMFTS_MASK_X_MSB 0x0f
  55. #define STMFTS_MASK_Y_LSB 0xf0
  56. /* key related event masks */
  57. #define STMFTS_MASK_KEY_NO_TOUCH 0x00
  58. #define STMFTS_MASK_KEY_MENU 0x01
  59. #define STMFTS_MASK_KEY_BACK 0x02
  60. #define STMFTS_EVENT_SIZE 8
  61. #define STMFTS_STACK_DEPTH 32
  62. #define STMFTS_DATA_MAX_SIZE (STMFTS_EVENT_SIZE * STMFTS_STACK_DEPTH)
  63. #define STMFTS_MAX_FINGERS 10
  64. #define STMFTS_DEV_NAME "stmfts"
  65. enum stmfts_regulators {
  66. STMFTS_REGULATOR_VDD,
  67. STMFTS_REGULATOR_AVDD,
  68. };
  69. struct stmfts_data {
  70. struct i2c_client *client;
  71. struct input_dev *input;
  72. struct led_classdev led_cdev;
  73. struct mutex mutex;
  74. struct touchscreen_properties prop;
  75. struct regulator_bulk_data regulators[2];
  76. /*
  77. * Presence of ledvdd will be used also to check
  78. * whether the LED is supported.
  79. */
  80. struct regulator *ledvdd;
  81. u16 chip_id;
  82. u8 chip_ver;
  83. u16 fw_ver;
  84. u8 config_id;
  85. u8 config_ver;
  86. u8 data[STMFTS_DATA_MAX_SIZE];
  87. struct completion cmd_done;
  88. bool use_key;
  89. bool led_status;
  90. bool hover_enabled;
  91. bool running;
  92. };
  93. static int stmfts_brightness_set(struct led_classdev *led_cdev,
  94. enum led_brightness value)
  95. {
  96. struct stmfts_data *sdata = container_of(led_cdev,
  97. struct stmfts_data, led_cdev);
  98. int err;
  99. if (value != sdata->led_status && sdata->ledvdd) {
  100. if (!value) {
  101. regulator_disable(sdata->ledvdd);
  102. } else {
  103. err = regulator_enable(sdata->ledvdd);
  104. if (err) {
  105. dev_warn(&sdata->client->dev,
  106. "failed to disable ledvdd regulator: %d\n",
  107. err);
  108. return err;
  109. }
  110. }
  111. sdata->led_status = value;
  112. }
  113. return 0;
  114. }
  115. static enum led_brightness stmfts_brightness_get(struct led_classdev *led_cdev)
  116. {
  117. struct stmfts_data *sdata = container_of(led_cdev,
  118. struct stmfts_data, led_cdev);
  119. return !!regulator_is_enabled(sdata->ledvdd);
  120. }
  121. /*
  122. * We can't simply use i2c_smbus_read_i2c_block_data because we
  123. * need to read more than 255 bytes (
  124. */
  125. static int stmfts_read_events(struct stmfts_data *sdata)
  126. {
  127. u8 cmd = STMFTS_READ_ALL_EVENT;
  128. struct i2c_msg msgs[2] = {
  129. {
  130. .addr = sdata->client->addr,
  131. .len = 1,
  132. .buf = &cmd,
  133. },
  134. {
  135. .addr = sdata->client->addr,
  136. .flags = I2C_M_RD,
  137. .len = STMFTS_DATA_MAX_SIZE,
  138. .buf = sdata->data,
  139. },
  140. };
  141. int ret;
  142. ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs));
  143. if (ret < 0)
  144. return ret;
  145. return ret == ARRAY_SIZE(msgs) ? 0 : -EIO;
  146. }
  147. static void stmfts_report_contact_event(struct stmfts_data *sdata,
  148. const u8 event[])
  149. {
  150. u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
  151. u16 x = event[1] | ((event[2] & STMFTS_MASK_X_MSB) << 8);
  152. u16 y = (event[2] >> 4) | (event[3] << 4);
  153. u8 maj = event[4];
  154. u8 min = event[5];
  155. u8 orientation = event[6];
  156. u8 area = event[7];
  157. input_mt_slot(sdata->input, slot_id);
  158. input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true);
  159. input_report_abs(sdata->input, ABS_MT_POSITION_X, x);
  160. input_report_abs(sdata->input, ABS_MT_POSITION_Y, y);
  161. input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, maj);
  162. input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, min);
  163. input_report_abs(sdata->input, ABS_MT_PRESSURE, area);
  164. input_report_abs(sdata->input, ABS_MT_ORIENTATION, orientation);
  165. input_sync(sdata->input);
  166. }
  167. static void stmfts_report_contact_release(struct stmfts_data *sdata,
  168. const u8 event[])
  169. {
  170. u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
  171. input_mt_slot(sdata->input, slot_id);
  172. input_mt_report_slot_inactive(sdata->input);
  173. input_sync(sdata->input);
  174. }
  175. static void stmfts_report_hover_event(struct stmfts_data *sdata,
  176. const u8 event[])
  177. {
  178. u16 x = (event[2] << 4) | (event[4] >> 4);
  179. u16 y = (event[3] << 4) | (event[4] & STMFTS_MASK_Y_LSB);
  180. u8 z = event[5];
  181. input_report_abs(sdata->input, ABS_X, x);
  182. input_report_abs(sdata->input, ABS_Y, y);
  183. input_report_abs(sdata->input, ABS_DISTANCE, z);
  184. input_sync(sdata->input);
  185. }
  186. static void stmfts_report_key_event(struct stmfts_data *sdata, const u8 event[])
  187. {
  188. switch (event[2]) {
  189. case 0:
  190. input_report_key(sdata->input, KEY_BACK, 0);
  191. input_report_key(sdata->input, KEY_MENU, 0);
  192. break;
  193. case STMFTS_MASK_KEY_BACK:
  194. input_report_key(sdata->input, KEY_BACK, 1);
  195. break;
  196. case STMFTS_MASK_KEY_MENU:
  197. input_report_key(sdata->input, KEY_MENU, 1);
  198. break;
  199. default:
  200. dev_warn(&sdata->client->dev,
  201. "unknown key event: %#02x\n", event[2]);
  202. break;
  203. }
  204. input_sync(sdata->input);
  205. }
  206. static void stmfts_parse_events(struct stmfts_data *sdata)
  207. {
  208. int i;
  209. for (i = 0; i < STMFTS_STACK_DEPTH; i++) {
  210. u8 *event = &sdata->data[i * STMFTS_EVENT_SIZE];
  211. switch (event[0]) {
  212. case STMFTS_EV_CONTROLLER_READY:
  213. case STMFTS_EV_SLEEP_OUT_CONTROLLER_READY:
  214. case STMFTS_EV_STATUS:
  215. complete(&sdata->cmd_done);
  216. fallthrough;
  217. case STMFTS_EV_NO_EVENT:
  218. case STMFTS_EV_DEBUG:
  219. return;
  220. }
  221. switch (event[0] & STMFTS_MASK_EVENT_ID) {
  222. case STMFTS_EV_MULTI_TOUCH_ENTER:
  223. case STMFTS_EV_MULTI_TOUCH_MOTION:
  224. stmfts_report_contact_event(sdata, event);
  225. break;
  226. case STMFTS_EV_MULTI_TOUCH_LEAVE:
  227. stmfts_report_contact_release(sdata, event);
  228. break;
  229. case STMFTS_EV_HOVER_ENTER:
  230. case STMFTS_EV_HOVER_LEAVE:
  231. case STMFTS_EV_HOVER_MOTION:
  232. stmfts_report_hover_event(sdata, event);
  233. break;
  234. case STMFTS_EV_KEY_STATUS:
  235. stmfts_report_key_event(sdata, event);
  236. break;
  237. case STMFTS_EV_ERROR:
  238. dev_warn(&sdata->client->dev,
  239. "error code: 0x%x%x%x%x%x%x",
  240. event[6], event[5], event[4],
  241. event[3], event[2], event[1]);
  242. break;
  243. default:
  244. dev_err(&sdata->client->dev,
  245. "unknown event %#02x\n", event[0]);
  246. }
  247. }
  248. }
  249. static irqreturn_t stmfts_irq_handler(int irq, void *dev)
  250. {
  251. struct stmfts_data *sdata = dev;
  252. int err;
  253. mutex_lock(&sdata->mutex);
  254. err = stmfts_read_events(sdata);
  255. if (unlikely(err))
  256. dev_err(&sdata->client->dev,
  257. "failed to read events: %d\n", err);
  258. else
  259. stmfts_parse_events(sdata);
  260. mutex_unlock(&sdata->mutex);
  261. return IRQ_HANDLED;
  262. }
  263. static int stmfts_command(struct stmfts_data *sdata, const u8 cmd)
  264. {
  265. int err;
  266. reinit_completion(&sdata->cmd_done);
  267. err = i2c_smbus_write_byte(sdata->client, cmd);
  268. if (err)
  269. return err;
  270. if (!wait_for_completion_timeout(&sdata->cmd_done,
  271. msecs_to_jiffies(1000)))
  272. return -ETIMEDOUT;
  273. return 0;
  274. }
  275. static int stmfts_input_open(struct input_dev *dev)
  276. {
  277. struct stmfts_data *sdata = input_get_drvdata(dev);
  278. int err;
  279. err = pm_runtime_resume_and_get(&sdata->client->dev);
  280. if (err)
  281. return err;
  282. err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
  283. if (err) {
  284. pm_runtime_put_sync(&sdata->client->dev);
  285. return err;
  286. }
  287. mutex_lock(&sdata->mutex);
  288. sdata->running = true;
  289. if (sdata->hover_enabled) {
  290. err = i2c_smbus_write_byte(sdata->client,
  291. STMFTS_SS_HOVER_SENSE_ON);
  292. if (err)
  293. dev_warn(&sdata->client->dev,
  294. "failed to enable hover\n");
  295. }
  296. mutex_unlock(&sdata->mutex);
  297. if (sdata->use_key) {
  298. err = i2c_smbus_write_byte(sdata->client,
  299. STMFTS_MS_KEY_SENSE_ON);
  300. if (err)
  301. /* I can still use only the touch screen */
  302. dev_warn(&sdata->client->dev,
  303. "failed to enable touchkey\n");
  304. }
  305. return 0;
  306. }
  307. static void stmfts_input_close(struct input_dev *dev)
  308. {
  309. struct stmfts_data *sdata = input_get_drvdata(dev);
  310. int err;
  311. err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_OFF);
  312. if (err)
  313. dev_warn(&sdata->client->dev,
  314. "failed to disable touchscreen: %d\n", err);
  315. mutex_lock(&sdata->mutex);
  316. sdata->running = false;
  317. if (sdata->hover_enabled) {
  318. err = i2c_smbus_write_byte(sdata->client,
  319. STMFTS_SS_HOVER_SENSE_OFF);
  320. if (err)
  321. dev_warn(&sdata->client->dev,
  322. "failed to disable hover: %d\n", err);
  323. }
  324. mutex_unlock(&sdata->mutex);
  325. if (sdata->use_key) {
  326. err = i2c_smbus_write_byte(sdata->client,
  327. STMFTS_MS_KEY_SENSE_OFF);
  328. if (err)
  329. dev_warn(&sdata->client->dev,
  330. "failed to disable touchkey: %d\n", err);
  331. }
  332. pm_runtime_put_sync(&sdata->client->dev);
  333. }
  334. static ssize_t stmfts_sysfs_chip_id(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct stmfts_data *sdata = dev_get_drvdata(dev);
  338. return sprintf(buf, "%#x\n", sdata->chip_id);
  339. }
  340. static ssize_t stmfts_sysfs_chip_version(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. struct stmfts_data *sdata = dev_get_drvdata(dev);
  344. return sprintf(buf, "%u\n", sdata->chip_ver);
  345. }
  346. static ssize_t stmfts_sysfs_fw_ver(struct device *dev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. struct stmfts_data *sdata = dev_get_drvdata(dev);
  350. return sprintf(buf, "%u\n", sdata->fw_ver);
  351. }
  352. static ssize_t stmfts_sysfs_config_id(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. struct stmfts_data *sdata = dev_get_drvdata(dev);
  356. return sprintf(buf, "%#x\n", sdata->config_id);
  357. }
  358. static ssize_t stmfts_sysfs_config_version(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. struct stmfts_data *sdata = dev_get_drvdata(dev);
  362. return sprintf(buf, "%u\n", sdata->config_ver);
  363. }
  364. static ssize_t stmfts_sysfs_read_status(struct device *dev,
  365. struct device_attribute *attr, char *buf)
  366. {
  367. struct stmfts_data *sdata = dev_get_drvdata(dev);
  368. u8 status[4];
  369. int err;
  370. err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_STATUS,
  371. sizeof(status), status);
  372. if (err)
  373. return err;
  374. return sprintf(buf, "%#02x\n", status[0]);
  375. }
  376. static ssize_t stmfts_sysfs_hover_enable_read(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct stmfts_data *sdata = dev_get_drvdata(dev);
  380. return sprintf(buf, "%u\n", sdata->hover_enabled);
  381. }
  382. static ssize_t stmfts_sysfs_hover_enable_write(struct device *dev,
  383. struct device_attribute *attr,
  384. const char *buf, size_t len)
  385. {
  386. struct stmfts_data *sdata = dev_get_drvdata(dev);
  387. unsigned long value;
  388. int err = 0;
  389. if (kstrtoul(buf, 0, &value))
  390. return -EINVAL;
  391. mutex_lock(&sdata->mutex);
  392. if (value && sdata->hover_enabled)
  393. goto out;
  394. if (sdata->running)
  395. err = i2c_smbus_write_byte(sdata->client,
  396. value ? STMFTS_SS_HOVER_SENSE_ON :
  397. STMFTS_SS_HOVER_SENSE_OFF);
  398. if (!err)
  399. sdata->hover_enabled = !!value;
  400. out:
  401. mutex_unlock(&sdata->mutex);
  402. return len;
  403. }
  404. static DEVICE_ATTR(chip_id, 0444, stmfts_sysfs_chip_id, NULL);
  405. static DEVICE_ATTR(chip_version, 0444, stmfts_sysfs_chip_version, NULL);
  406. static DEVICE_ATTR(fw_ver, 0444, stmfts_sysfs_fw_ver, NULL);
  407. static DEVICE_ATTR(config_id, 0444, stmfts_sysfs_config_id, NULL);
  408. static DEVICE_ATTR(config_version, 0444, stmfts_sysfs_config_version, NULL);
  409. static DEVICE_ATTR(status, 0444, stmfts_sysfs_read_status, NULL);
  410. static DEVICE_ATTR(hover_enable, 0644, stmfts_sysfs_hover_enable_read,
  411. stmfts_sysfs_hover_enable_write);
  412. static struct attribute *stmfts_sysfs_attrs[] = {
  413. &dev_attr_chip_id.attr,
  414. &dev_attr_chip_version.attr,
  415. &dev_attr_fw_ver.attr,
  416. &dev_attr_config_id.attr,
  417. &dev_attr_config_version.attr,
  418. &dev_attr_status.attr,
  419. &dev_attr_hover_enable.attr,
  420. NULL
  421. };
  422. static struct attribute_group stmfts_attribute_group = {
  423. .attrs = stmfts_sysfs_attrs
  424. };
  425. static int stmfts_power_on(struct stmfts_data *sdata)
  426. {
  427. int err;
  428. u8 reg[8];
  429. err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
  430. sdata->regulators);
  431. if (err)
  432. return err;
  433. /*
  434. * The datasheet does not specify the power on time, but considering
  435. * that the reset time is < 10ms, I sleep 20ms to be sure
  436. */
  437. msleep(20);
  438. err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO,
  439. sizeof(reg), reg);
  440. if (err < 0)
  441. return err;
  442. if (err != sizeof(reg))
  443. return -EIO;
  444. sdata->chip_id = be16_to_cpup((__be16 *)&reg[6]);
  445. sdata->chip_ver = reg[0];
  446. sdata->fw_ver = be16_to_cpup((__be16 *)&reg[2]);
  447. sdata->config_id = reg[4];
  448. sdata->config_ver = reg[5];
  449. enable_irq(sdata->client->irq);
  450. msleep(50);
  451. err = stmfts_command(sdata, STMFTS_SYSTEM_RESET);
  452. if (err)
  453. return err;
  454. err = stmfts_command(sdata, STMFTS_SLEEP_OUT);
  455. if (err)
  456. return err;
  457. /* optional tuning */
  458. err = stmfts_command(sdata, STMFTS_MS_CX_TUNING);
  459. if (err)
  460. dev_warn(&sdata->client->dev,
  461. "failed to perform mutual auto tune: %d\n", err);
  462. /* optional tuning */
  463. err = stmfts_command(sdata, STMFTS_SS_CX_TUNING);
  464. if (err)
  465. dev_warn(&sdata->client->dev,
  466. "failed to perform self auto tune: %d\n", err);
  467. err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION);
  468. if (err)
  469. return err;
  470. /*
  471. * At this point no one is using the touchscreen
  472. * and I don't really care about the return value
  473. */
  474. (void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
  475. return 0;
  476. }
  477. static void stmfts_power_off(void *data)
  478. {
  479. struct stmfts_data *sdata = data;
  480. disable_irq(sdata->client->irq);
  481. regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
  482. sdata->regulators);
  483. }
  484. /* This function is void because I don't want to prevent using the touch key
  485. * only because the LEDs don't get registered
  486. */
  487. static int stmfts_enable_led(struct stmfts_data *sdata)
  488. {
  489. int err;
  490. /* get the regulator for powering the leds on */
  491. sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd");
  492. if (IS_ERR(sdata->ledvdd))
  493. return PTR_ERR(sdata->ledvdd);
  494. sdata->led_cdev.name = STMFTS_DEV_NAME;
  495. sdata->led_cdev.max_brightness = LED_ON;
  496. sdata->led_cdev.brightness = LED_OFF;
  497. sdata->led_cdev.brightness_set_blocking = stmfts_brightness_set;
  498. sdata->led_cdev.brightness_get = stmfts_brightness_get;
  499. err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev);
  500. if (err) {
  501. devm_regulator_put(sdata->ledvdd);
  502. return err;
  503. }
  504. return 0;
  505. }
  506. static int stmfts_probe(struct i2c_client *client,
  507. const struct i2c_device_id *id)
  508. {
  509. int err;
  510. struct stmfts_data *sdata;
  511. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  512. I2C_FUNC_SMBUS_BYTE_DATA |
  513. I2C_FUNC_SMBUS_I2C_BLOCK))
  514. return -ENODEV;
  515. sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
  516. if (!sdata)
  517. return -ENOMEM;
  518. i2c_set_clientdata(client, sdata);
  519. sdata->client = client;
  520. mutex_init(&sdata->mutex);
  521. init_completion(&sdata->cmd_done);
  522. sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd";
  523. sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd";
  524. err = devm_regulator_bulk_get(&client->dev,
  525. ARRAY_SIZE(sdata->regulators),
  526. sdata->regulators);
  527. if (err)
  528. return err;
  529. sdata->input = devm_input_allocate_device(&client->dev);
  530. if (!sdata->input)
  531. return -ENOMEM;
  532. sdata->input->name = STMFTS_DEV_NAME;
  533. sdata->input->id.bustype = BUS_I2C;
  534. sdata->input->open = stmfts_input_open;
  535. sdata->input->close = stmfts_input_close;
  536. input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X);
  537. input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y);
  538. touchscreen_parse_properties(sdata->input, true, &sdata->prop);
  539. input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  540. input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
  541. input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0);
  542. input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  543. input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0);
  544. sdata->use_key = device_property_read_bool(&client->dev,
  545. "touch-key-connected");
  546. if (sdata->use_key) {
  547. input_set_capability(sdata->input, EV_KEY, KEY_MENU);
  548. input_set_capability(sdata->input, EV_KEY, KEY_BACK);
  549. }
  550. err = input_mt_init_slots(sdata->input,
  551. STMFTS_MAX_FINGERS, INPUT_MT_DIRECT);
  552. if (err)
  553. return err;
  554. input_set_drvdata(sdata->input, sdata);
  555. /*
  556. * stmfts_power_on expects interrupt to be disabled, but
  557. * at this point the device is still off and I do not trust
  558. * the status of the irq line that can generate some spurious
  559. * interrupts. To be on the safe side it's better to not enable
  560. * the interrupts during their request.
  561. */
  562. err = devm_request_threaded_irq(&client->dev, client->irq,
  563. NULL, stmfts_irq_handler,
  564. IRQF_ONESHOT | IRQF_NO_AUTOEN,
  565. "stmfts_irq", sdata);
  566. if (err)
  567. return err;
  568. dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n");
  569. err = stmfts_power_on(sdata);
  570. if (err)
  571. return err;
  572. err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata);
  573. if (err)
  574. return err;
  575. err = input_register_device(sdata->input);
  576. if (err)
  577. return err;
  578. if (sdata->use_key) {
  579. err = stmfts_enable_led(sdata);
  580. if (err) {
  581. /*
  582. * Even if the LEDs have failed to be initialized and
  583. * used in the driver, I can still use the device even
  584. * without LEDs. The ledvdd regulator pointer will be
  585. * used as a flag.
  586. */
  587. dev_warn(&client->dev, "unable to use touchkey leds\n");
  588. sdata->ledvdd = NULL;
  589. }
  590. }
  591. err = devm_device_add_group(&client->dev, &stmfts_attribute_group);
  592. if (err)
  593. return err;
  594. pm_runtime_enable(&client->dev);
  595. device_enable_async_suspend(&client->dev);
  596. return 0;
  597. }
  598. static void stmfts_remove(struct i2c_client *client)
  599. {
  600. pm_runtime_disable(&client->dev);
  601. }
  602. static int __maybe_unused stmfts_runtime_suspend(struct device *dev)
  603. {
  604. struct stmfts_data *sdata = dev_get_drvdata(dev);
  605. int ret;
  606. ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
  607. if (ret)
  608. dev_warn(dev, "failed to suspend device: %d\n", ret);
  609. return ret;
  610. }
  611. static int __maybe_unused stmfts_runtime_resume(struct device *dev)
  612. {
  613. struct stmfts_data *sdata = dev_get_drvdata(dev);
  614. int ret;
  615. ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_OUT);
  616. if (ret)
  617. dev_err(dev, "failed to resume device: %d\n", ret);
  618. return ret;
  619. }
  620. static int __maybe_unused stmfts_suspend(struct device *dev)
  621. {
  622. struct stmfts_data *sdata = dev_get_drvdata(dev);
  623. stmfts_power_off(sdata);
  624. return 0;
  625. }
  626. static int __maybe_unused stmfts_resume(struct device *dev)
  627. {
  628. struct stmfts_data *sdata = dev_get_drvdata(dev);
  629. return stmfts_power_on(sdata);
  630. }
  631. static const struct dev_pm_ops stmfts_pm_ops = {
  632. SET_SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume)
  633. SET_RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL)
  634. };
  635. #ifdef CONFIG_OF
  636. static const struct of_device_id stmfts_of_match[] = {
  637. { .compatible = "st,stmfts", },
  638. { },
  639. };
  640. MODULE_DEVICE_TABLE(of, stmfts_of_match);
  641. #endif
  642. static const struct i2c_device_id stmfts_id[] = {
  643. { "stmfts", 0 },
  644. { },
  645. };
  646. MODULE_DEVICE_TABLE(i2c, stmfts_id);
  647. static struct i2c_driver stmfts_driver = {
  648. .driver = {
  649. .name = STMFTS_DEV_NAME,
  650. .of_match_table = of_match_ptr(stmfts_of_match),
  651. .pm = &stmfts_pm_ops,
  652. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  653. },
  654. .probe = stmfts_probe,
  655. .remove = stmfts_remove,
  656. .id_table = stmfts_id,
  657. };
  658. module_i2c_driver(stmfts_driver);
  659. MODULE_AUTHOR("Andi Shyti <[email protected]>");
  660. MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen");
  661. MODULE_LICENSE("GPL v2");