mms114.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/of.h>
  12. #include <linux/i2c.h>
  13. #include <linux/i2c/mms114.h>
  14. #include <linux/input/mt.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. /* Write only registers */
  19. #define MMS114_MODE_CONTROL 0x01
  20. #define MMS114_OPERATION_MODE_MASK 0xE
  21. #define MMS114_ACTIVE (1 << 1)
  22. #define MMS114_XY_RESOLUTION_H 0x02
  23. #define MMS114_X_RESOLUTION 0x03
  24. #define MMS114_Y_RESOLUTION 0x04
  25. #define MMS114_CONTACT_THRESHOLD 0x05
  26. #define MMS114_MOVING_THRESHOLD 0x06
  27. /* Read only registers */
  28. #define MMS114_PACKET_SIZE 0x0F
  29. #define MMS114_INFOMATION 0x10
  30. #define MMS114_TSP_REV 0xF0
  31. /* Minimum delay time is 50us between stop and start signal of i2c */
  32. #define MMS114_I2C_DELAY 50
  33. /* 200ms needs after power on */
  34. #define MMS114_POWERON_DELAY 200
  35. /* Touchscreen absolute values */
  36. #define MMS114_MAX_AREA 0xff
  37. #define MMS114_MAX_TOUCH 10
  38. #define MMS114_PACKET_NUM 8
  39. /* Touch type */
  40. #define MMS114_TYPE_NONE 0
  41. #define MMS114_TYPE_TOUCHSCREEN 1
  42. #define MMS114_TYPE_TOUCHKEY 2
  43. struct mms114_data {
  44. struct i2c_client *client;
  45. struct input_dev *input_dev;
  46. struct regulator *core_reg;
  47. struct regulator *io_reg;
  48. const struct mms114_platform_data *pdata;
  49. /* Use cache data for mode control register(write only) */
  50. u8 cache_mode_control;
  51. };
  52. struct mms114_touch {
  53. u8 id:4, reserved_bit4:1, type:2, pressed:1;
  54. u8 x_hi:4, y_hi:4;
  55. u8 x_lo;
  56. u8 y_lo;
  57. u8 width;
  58. u8 strength;
  59. u8 reserved[2];
  60. } __packed;
  61. static int __mms114_read_reg(struct mms114_data *data, unsigned int reg,
  62. unsigned int len, u8 *val)
  63. {
  64. struct i2c_client *client = data->client;
  65. struct i2c_msg xfer[2];
  66. u8 buf = reg & 0xff;
  67. int error;
  68. if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL)
  69. BUG();
  70. /* Write register: use repeated start */
  71. xfer[0].addr = client->addr;
  72. xfer[0].flags = I2C_M_TEN | I2C_M_NOSTART;
  73. xfer[0].len = 1;
  74. xfer[0].buf = &buf;
  75. /* Read data */
  76. xfer[1].addr = client->addr;
  77. xfer[1].flags = I2C_M_RD;
  78. xfer[1].len = len;
  79. xfer[1].buf = val;
  80. error = i2c_transfer(client->adapter, xfer, 2);
  81. if (error != 2) {
  82. dev_err(&client->dev,
  83. "%s: i2c transfer failed (%d)\n", __func__, error);
  84. return error < 0 ? error : -EIO;
  85. }
  86. udelay(MMS114_I2C_DELAY);
  87. return 0;
  88. }
  89. static int mms114_read_reg(struct mms114_data *data, unsigned int reg)
  90. {
  91. u8 val;
  92. int error;
  93. if (reg == MMS114_MODE_CONTROL)
  94. return data->cache_mode_control;
  95. error = __mms114_read_reg(data, reg, 1, &val);
  96. return error < 0 ? error : val;
  97. }
  98. static int mms114_write_reg(struct mms114_data *data, unsigned int reg,
  99. unsigned int val)
  100. {
  101. struct i2c_client *client = data->client;
  102. u8 buf[2];
  103. int error;
  104. buf[0] = reg & 0xff;
  105. buf[1] = val & 0xff;
  106. error = i2c_master_send(client, buf, 2);
  107. if (error != 2) {
  108. dev_err(&client->dev,
  109. "%s: i2c send failed (%d)\n", __func__, error);
  110. return error < 0 ? error : -EIO;
  111. }
  112. udelay(MMS114_I2C_DELAY);
  113. if (reg == MMS114_MODE_CONTROL)
  114. data->cache_mode_control = val;
  115. return 0;
  116. }
  117. static void mms114_process_mt(struct mms114_data *data, struct mms114_touch *touch)
  118. {
  119. const struct mms114_platform_data *pdata = data->pdata;
  120. struct i2c_client *client = data->client;
  121. struct input_dev *input_dev = data->input_dev;
  122. unsigned int id;
  123. unsigned int x;
  124. unsigned int y;
  125. if (touch->id > MMS114_MAX_TOUCH) {
  126. dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id);
  127. return;
  128. }
  129. if (touch->type != MMS114_TYPE_TOUCHSCREEN) {
  130. dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type);
  131. return;
  132. }
  133. id = touch->id - 1;
  134. x = touch->x_lo | touch->x_hi << 8;
  135. y = touch->y_lo | touch->y_hi << 8;
  136. if (x > pdata->x_size || y > pdata->y_size) {
  137. dev_dbg(&client->dev,
  138. "Wrong touch coordinates (%d, %d)\n", x, y);
  139. return;
  140. }
  141. if (pdata->x_invert)
  142. x = pdata->x_size - x;
  143. if (pdata->y_invert)
  144. y = pdata->y_size - y;
  145. dev_dbg(&client->dev,
  146. "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
  147. id, touch->type, touch->pressed,
  148. x, y, touch->width, touch->strength);
  149. input_mt_slot(input_dev, id);
  150. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, touch->pressed);
  151. if (touch->pressed) {
  152. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, touch->width);
  153. input_report_abs(input_dev, ABS_MT_POSITION_X, x);
  154. input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
  155. input_report_abs(input_dev, ABS_MT_PRESSURE, touch->strength);
  156. }
  157. }
  158. static irqreturn_t mms114_interrupt(int irq, void *dev_id)
  159. {
  160. struct mms114_data *data = dev_id;
  161. struct input_dev *input_dev = data->input_dev;
  162. struct mms114_touch touch[MMS114_MAX_TOUCH];
  163. int packet_size;
  164. int touch_size;
  165. int index;
  166. int error;
  167. mutex_lock(&input_dev->mutex);
  168. if (!input_dev->users) {
  169. mutex_unlock(&input_dev->mutex);
  170. goto out;
  171. }
  172. mutex_unlock(&input_dev->mutex);
  173. packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
  174. if (packet_size <= 0)
  175. goto out;
  176. touch_size = packet_size / MMS114_PACKET_NUM;
  177. error = __mms114_read_reg(data, MMS114_INFOMATION, packet_size,
  178. (u8 *)touch);
  179. if (error < 0)
  180. goto out;
  181. for (index = 0; index < touch_size; index++)
  182. mms114_process_mt(data, touch + index);
  183. input_mt_report_pointer_emulation(data->input_dev, true);
  184. input_sync(data->input_dev);
  185. out:
  186. return IRQ_HANDLED;
  187. }
  188. static int mms114_set_active(struct mms114_data *data, bool active)
  189. {
  190. int val;
  191. val = mms114_read_reg(data, MMS114_MODE_CONTROL);
  192. if (val < 0)
  193. return val;
  194. val &= ~MMS114_OPERATION_MODE_MASK;
  195. /* If active is false, sleep mode */
  196. if (active)
  197. val |= MMS114_ACTIVE;
  198. return mms114_write_reg(data, MMS114_MODE_CONTROL, val);
  199. }
  200. static int mms114_get_version(struct mms114_data *data)
  201. {
  202. struct device *dev = &data->client->dev;
  203. u8 buf[6];
  204. int error;
  205. error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
  206. if (error < 0)
  207. return error;
  208. dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
  209. buf[0], buf[1], buf[3]);
  210. return 0;
  211. }
  212. static int mms114_setup_regs(struct mms114_data *data)
  213. {
  214. const struct mms114_platform_data *pdata = data->pdata;
  215. int val;
  216. int error;
  217. error = mms114_get_version(data);
  218. if (error < 0)
  219. return error;
  220. error = mms114_set_active(data, true);
  221. if (error < 0)
  222. return error;
  223. val = (pdata->x_size >> 8) & 0xf;
  224. val |= ((pdata->y_size >> 8) & 0xf) << 4;
  225. error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val);
  226. if (error < 0)
  227. return error;
  228. val = pdata->x_size & 0xff;
  229. error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
  230. if (error < 0)
  231. return error;
  232. val = pdata->y_size & 0xff;
  233. error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
  234. if (error < 0)
  235. return error;
  236. if (pdata->contact_threshold) {
  237. error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
  238. pdata->contact_threshold);
  239. if (error < 0)
  240. return error;
  241. }
  242. if (pdata->moving_threshold) {
  243. error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
  244. pdata->moving_threshold);
  245. if (error < 0)
  246. return error;
  247. }
  248. return 0;
  249. }
  250. static int mms114_start(struct mms114_data *data)
  251. {
  252. struct i2c_client *client = data->client;
  253. int error;
  254. error = regulator_enable(data->core_reg);
  255. if (error) {
  256. dev_err(&client->dev, "Failed to enable avdd: %d\n", error);
  257. return error;
  258. }
  259. error = regulator_enable(data->io_reg);
  260. if (error) {
  261. dev_err(&client->dev, "Failed to enable vdd: %d\n", error);
  262. regulator_disable(data->core_reg);
  263. return error;
  264. }
  265. mdelay(MMS114_POWERON_DELAY);
  266. error = mms114_setup_regs(data);
  267. if (error < 0) {
  268. regulator_disable(data->io_reg);
  269. regulator_disable(data->core_reg);
  270. return error;
  271. }
  272. if (data->pdata->cfg_pin)
  273. data->pdata->cfg_pin(true);
  274. enable_irq(client->irq);
  275. return 0;
  276. }
  277. static void mms114_stop(struct mms114_data *data)
  278. {
  279. struct i2c_client *client = data->client;
  280. int error;
  281. disable_irq(client->irq);
  282. if (data->pdata->cfg_pin)
  283. data->pdata->cfg_pin(false);
  284. error = regulator_disable(data->io_reg);
  285. if (error)
  286. dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
  287. error = regulator_disable(data->core_reg);
  288. if (error)
  289. dev_warn(&client->dev, "Failed to disable avdd: %d\n", error);
  290. }
  291. static int mms114_input_open(struct input_dev *dev)
  292. {
  293. struct mms114_data *data = input_get_drvdata(dev);
  294. return mms114_start(data);
  295. }
  296. static void mms114_input_close(struct input_dev *dev)
  297. {
  298. struct mms114_data *data = input_get_drvdata(dev);
  299. mms114_stop(data);
  300. }
  301. #ifdef CONFIG_OF
  302. static struct mms114_platform_data *mms114_parse_dt(struct device *dev)
  303. {
  304. struct mms114_platform_data *pdata;
  305. struct device_node *np = dev->of_node;
  306. if (!np)
  307. return NULL;
  308. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  309. if (!pdata) {
  310. dev_err(dev, "failed to allocate platform data\n");
  311. return NULL;
  312. }
  313. if (of_property_read_u32(np, "x-size", &pdata->x_size)) {
  314. dev_err(dev, "failed to get x-size property\n");
  315. return NULL;
  316. }
  317. if (of_property_read_u32(np, "y-size", &pdata->y_size)) {
  318. dev_err(dev, "failed to get y-size property\n");
  319. return NULL;
  320. }
  321. of_property_read_u32(np, "contact-threshold",
  322. &pdata->contact_threshold);
  323. of_property_read_u32(np, "moving-threshold",
  324. &pdata->moving_threshold);
  325. if (of_find_property(np, "x-invert", NULL))
  326. pdata->x_invert = true;
  327. if (of_find_property(np, "y-invert", NULL))
  328. pdata->y_invert = true;
  329. return pdata;
  330. }
  331. #else
  332. static inline struct mms114_platform_data *mms114_parse_dt(struct device *dev)
  333. {
  334. return NULL;
  335. }
  336. #endif
  337. static int mms114_probe(struct i2c_client *client,
  338. const struct i2c_device_id *id)
  339. {
  340. const struct mms114_platform_data *pdata;
  341. struct mms114_data *data;
  342. struct input_dev *input_dev;
  343. int error;
  344. pdata = dev_get_platdata(&client->dev);
  345. if (!pdata)
  346. pdata = mms114_parse_dt(&client->dev);
  347. if (!pdata) {
  348. dev_err(&client->dev, "Need platform data\n");
  349. return -EINVAL;
  350. }
  351. if (!i2c_check_functionality(client->adapter,
  352. I2C_FUNC_PROTOCOL_MANGLING)) {
  353. dev_err(&client->dev,
  354. "Need i2c bus that supports protocol mangling\n");
  355. return -ENODEV;
  356. }
  357. data = devm_kzalloc(&client->dev, sizeof(struct mms114_data),
  358. GFP_KERNEL);
  359. input_dev = devm_input_allocate_device(&client->dev);
  360. if (!data || !input_dev) {
  361. dev_err(&client->dev, "Failed to allocate memory\n");
  362. return -ENOMEM;
  363. }
  364. data->client = client;
  365. data->input_dev = input_dev;
  366. data->pdata = pdata;
  367. input_dev->name = "MELFAS MMS114 Touchscreen";
  368. input_dev->id.bustype = BUS_I2C;
  369. input_dev->dev.parent = &client->dev;
  370. input_dev->open = mms114_input_open;
  371. input_dev->close = mms114_input_close;
  372. __set_bit(EV_ABS, input_dev->evbit);
  373. __set_bit(EV_KEY, input_dev->evbit);
  374. __set_bit(BTN_TOUCH, input_dev->keybit);
  375. input_set_abs_params(input_dev, ABS_X, 0, data->pdata->x_size, 0, 0);
  376. input_set_abs_params(input_dev, ABS_Y, 0, data->pdata->y_size, 0, 0);
  377. /* For multi touch */
  378. input_mt_init_slots(input_dev, MMS114_MAX_TOUCH, 0);
  379. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  380. 0, MMS114_MAX_AREA, 0, 0);
  381. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  382. 0, data->pdata->x_size, 0, 0);
  383. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  384. 0, data->pdata->y_size, 0, 0);
  385. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
  386. input_set_drvdata(input_dev, data);
  387. i2c_set_clientdata(client, data);
  388. data->core_reg = devm_regulator_get(&client->dev, "avdd");
  389. if (IS_ERR(data->core_reg)) {
  390. error = PTR_ERR(data->core_reg);
  391. dev_err(&client->dev,
  392. "Unable to get the Core regulator (%d)\n", error);
  393. return error;
  394. }
  395. data->io_reg = devm_regulator_get(&client->dev, "vdd");
  396. if (IS_ERR(data->io_reg)) {
  397. error = PTR_ERR(data->io_reg);
  398. dev_err(&client->dev,
  399. "Unable to get the IO regulator (%d)\n", error);
  400. return error;
  401. }
  402. error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  403. mms114_interrupt, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  404. dev_name(&client->dev), data);
  405. if (error) {
  406. dev_err(&client->dev, "Failed to register interrupt\n");
  407. return error;
  408. }
  409. disable_irq(client->irq);
  410. error = input_register_device(data->input_dev);
  411. if (error) {
  412. dev_err(&client->dev, "Failed to register input device\n");
  413. return error;
  414. }
  415. return 0;
  416. }
  417. static int __maybe_unused mms114_suspend(struct device *dev)
  418. {
  419. struct i2c_client *client = to_i2c_client(dev);
  420. struct mms114_data *data = i2c_get_clientdata(client);
  421. struct input_dev *input_dev = data->input_dev;
  422. int id;
  423. /* Release all touch */
  424. for (id = 0; id < MMS114_MAX_TOUCH; id++) {
  425. input_mt_slot(input_dev, id);
  426. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
  427. }
  428. input_mt_report_pointer_emulation(input_dev, true);
  429. input_sync(input_dev);
  430. mutex_lock(&input_dev->mutex);
  431. if (input_dev->users)
  432. mms114_stop(data);
  433. mutex_unlock(&input_dev->mutex);
  434. return 0;
  435. }
  436. static int __maybe_unused mms114_resume(struct device *dev)
  437. {
  438. struct i2c_client *client = to_i2c_client(dev);
  439. struct mms114_data *data = i2c_get_clientdata(client);
  440. struct input_dev *input_dev = data->input_dev;
  441. int error;
  442. mutex_lock(&input_dev->mutex);
  443. if (input_dev->users) {
  444. error = mms114_start(data);
  445. if (error < 0) {
  446. mutex_unlock(&input_dev->mutex);
  447. return error;
  448. }
  449. }
  450. mutex_unlock(&input_dev->mutex);
  451. return 0;
  452. }
  453. static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
  454. static const struct i2c_device_id mms114_id[] = {
  455. { "mms114", 0 },
  456. { }
  457. };
  458. MODULE_DEVICE_TABLE(i2c, mms114_id);
  459. #ifdef CONFIG_OF
  460. static const struct of_device_id mms114_dt_match[] = {
  461. { .compatible = "melfas,mms114" },
  462. { }
  463. };
  464. MODULE_DEVICE_TABLE(of, mms114_dt_match);
  465. #endif
  466. static struct i2c_driver mms114_driver = {
  467. .driver = {
  468. .name = "mms114",
  469. .pm = &mms114_pm_ops,
  470. .of_match_table = of_match_ptr(mms114_dt_match),
  471. },
  472. .probe = mms114_probe,
  473. .id_table = mms114_id,
  474. };
  475. module_i2c_driver(mms114_driver);
  476. /* Module information */
  477. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  478. MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
  479. MODULE_LICENSE("GPL");