stmfts.c 20 KB

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