stmfts.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 <andi@etezian.org>
  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 enable 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 256 bytes, which exceeds the 255-byte SMBus block limit.
  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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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. ATTRIBUTE_GROUPS(stmfts_sysfs);
  423. static int stmfts_power_on(struct stmfts_data *sdata)
  424. {
  425. int err;
  426. u8 reg[8];
  427. err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
  428. sdata->regulators);
  429. if (err)
  430. return err;
  431. /*
  432. * The datasheet does not specify the power on time, but considering
  433. * that the reset time is < 10ms, I sleep 20ms to be sure
  434. */
  435. msleep(20);
  436. err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO,
  437. sizeof(reg), reg);
  438. if (err < 0)
  439. return err;
  440. if (err != sizeof(reg))
  441. return -EIO;
  442. sdata->chip_id = be16_to_cpup((__be16 *)&reg[6]);
  443. sdata->chip_ver = reg[0];
  444. sdata->fw_ver = be16_to_cpup((__be16 *)&reg[2]);
  445. sdata->config_id = reg[4];
  446. sdata->config_ver = reg[5];
  447. enable_irq(sdata->client->irq);
  448. msleep(50);
  449. err = stmfts_command(sdata, STMFTS_SYSTEM_RESET);
  450. if (err)
  451. return err;
  452. err = stmfts_command(sdata, STMFTS_SLEEP_OUT);
  453. if (err)
  454. return err;
  455. /* optional tuning */
  456. err = stmfts_command(sdata, STMFTS_MS_CX_TUNING);
  457. if (err)
  458. dev_warn(&sdata->client->dev,
  459. "failed to perform mutual auto tune: %d\n", err);
  460. /* optional tuning */
  461. err = stmfts_command(sdata, STMFTS_SS_CX_TUNING);
  462. if (err)
  463. dev_warn(&sdata->client->dev,
  464. "failed to perform self auto tune: %d\n", err);
  465. err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION);
  466. if (err)
  467. return err;
  468. /*
  469. * At this point no one is using the touchscreen
  470. * and I don't really care about the return value
  471. */
  472. (void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
  473. return 0;
  474. }
  475. static void stmfts_power_off(void *data)
  476. {
  477. struct stmfts_data *sdata = data;
  478. disable_irq(sdata->client->irq);
  479. regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
  480. sdata->regulators);
  481. }
  482. static int stmfts_enable_led(struct stmfts_data *sdata)
  483. {
  484. int err;
  485. /* get the regulator for powering the leds on */
  486. sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd");
  487. if (IS_ERR(sdata->ledvdd))
  488. return PTR_ERR(sdata->ledvdd);
  489. sdata->led_cdev.name = STMFTS_DEV_NAME;
  490. sdata->led_cdev.max_brightness = LED_ON;
  491. sdata->led_cdev.brightness = LED_OFF;
  492. sdata->led_cdev.brightness_set_blocking = stmfts_brightness_set;
  493. sdata->led_cdev.brightness_get = stmfts_brightness_get;
  494. err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev);
  495. if (err) {
  496. devm_regulator_put(sdata->ledvdd);
  497. return err;
  498. }
  499. return 0;
  500. }
  501. static int stmfts_probe(struct i2c_client *client)
  502. {
  503. int err;
  504. struct stmfts_data *sdata;
  505. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  506. I2C_FUNC_SMBUS_BYTE_DATA |
  507. I2C_FUNC_SMBUS_I2C_BLOCK))
  508. return -ENODEV;
  509. sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
  510. if (!sdata)
  511. return -ENOMEM;
  512. i2c_set_clientdata(client, sdata);
  513. sdata->client = client;
  514. mutex_init(&sdata->mutex);
  515. init_completion(&sdata->cmd_done);
  516. sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd";
  517. sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd";
  518. err = devm_regulator_bulk_get(&client->dev,
  519. ARRAY_SIZE(sdata->regulators),
  520. sdata->regulators);
  521. if (err)
  522. return err;
  523. sdata->input = devm_input_allocate_device(&client->dev);
  524. if (!sdata->input)
  525. return -ENOMEM;
  526. sdata->input->name = STMFTS_DEV_NAME;
  527. sdata->input->id.bustype = BUS_I2C;
  528. sdata->input->open = stmfts_input_open;
  529. sdata->input->close = stmfts_input_close;
  530. input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X);
  531. input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y);
  532. touchscreen_parse_properties(sdata->input, true, &sdata->prop);
  533. input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  534. input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
  535. input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0);
  536. input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  537. input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0);
  538. sdata->use_key = device_property_read_bool(&client->dev,
  539. "touch-key-connected");
  540. if (sdata->use_key) {
  541. input_set_capability(sdata->input, EV_KEY, KEY_MENU);
  542. input_set_capability(sdata->input, EV_KEY, KEY_BACK);
  543. }
  544. err = input_mt_init_slots(sdata->input,
  545. STMFTS_MAX_FINGERS, INPUT_MT_DIRECT);
  546. if (err)
  547. return err;
  548. input_set_drvdata(sdata->input, sdata);
  549. /*
  550. * stmfts_power_on expects interrupt to be disabled, but
  551. * at this point the device is still off and I do not trust
  552. * the status of the irq line that can generate some spurious
  553. * interrupts. To be on the safe side it's better to not enable
  554. * the interrupts during their request.
  555. */
  556. err = devm_request_threaded_irq(&client->dev, client->irq,
  557. NULL, stmfts_irq_handler,
  558. IRQF_ONESHOT | IRQF_NO_AUTOEN,
  559. "stmfts_irq", sdata);
  560. if (err)
  561. return err;
  562. dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n");
  563. err = stmfts_power_on(sdata);
  564. if (err)
  565. return err;
  566. err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata);
  567. if (err)
  568. return err;
  569. err = input_register_device(sdata->input);
  570. if (err)
  571. return err;
  572. if (sdata->use_key) {
  573. err = stmfts_enable_led(sdata);
  574. if (err) {
  575. /*
  576. * Even if the LEDs have failed to be initialized and
  577. * used in the driver, I can still use the device even
  578. * without LEDs. The ledvdd regulator pointer will be
  579. * used as a flag.
  580. */
  581. dev_warn(&client->dev, "unable to use touchkey leds\n");
  582. sdata->ledvdd = NULL;
  583. }
  584. }
  585. pm_runtime_enable(&client->dev);
  586. device_enable_async_suspend(&client->dev);
  587. return 0;
  588. }
  589. static void stmfts_remove(struct i2c_client *client)
  590. {
  591. pm_runtime_disable(&client->dev);
  592. }
  593. static int stmfts_runtime_suspend(struct device *dev)
  594. {
  595. struct stmfts_data *sdata = dev_get_drvdata(dev);
  596. int ret;
  597. ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
  598. if (ret)
  599. dev_warn(dev, "failed to suspend device: %d\n", ret);
  600. return ret;
  601. }
  602. static int stmfts_runtime_resume(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_OUT);
  607. if (ret)
  608. dev_err(dev, "failed to resume device: %d\n", ret);
  609. return ret;
  610. }
  611. static int stmfts_suspend(struct device *dev)
  612. {
  613. struct stmfts_data *sdata = dev_get_drvdata(dev);
  614. stmfts_power_off(sdata);
  615. return 0;
  616. }
  617. static int stmfts_resume(struct device *dev)
  618. {
  619. struct stmfts_data *sdata = dev_get_drvdata(dev);
  620. return stmfts_power_on(sdata);
  621. }
  622. static const struct dev_pm_ops stmfts_pm_ops = {
  623. SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume)
  624. RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL)
  625. };
  626. #ifdef CONFIG_OF
  627. static const struct of_device_id stmfts_of_match[] = {
  628. { .compatible = "st,stmfts", },
  629. { },
  630. };
  631. MODULE_DEVICE_TABLE(of, stmfts_of_match);
  632. #endif
  633. static const struct i2c_device_id stmfts_id[] = {
  634. { "stmfts" },
  635. { }
  636. };
  637. MODULE_DEVICE_TABLE(i2c, stmfts_id);
  638. static struct i2c_driver stmfts_driver = {
  639. .driver = {
  640. .name = STMFTS_DEV_NAME,
  641. .dev_groups = stmfts_sysfs_groups,
  642. .of_match_table = of_match_ptr(stmfts_of_match),
  643. .pm = pm_ptr(&stmfts_pm_ops),
  644. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  645. },
  646. .probe = stmfts_probe,
  647. .remove = stmfts_remove,
  648. .id_table = stmfts_id,
  649. };
  650. module_i2c_driver(stmfts_driver);
  651. MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
  652. MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen");
  653. MODULE_LICENSE("GPL v2");