melfas_mip4.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * MELFAS MIP4 Touchscreen
  3. *
  4. * Copyright (C) 2016 MELFAS Inc.
  5. *
  6. * Author : Sangwon Jee <jeesw@melfas.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/acpi.h>
  19. #include <linux/delay.h>
  20. #include <linux/firmware.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/i2c.h>
  23. #include <linux/input.h>
  24. #include <linux/input/mt.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/slab.h>
  29. #include <asm/unaligned.h>
  30. #define MIP4_DEVICE_NAME "mip4_ts"
  31. /*****************************************************************
  32. * Protocol
  33. * Version : MIP 4.0 Rev 4.6
  34. *****************************************************************/
  35. /* Address */
  36. #define MIP4_R0_BOOT 0x00
  37. #define MIP4_R1_BOOT_MODE 0x01
  38. #define MIP4_R1_BOOT_BUF_ADDR 0x10
  39. #define MIP4_R1_BOOT_STATUS 0x20
  40. #define MIP4_R1_BOOT_CMD 0x30
  41. #define MIP4_R1_BOOT_TARGET_ADDR 0x40
  42. #define MIP4_R1_BOOT_SIZE 0x44
  43. #define MIP4_R0_INFO 0x01
  44. #define MIP4_R1_INFO_PRODUCT_NAME 0x00
  45. #define MIP4_R1_INFO_RESOLUTION_X 0x10
  46. #define MIP4_R1_INFO_RESOLUTION_Y 0x12
  47. #define MIP4_R1_INFO_NODE_NUM_X 0x14
  48. #define MIP4_R1_INFO_NODE_NUM_Y 0x15
  49. #define MIP4_R1_INFO_KEY_NUM 0x16
  50. #define MIP4_R1_INFO_PRESSURE_NUM 0x17
  51. #define MIP4_R1_INFO_LENGTH_X 0x18
  52. #define MIP4_R1_INFO_LENGTH_Y 0x1A
  53. #define MIP4_R1_INFO_PPM_X 0x1C
  54. #define MIP4_R1_INFO_PPM_Y 0x1D
  55. #define MIP4_R1_INFO_VERSION_BOOT 0x20
  56. #define MIP4_R1_INFO_VERSION_CORE 0x22
  57. #define MIP4_R1_INFO_VERSION_APP 0x24
  58. #define MIP4_R1_INFO_VERSION_PARAM 0x26
  59. #define MIP4_R1_INFO_SECT_BOOT_START 0x30
  60. #define MIP4_R1_INFO_SECT_BOOT_END 0x31
  61. #define MIP4_R1_INFO_SECT_CORE_START 0x32
  62. #define MIP4_R1_INFO_SECT_CORE_END 0x33
  63. #define MIP4_R1_INFO_SECT_APP_START 0x34
  64. #define MIP4_R1_INFO_SECT_APP_END 0x35
  65. #define MIP4_R1_INFO_SECT_PARAM_START 0x36
  66. #define MIP4_R1_INFO_SECT_PARAM_END 0x37
  67. #define MIP4_R1_INFO_BUILD_DATE 0x40
  68. #define MIP4_R1_INFO_BUILD_TIME 0x44
  69. #define MIP4_R1_INFO_CHECKSUM_PRECALC 0x48
  70. #define MIP4_R1_INFO_CHECKSUM_REALTIME 0x4A
  71. #define MIP4_R1_INFO_PROTOCOL_NAME 0x50
  72. #define MIP4_R1_INFO_PROTOCOL_VERSION 0x58
  73. #define MIP4_R1_INFO_IC_ID 0x70
  74. #define MIP4_R1_INFO_IC_NAME 0x71
  75. #define MIP4_R1_INFO_IC_VENDOR_ID 0x75
  76. #define MIP4_R1_INFO_IC_HW_CATEGORY 0x77
  77. #define MIP4_R1_INFO_CONTACT_THD_SCR 0x78
  78. #define MIP4_R1_INFO_CONTACT_THD_KEY 0x7A
  79. #define MIP4_R0_EVENT 0x02
  80. #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00
  81. #define MIP4_R1_EVENT_FORMAT 0x04
  82. #define MIP4_R1_EVENT_SIZE 0x06
  83. #define MIP4_R1_EVENT_PACKET_INFO 0x10
  84. #define MIP4_R1_EVENT_PACKET_DATA 0x11
  85. #define MIP4_R0_CTRL 0x06
  86. #define MIP4_R1_CTRL_READY_STATUS 0x00
  87. #define MIP4_R1_CTRL_EVENT_READY 0x01
  88. #define MIP4_R1_CTRL_MODE 0x10
  89. #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11
  90. #define MIP4_R1_CTRL_RECALIBRATE 0x12
  91. #define MIP4_R1_CTRL_POWER_STATE 0x13
  92. #define MIP4_R1_CTRL_GESTURE_TYPE 0x14
  93. #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18
  94. #define MIP4_R1_CTRL_CHARGER_MODE 0x19
  95. #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A
  96. #define MIP4_R1_CTRL_WINDOW_MODE 0x1B
  97. #define MIP4_R1_CTRL_PALM_REJECTION 0x1C
  98. #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D
  99. #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E
  100. #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F
  101. #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20
  102. #define MIP4_R1_CTRL_PALM_EVENT 0x22
  103. #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23
  104. /* Value */
  105. #define MIP4_BOOT_MODE_BOOT 0x01
  106. #define MIP4_BOOT_MODE_APP 0x02
  107. #define MIP4_BOOT_STATUS_BUSY 0x05
  108. #define MIP4_BOOT_STATUS_ERROR 0x0E
  109. #define MIP4_BOOT_STATUS_DONE 0xA0
  110. #define MIP4_BOOT_CMD_MASS_ERASE 0x15
  111. #define MIP4_BOOT_CMD_PROGRAM 0x54
  112. #define MIP4_BOOT_CMD_ERASE 0x8F
  113. #define MIP4_BOOT_CMD_WRITE 0xA5
  114. #define MIP4_BOOT_CMD_READ 0xC2
  115. #define MIP4_EVENT_INPUT_TYPE_KEY 0
  116. #define MIP4_EVENT_INPUT_TYPE_SCREEN 1
  117. #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2
  118. #define I2C_RETRY_COUNT 3 /* 2~ */
  119. #define MIP4_BUF_SIZE 128
  120. #define MIP4_MAX_FINGERS 10
  121. #define MIP4_MAX_KEYS 4
  122. #define MIP4_TOUCH_MAJOR_MIN 0
  123. #define MIP4_TOUCH_MAJOR_MAX 255
  124. #define MIP4_TOUCH_MINOR_MIN 0
  125. #define MIP4_TOUCH_MINOR_MAX 255
  126. #define MIP4_PRESSURE_MIN 0
  127. #define MIP4_PRESSURE_MAX 255
  128. #define MIP4_FW_NAME "/*(DEBLOBBED)*/"
  129. #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */
  130. struct mip4_fw_version {
  131. u16 boot;
  132. u16 core;
  133. u16 app;
  134. u16 param;
  135. };
  136. struct mip4_ts {
  137. struct i2c_client *client;
  138. struct input_dev *input;
  139. struct gpio_desc *gpio_ce;
  140. char phys[32];
  141. char product_name[16];
  142. char ic_name[4];
  143. unsigned int max_x;
  144. unsigned int max_y;
  145. u8 node_x;
  146. u8 node_y;
  147. u8 node_key;
  148. unsigned int ppm_x;
  149. unsigned int ppm_y;
  150. struct mip4_fw_version fw_version;
  151. unsigned int event_size;
  152. unsigned int event_format;
  153. unsigned int key_num;
  154. unsigned short key_code[MIP4_MAX_KEYS];
  155. bool wake_irq_enabled;
  156. u8 buf[MIP4_BUF_SIZE];
  157. };
  158. static int mip4_i2c_xfer(struct mip4_ts *ts,
  159. char *write_buf, unsigned int write_len,
  160. char *read_buf, unsigned int read_len)
  161. {
  162. struct i2c_msg msg[] = {
  163. {
  164. .addr = ts->client->addr,
  165. .flags = 0,
  166. .buf = write_buf,
  167. .len = write_len,
  168. }, {
  169. .addr = ts->client->addr,
  170. .flags = I2C_M_RD,
  171. .buf = read_buf,
  172. .len = read_len,
  173. },
  174. };
  175. int retry = I2C_RETRY_COUNT;
  176. int res;
  177. int error;
  178. do {
  179. res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  180. if (res == ARRAY_SIZE(msg))
  181. return 0;
  182. error = res < 0 ? res : -EIO;
  183. dev_err(&ts->client->dev,
  184. "%s - i2c_transfer failed: %d (%d)\n",
  185. __func__, error, res);
  186. } while (--retry);
  187. return error;
  188. }
  189. static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
  190. {
  191. v->boot = get_unaligned_le16(buf + 0);
  192. v->core = get_unaligned_le16(buf + 2);
  193. v->app = get_unaligned_le16(buf + 4);
  194. v->param = get_unaligned_le16(buf + 6);
  195. }
  196. /*
  197. * Read chip firmware version
  198. */
  199. static int mip4_get_fw_version(struct mip4_ts *ts)
  200. {
  201. u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
  202. u8 buf[sizeof(ts->fw_version)];
  203. int error;
  204. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
  205. if (error) {
  206. memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
  207. return error;
  208. }
  209. mip4_parse_fw_version(buf, &ts->fw_version);
  210. return 0;
  211. }
  212. /*
  213. * Fetch device characteristics
  214. */
  215. static int mip4_query_device(struct mip4_ts *ts)
  216. {
  217. int error;
  218. u8 cmd[2];
  219. u8 buf[14];
  220. /* Product name */
  221. cmd[0] = MIP4_R0_INFO;
  222. cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
  223. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
  224. ts->product_name, sizeof(ts->product_name));
  225. if (error)
  226. dev_warn(&ts->client->dev,
  227. "Failed to retrieve product name: %d\n", error);
  228. else
  229. dev_dbg(&ts->client->dev, "product name: %.*s\n",
  230. (int)sizeof(ts->product_name), ts->product_name);
  231. /* IC name */
  232. cmd[0] = MIP4_R0_INFO;
  233. cmd[1] = MIP4_R1_INFO_IC_NAME;
  234. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
  235. ts->ic_name, sizeof(ts->ic_name));
  236. if (error)
  237. dev_warn(&ts->client->dev,
  238. "Failed to retrieve IC name: %d\n", error);
  239. else
  240. dev_dbg(&ts->client->dev, "IC name: %.*s\n",
  241. (int)sizeof(ts->ic_name), ts->ic_name);
  242. /* Firmware version */
  243. error = mip4_get_fw_version(ts);
  244. if (error)
  245. dev_warn(&ts->client->dev,
  246. "Failed to retrieve FW version: %d\n", error);
  247. else
  248. dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
  249. ts->fw_version.boot, ts->fw_version.core,
  250. ts->fw_version.app, ts->fw_version.param);
  251. /* Resolution */
  252. cmd[0] = MIP4_R0_INFO;
  253. cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
  254. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
  255. if (error) {
  256. dev_warn(&ts->client->dev,
  257. "Failed to retrieve touchscreen parameters: %d\n",
  258. error);
  259. } else {
  260. ts->max_x = get_unaligned_le16(&buf[0]);
  261. ts->max_y = get_unaligned_le16(&buf[2]);
  262. dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
  263. ts->max_x, ts->max_y);
  264. ts->node_x = buf[4];
  265. ts->node_y = buf[5];
  266. ts->node_key = buf[6];
  267. dev_dbg(&ts->client->dev,
  268. "node_x: %d, node_y: %d, node_key: %d\n",
  269. ts->node_x, ts->node_y, ts->node_key);
  270. ts->ppm_x = buf[12];
  271. ts->ppm_y = buf[13];
  272. dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
  273. ts->ppm_x, ts->ppm_y);
  274. /* Key ts */
  275. if (ts->node_key > 0)
  276. ts->key_num = ts->node_key;
  277. }
  278. /* Protocol */
  279. cmd[0] = MIP4_R0_EVENT;
  280. cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
  281. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
  282. if (error) {
  283. dev_warn(&ts->client->dev,
  284. "Failed to retrieve device type: %d\n", error);
  285. ts->event_format = 0xff;
  286. } else {
  287. ts->event_format = get_unaligned_le16(&buf[4]);
  288. ts->event_size = buf[6];
  289. dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
  290. ts->event_format, ts->event_size);
  291. if (ts->event_format == 2 || ts->event_format > 3)
  292. dev_warn(&ts->client->dev,
  293. "Unknown event format %d\n", ts->event_format);
  294. }
  295. return 0;
  296. }
  297. static int mip4_power_on(struct mip4_ts *ts)
  298. {
  299. if (ts->gpio_ce) {
  300. gpiod_set_value_cansleep(ts->gpio_ce, 1);
  301. /* Booting delay : 200~300ms */
  302. usleep_range(200 * 1000, 300 * 1000);
  303. }
  304. return 0;
  305. }
  306. static void mip4_power_off(struct mip4_ts *ts)
  307. {
  308. if (ts->gpio_ce)
  309. gpiod_set_value_cansleep(ts->gpio_ce, 0);
  310. }
  311. /*
  312. * Clear touch input event status
  313. */
  314. static void mip4_clear_input(struct mip4_ts *ts)
  315. {
  316. int i;
  317. /* Screen */
  318. for (i = 0; i < MIP4_MAX_FINGERS; i++) {
  319. input_mt_slot(ts->input, i);
  320. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
  321. }
  322. /* Keys */
  323. for (i = 0; i < ts->key_num; i++)
  324. input_report_key(ts->input, ts->key_code[i], 0);
  325. input_sync(ts->input);
  326. }
  327. static int mip4_enable(struct mip4_ts *ts)
  328. {
  329. int error;
  330. error = mip4_power_on(ts);
  331. if (error)
  332. return error;
  333. enable_irq(ts->client->irq);
  334. return 0;
  335. }
  336. static void mip4_disable(struct mip4_ts *ts)
  337. {
  338. disable_irq(ts->client->irq);
  339. mip4_power_off(ts);
  340. mip4_clear_input(ts);
  341. }
  342. /*****************************************************************
  343. * Input handling
  344. *****************************************************************/
  345. static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
  346. {
  347. u8 key;
  348. bool down;
  349. switch (ts->event_format) {
  350. case 0:
  351. case 1:
  352. key = packet[0] & 0x0F;
  353. down = packet[0] & 0x80;
  354. break;
  355. case 3:
  356. default:
  357. key = packet[0] & 0x0F;
  358. down = packet[1] & 0x01;
  359. break;
  360. }
  361. /* Report key event */
  362. if (key >= 1 && key <= ts->key_num) {
  363. unsigned short keycode = ts->key_code[key - 1];
  364. dev_dbg(&ts->client->dev,
  365. "Key - ID: %d, keycode: %d, state: %d\n",
  366. key, keycode, down);
  367. input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
  368. input_report_key(ts->input, keycode, down);
  369. } else {
  370. dev_err(&ts->client->dev, "Unknown key: %d\n", key);
  371. }
  372. }
  373. static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
  374. {
  375. int id;
  376. bool hover;
  377. bool palm;
  378. bool state;
  379. u16 x, y;
  380. u8 pressure_stage = 0;
  381. u8 pressure;
  382. u8 size;
  383. u8 touch_major;
  384. u8 touch_minor;
  385. switch (ts->event_format) {
  386. case 0:
  387. case 1:
  388. /* Touch only */
  389. state = packet[0] & BIT(7);
  390. hover = packet[0] & BIT(5);
  391. palm = packet[0] & BIT(4);
  392. id = (packet[0] & 0x0F) - 1;
  393. x = ((packet[1] & 0x0F) << 8) | packet[2];
  394. y = (((packet[1] >> 4) & 0x0F) << 8) |
  395. packet[3];
  396. pressure = packet[4];
  397. size = packet[5];
  398. if (ts->event_format == 0) {
  399. touch_major = packet[5];
  400. touch_minor = packet[5];
  401. } else {
  402. touch_major = packet[6];
  403. touch_minor = packet[7];
  404. }
  405. break;
  406. case 3:
  407. default:
  408. /* Touch + Force(Pressure) */
  409. id = (packet[0] & 0x0F) - 1;
  410. hover = packet[1] & BIT(2);
  411. palm = packet[1] & BIT(1);
  412. state = packet[1] & BIT(0);
  413. x = ((packet[2] & 0x0F) << 8) | packet[3];
  414. y = (((packet[2] >> 4) & 0x0F) << 8) |
  415. packet[4];
  416. size = packet[6];
  417. pressure_stage = (packet[7] & 0xF0) >> 4;
  418. pressure = ((packet[7] & 0x0F) << 8) |
  419. packet[8];
  420. touch_major = packet[9];
  421. touch_minor = packet[10];
  422. break;
  423. }
  424. dev_dbg(&ts->client->dev,
  425. "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
  426. id, state, x, y, pressure);
  427. if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
  428. dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
  429. } else if (state) {
  430. /* Press or Move event */
  431. input_mt_slot(ts->input, id);
  432. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
  433. input_report_abs(ts->input, ABS_MT_POSITION_X, x);
  434. input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
  435. input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
  436. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
  437. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
  438. } else {
  439. /* Release event */
  440. input_mt_slot(ts->input, id);
  441. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
  442. }
  443. input_mt_sync_frame(ts->input);
  444. }
  445. static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
  446. {
  447. u8 type;
  448. switch (ts->event_format) {
  449. case 0:
  450. case 1:
  451. type = (packet[0] & 0x40) >> 6;
  452. break;
  453. case 3:
  454. type = (packet[0] & 0xF0) >> 4;
  455. break;
  456. default:
  457. /* Should not happen unless we have corrupted firmware */
  458. return -EINVAL;
  459. }
  460. dev_dbg(&ts->client->dev, "Type: %d\n", type);
  461. /* Report input event */
  462. switch (type) {
  463. case MIP4_EVENT_INPUT_TYPE_KEY:
  464. mip4_report_keys(ts, packet);
  465. break;
  466. case MIP4_EVENT_INPUT_TYPE_SCREEN:
  467. mip4_report_touch(ts, packet);
  468. break;
  469. default:
  470. dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
  471. break;
  472. }
  473. return 0;
  474. }
  475. static irqreturn_t mip4_interrupt(int irq, void *dev_id)
  476. {
  477. struct mip4_ts *ts = dev_id;
  478. struct i2c_client *client = ts->client;
  479. unsigned int i;
  480. int error;
  481. u8 cmd[2];
  482. u8 size;
  483. bool alert;
  484. /* Read packet info */
  485. cmd[0] = MIP4_R0_EVENT;
  486. cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
  487. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
  488. if (error) {
  489. dev_err(&client->dev,
  490. "Failed to read packet info: %d\n", error);
  491. goto out;
  492. }
  493. size = ts->buf[0] & 0x7F;
  494. alert = ts->buf[0] & BIT(7);
  495. dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
  496. /* Check size */
  497. if (!size) {
  498. dev_err(&client->dev, "Empty packet\n");
  499. goto out;
  500. }
  501. /* Read packet data */
  502. cmd[0] = MIP4_R0_EVENT;
  503. cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
  504. error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
  505. if (error) {
  506. dev_err(&client->dev,
  507. "Failed to read packet data: %d\n", error);
  508. goto out;
  509. }
  510. if (alert) {
  511. dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
  512. } else {
  513. for (i = 0; i < size; i += ts->event_size) {
  514. error = mip4_handle_packet(ts, &ts->buf[i]);
  515. if (error)
  516. break;
  517. }
  518. input_sync(ts->input);
  519. }
  520. out:
  521. return IRQ_HANDLED;
  522. }
  523. static int mip4_input_open(struct input_dev *dev)
  524. {
  525. struct mip4_ts *ts = input_get_drvdata(dev);
  526. return mip4_enable(ts);
  527. }
  528. static void mip4_input_close(struct input_dev *dev)
  529. {
  530. struct mip4_ts *ts = input_get_drvdata(dev);
  531. mip4_disable(ts);
  532. }
  533. /*****************************************************************
  534. * Firmware update
  535. *****************************************************************/
  536. /* Firmware Info */
  537. #define MIP4_BL_PAGE_SIZE 512 /* 512 */
  538. #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
  539. /*
  540. * Firmware binary tail info
  541. */
  542. struct mip4_bin_tail {
  543. u8 tail_mark[4];
  544. u8 chip_name[4];
  545. __le32 bin_start_addr;
  546. __le32 bin_length;
  547. __le16 ver_boot;
  548. __le16 ver_core;
  549. __le16 ver_app;
  550. __le16 ver_param;
  551. u8 boot_start;
  552. u8 boot_end;
  553. u8 core_start;
  554. u8 core_end;
  555. u8 app_start;
  556. u8 app_end;
  557. u8 param_start;
  558. u8 param_end;
  559. u8 checksum_type;
  560. u8 hw_category;
  561. __le16 param_id;
  562. __le32 param_length;
  563. __le32 build_date;
  564. __le32 build_time;
  565. __le32 reserved1;
  566. __le32 reserved2;
  567. __le16 reserved3;
  568. __le16 tail_size;
  569. __le32 crc;
  570. } __packed;
  571. #define MIP4_BIN_TAIL_MARK "MBT\001"
  572. #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
  573. /*
  574. * Bootloader - Read status
  575. */
  576. static int mip4_bl_read_status(struct mip4_ts *ts)
  577. {
  578. u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
  579. u8 result;
  580. struct i2c_msg msg[] = {
  581. {
  582. .addr = ts->client->addr,
  583. .flags = 0,
  584. .buf = cmd,
  585. .len = sizeof(cmd),
  586. }, {
  587. .addr = ts->client->addr,
  588. .flags = I2C_M_RD,
  589. .buf = &result,
  590. .len = sizeof(result),
  591. },
  592. };
  593. int ret;
  594. int error;
  595. int retry = 1000;
  596. do {
  597. ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  598. if (ret != ARRAY_SIZE(msg)) {
  599. error = ret < 0 ? ret : -EIO;
  600. dev_err(&ts->client->dev,
  601. "Failed to read bootloader status: %d\n",
  602. error);
  603. return error;
  604. }
  605. switch (result) {
  606. case MIP4_BOOT_STATUS_DONE:
  607. dev_dbg(&ts->client->dev, "%s - done\n", __func__);
  608. return 0;
  609. case MIP4_BOOT_STATUS_ERROR:
  610. dev_err(&ts->client->dev, "Bootloader failure\n");
  611. return -EIO;
  612. case MIP4_BOOT_STATUS_BUSY:
  613. dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
  614. error = -EBUSY;
  615. break;
  616. default:
  617. dev_err(&ts->client->dev,
  618. "Unexpected bootloader status: %#02x\n",
  619. result);
  620. error = -EINVAL;
  621. break;
  622. }
  623. usleep_range(1000, 2000);
  624. } while (--retry);
  625. return error;
  626. }
  627. /*
  628. * Bootloader - Change mode
  629. */
  630. static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
  631. {
  632. u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
  633. u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
  634. u8 result;
  635. struct i2c_msg msg[] = {
  636. {
  637. .addr = ts->client->addr,
  638. .flags = 0,
  639. .buf = mode_read_cmd,
  640. .len = sizeof(mode_read_cmd),
  641. }, {
  642. .addr = ts->client->addr,
  643. .flags = I2C_M_RD,
  644. .buf = &result,
  645. .len = sizeof(result),
  646. },
  647. };
  648. int retry = 10;
  649. int ret;
  650. int error;
  651. do {
  652. /* Send mode change command */
  653. ret = i2c_master_send(ts->client,
  654. mode_chg_cmd, sizeof(mode_chg_cmd));
  655. if (ret != sizeof(mode_chg_cmd)) {
  656. error = ret < 0 ? ret : -EIO;
  657. dev_err(&ts->client->dev,
  658. "Failed to send %d mode change: %d (%d)\n",
  659. mode, error, ret);
  660. return error;
  661. }
  662. dev_dbg(&ts->client->dev,
  663. "Sent mode change request (mode: %d)\n", mode);
  664. /* Wait */
  665. msleep(1000);
  666. /* Verify target mode */
  667. ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  668. if (ret != ARRAY_SIZE(msg)) {
  669. error = ret < 0 ? ret : -EIO;
  670. dev_err(&ts->client->dev,
  671. "Failed to read device mode: %d\n", error);
  672. return error;
  673. }
  674. dev_dbg(&ts->client->dev,
  675. "Current device mode: %d, want: %d\n", result, mode);
  676. if (result == mode)
  677. return 0;
  678. } while (--retry);
  679. return -EIO;
  680. }
  681. /*
  682. * Bootloader - Start bootloader mode
  683. */
  684. static int mip4_bl_enter(struct mip4_ts *ts)
  685. {
  686. return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
  687. }
  688. /*
  689. * Bootloader - Exit bootloader mode
  690. */
  691. static int mip4_bl_exit(struct mip4_ts *ts)
  692. {
  693. return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
  694. }
  695. static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
  696. {
  697. u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
  698. u8 result[sizeof(u16)];
  699. struct i2c_msg msg[] = {
  700. {
  701. .addr = ts->client->addr,
  702. .flags = 0,
  703. .buf = cmd,
  704. .len = sizeof(cmd),
  705. }, {
  706. .addr = ts->client->addr,
  707. .flags = I2C_M_RD,
  708. .buf = result,
  709. .len = sizeof(result),
  710. },
  711. };
  712. int ret;
  713. int error;
  714. ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  715. if (ret != ARRAY_SIZE(msg)) {
  716. error = ret < 0 ? ret : -EIO;
  717. dev_err(&ts->client->dev,
  718. "Failed to retrieve bootloader buffer address: %d\n",
  719. error);
  720. return error;
  721. }
  722. *buf_addr = get_unaligned_le16(result);
  723. dev_dbg(&ts->client->dev,
  724. "Bootloader buffer address %#04x\n", *buf_addr);
  725. return 0;
  726. }
  727. static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
  728. const u8 *data, int length, u16 buf_addr)
  729. {
  730. u8 cmd[6];
  731. u8 *data_buf;
  732. u16 buf_offset;
  733. int ret;
  734. int error;
  735. dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
  736. offset, length);
  737. if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
  738. dev_err(&ts->client->dev,
  739. "Invalid page length: %d\n", length);
  740. return -EINVAL;
  741. }
  742. data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
  743. if (!data_buf)
  744. return -ENOMEM;
  745. /* Addr */
  746. cmd[0] = MIP4_R0_BOOT;
  747. cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
  748. put_unaligned_le32(offset, &cmd[2]);
  749. ret = i2c_master_send(ts->client, cmd, 6);
  750. if (ret != 6) {
  751. error = ret < 0 ? ret : -EIO;
  752. dev_err(&ts->client->dev,
  753. "Failed to send write page address: %d\n", error);
  754. goto out;
  755. }
  756. /* Size */
  757. cmd[0] = MIP4_R0_BOOT;
  758. cmd[1] = MIP4_R1_BOOT_SIZE;
  759. put_unaligned_le32(length, &cmd[2]);
  760. ret = i2c_master_send(ts->client, cmd, 6);
  761. if (ret != 6) {
  762. error = ret < 0 ? ret : -EIO;
  763. dev_err(&ts->client->dev,
  764. "Failed to send write page size: %d\n", error);
  765. goto out;
  766. }
  767. /* Data */
  768. for (buf_offset = 0;
  769. buf_offset < length;
  770. buf_offset += MIP4_BL_PACKET_SIZE) {
  771. dev_dbg(&ts->client->dev,
  772. "writing chunk at %#04x (size %d)\n",
  773. buf_offset, MIP4_BL_PACKET_SIZE);
  774. put_unaligned_be16(buf_addr + buf_offset, data_buf);
  775. memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
  776. ret = i2c_master_send(ts->client,
  777. data_buf, 2 + MIP4_BL_PACKET_SIZE);
  778. if (ret != 2 + MIP4_BL_PACKET_SIZE) {
  779. error = ret < 0 ? ret : -EIO;
  780. dev_err(&ts->client->dev,
  781. "Failed to read chunk at %#04x (size %d): %d\n",
  782. buf_offset, MIP4_BL_PACKET_SIZE, error);
  783. goto out;
  784. }
  785. }
  786. /* Command */
  787. cmd[0] = MIP4_R0_BOOT;
  788. cmd[1] = MIP4_R1_BOOT_CMD;
  789. cmd[2] = MIP4_BOOT_CMD_PROGRAM;
  790. ret = i2c_master_send(ts->client, cmd, 3);
  791. if (ret != 3) {
  792. error = ret < 0 ? ret : -EIO;
  793. dev_err(&ts->client->dev,
  794. "Failed to send 'write' command: %d\n", error);
  795. goto out;
  796. }
  797. /* Status */
  798. error = mip4_bl_read_status(ts);
  799. out:
  800. kfree(data_buf);
  801. return error ? error : 0;
  802. }
  803. static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
  804. const u8 *data, int length, int buf_addr)
  805. {
  806. u8 cmd[8];
  807. u8 *read_buf;
  808. int buf_offset;
  809. struct i2c_msg msg[] = {
  810. {
  811. .addr = ts->client->addr,
  812. .flags = 0,
  813. .buf = cmd,
  814. .len = 2,
  815. }, {
  816. .addr = ts->client->addr,
  817. .flags = I2C_M_RD,
  818. .len = MIP4_BL_PACKET_SIZE,
  819. },
  820. };
  821. int ret;
  822. int error;
  823. dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
  824. offset, length);
  825. /* Addr */
  826. cmd[0] = MIP4_R0_BOOT;
  827. cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
  828. put_unaligned_le32(offset, &cmd[2]);
  829. ret = i2c_master_send(ts->client, cmd, 6);
  830. if (ret != 6) {
  831. error = ret < 0 ? ret : -EIO;
  832. dev_err(&ts->client->dev,
  833. "Failed to send read page address: %d\n", error);
  834. return error;
  835. }
  836. /* Size */
  837. cmd[0] = MIP4_R0_BOOT;
  838. cmd[1] = MIP4_R1_BOOT_SIZE;
  839. put_unaligned_le32(length, &cmd[2]);
  840. ret = i2c_master_send(ts->client, cmd, 6);
  841. if (ret != 6) {
  842. error = ret < 0 ? ret : -EIO;
  843. dev_err(&ts->client->dev,
  844. "Failed to send read page size: %d\n", error);
  845. return error;
  846. }
  847. /* Command */
  848. cmd[0] = MIP4_R0_BOOT;
  849. cmd[1] = MIP4_R1_BOOT_CMD;
  850. cmd[2] = MIP4_BOOT_CMD_READ;
  851. ret = i2c_master_send(ts->client, cmd, 3);
  852. if (ret != 3) {
  853. error = ret < 0 ? ret : -EIO;
  854. dev_err(&ts->client->dev,
  855. "Failed to send 'read' command: %d\n", error);
  856. return error;
  857. }
  858. /* Status */
  859. error = mip4_bl_read_status(ts);
  860. if (error)
  861. return error;
  862. /* Read */
  863. msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
  864. if (!read_buf)
  865. return -ENOMEM;
  866. for (buf_offset = 0;
  867. buf_offset < length;
  868. buf_offset += MIP4_BL_PACKET_SIZE) {
  869. dev_dbg(&ts->client->dev,
  870. "reading chunk at %#04x (size %d)\n",
  871. buf_offset, MIP4_BL_PACKET_SIZE);
  872. put_unaligned_be16(buf_addr + buf_offset, cmd);
  873. ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  874. if (ret != ARRAY_SIZE(msg)) {
  875. error = ret < 0 ? ret : -EIO;
  876. dev_err(&ts->client->dev,
  877. "Failed to read chunk at %#04x (size %d): %d\n",
  878. buf_offset, MIP4_BL_PACKET_SIZE, error);
  879. break;
  880. }
  881. if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
  882. dev_err(&ts->client->dev,
  883. "Failed to validate chunk at %#04x (size %d)\n",
  884. buf_offset, MIP4_BL_PACKET_SIZE);
  885. #if MIP4_FW_UPDATE_DEBUG
  886. print_hex_dump(KERN_DEBUG,
  887. MIP4_DEVICE_NAME " F/W File: ",
  888. DUMP_PREFIX_OFFSET, 16, 1,
  889. data + offset, MIP4_BL_PACKET_SIZE,
  890. false);
  891. print_hex_dump(KERN_DEBUG,
  892. MIP4_DEVICE_NAME " F/W Chip: ",
  893. DUMP_PREFIX_OFFSET, 16, 1,
  894. read_buf, MIP4_BL_PAGE_SIZE, false);
  895. #endif
  896. error = -EINVAL;
  897. break;
  898. }
  899. }
  900. kfree(read_buf);
  901. return error ? error : 0;
  902. }
  903. /*
  904. * Flash chip firmware
  905. */
  906. static int mip4_flash_fw(struct mip4_ts *ts,
  907. const u8 *fw_data, u32 fw_size, u32 fw_offset)
  908. {
  909. struct i2c_client *client = ts->client;
  910. int offset;
  911. u16 buf_addr;
  912. int error, error2;
  913. /* Enter bootloader mode */
  914. dev_dbg(&client->dev, "Entering bootloader mode\n");
  915. error = mip4_bl_enter(ts);
  916. if (error) {
  917. dev_err(&client->dev,
  918. "Failed to enter bootloader mode: %d\n",
  919. error);
  920. return error;
  921. }
  922. /* Read info */
  923. error = mip4_bl_get_address(ts, &buf_addr);
  924. if (error)
  925. goto exit_bl;
  926. /* Program & Verify */
  927. dev_dbg(&client->dev,
  928. "Program & Verify, page size: %d, packet size: %d\n",
  929. MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
  930. for (offset = fw_offset;
  931. offset < fw_offset + fw_size;
  932. offset += MIP4_BL_PAGE_SIZE) {
  933. /* Program */
  934. error = mip4_bl_program_page(ts, offset, fw_data + offset,
  935. MIP4_BL_PAGE_SIZE, buf_addr);
  936. if (error)
  937. break;
  938. /* Verify */
  939. error = mip4_bl_verify_page(ts, offset, fw_data + offset,
  940. MIP4_BL_PAGE_SIZE, buf_addr);
  941. if (error)
  942. break;
  943. }
  944. exit_bl:
  945. /* Exit bootloader mode */
  946. dev_dbg(&client->dev, "Exiting bootloader mode\n");
  947. error2 = mip4_bl_exit(ts);
  948. if (error2) {
  949. dev_err(&client->dev,
  950. "Failed to exit bootloader mode: %d\n", error2);
  951. if (!error)
  952. error = error2;
  953. }
  954. /* Reset chip */
  955. mip4_power_off(ts);
  956. mip4_power_on(ts);
  957. mip4_query_device(ts);
  958. /* Refresh device parameters */
  959. input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
  960. input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
  961. input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
  962. input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
  963. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
  964. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
  965. input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
  966. input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
  967. return error ? error : 0;
  968. }
  969. static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
  970. u32 *fw_offset_start, u32 *fw_size,
  971. const struct mip4_bin_tail **pfw_info)
  972. {
  973. const struct mip4_bin_tail *fw_info;
  974. struct mip4_fw_version fw_version;
  975. u16 tail_size;
  976. if (fw->size < MIP4_BIN_TAIL_SIZE) {
  977. dev_err(&ts->client->dev,
  978. "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
  979. MIP4_BIN_TAIL_SIZE, fw->size);
  980. return -EINVAL;
  981. }
  982. fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
  983. #if MIP4_FW_UPDATE_DEBUG
  984. print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
  985. DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
  986. #endif
  987. tail_size = get_unaligned_le16(&fw_info->tail_size);
  988. if (tail_size != MIP4_BIN_TAIL_SIZE) {
  989. dev_err(&ts->client->dev,
  990. "wrong tail size: %d (expected %zd)\n",
  991. tail_size, MIP4_BIN_TAIL_SIZE);
  992. return -EINVAL;
  993. }
  994. /* Check bin format */
  995. if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
  996. sizeof(fw_info->tail_mark))) {
  997. dev_err(&ts->client->dev,
  998. "unable to locate tail marker (%*ph vs %*ph)\n",
  999. (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
  1000. (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
  1001. return -EINVAL;
  1002. }
  1003. *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
  1004. *fw_size = get_unaligned_le32(&fw_info->bin_length);
  1005. dev_dbg(&ts->client->dev,
  1006. "F/W Data offset: %#08x, size: %d\n",
  1007. *fw_offset_start, *fw_size);
  1008. if (*fw_size % MIP4_BL_PAGE_SIZE) {
  1009. dev_err(&ts->client->dev,
  1010. "encoded fw length %d is not multiple of pages (%d)\n",
  1011. *fw_size, MIP4_BL_PAGE_SIZE);
  1012. return -EINVAL;
  1013. }
  1014. if (fw->size != *fw_offset_start + *fw_size) {
  1015. dev_err(&ts->client->dev,
  1016. "Wrong firmware size, expected %d bytes, got %zd\n",
  1017. *fw_offset_start + *fw_size, fw->size);
  1018. return -EINVAL;
  1019. }
  1020. mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
  1021. dev_dbg(&ts->client->dev,
  1022. "F/W file version %04X %04X %04X %04X\n",
  1023. fw_version.boot, fw_version.core,
  1024. fw_version.app, fw_version.param);
  1025. dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
  1026. ts->fw_version.boot, ts->fw_version.core,
  1027. ts->fw_version.app, ts->fw_version.param);
  1028. /* Check F/W type */
  1029. if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
  1030. fw_version.core == 0xEEEE &&
  1031. fw_version.app == 0xEEEE &&
  1032. fw_version.param == 0xEEEE) {
  1033. dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
  1034. } else if (fw_version.boot == 0xEEEE &&
  1035. fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
  1036. fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
  1037. fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
  1038. dev_dbg(&ts->client->dev, "F/W type: Main\n");
  1039. } else {
  1040. dev_err(&ts->client->dev, "Wrong firmware type\n");
  1041. return -EINVAL;
  1042. }
  1043. return 0;
  1044. }
  1045. static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
  1046. {
  1047. const struct mip4_bin_tail *fw_info;
  1048. u32 fw_start_offset;
  1049. u32 fw_size;
  1050. int retires = 3;
  1051. int error;
  1052. error = mip4_parse_firmware(ts, fw,
  1053. &fw_start_offset, &fw_size, &fw_info);
  1054. if (error)
  1055. return error;
  1056. if (ts->input->users) {
  1057. disable_irq(ts->client->irq);
  1058. } else {
  1059. error = mip4_power_on(ts);
  1060. if (error)
  1061. return error;
  1062. }
  1063. /* Update firmware */
  1064. do {
  1065. error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
  1066. if (!error)
  1067. break;
  1068. } while (--retires);
  1069. if (error)
  1070. dev_err(&ts->client->dev,
  1071. "Failed to flash firmware: %d\n", error);
  1072. /* Enable IRQ */
  1073. if (ts->input->users)
  1074. enable_irq(ts->client->irq);
  1075. else
  1076. mip4_power_off(ts);
  1077. return error ? error : 0;
  1078. }
  1079. static ssize_t mip4_sysfs_fw_update(struct device *dev,
  1080. struct device_attribute *attr,
  1081. const char *buf, size_t count)
  1082. {
  1083. struct i2c_client *client = to_i2c_client(dev);
  1084. struct mip4_ts *ts = i2c_get_clientdata(client);
  1085. const struct firmware *fw;
  1086. int error;
  1087. error = reject_firmware(&fw, MIP4_FW_NAME, dev);
  1088. if (error) {
  1089. dev_err(&ts->client->dev,
  1090. "Failed to retrieve firmware %s: %d\n",
  1091. MIP4_FW_NAME, error);
  1092. return error;
  1093. }
  1094. /*
  1095. * Take input mutex to prevent racing with itself and also with
  1096. * userspace opening and closing the device and also suspend/resume
  1097. * transitions.
  1098. */
  1099. mutex_lock(&ts->input->mutex);
  1100. error = mip4_execute_fw_update(ts, fw);
  1101. mutex_unlock(&ts->input->mutex);
  1102. release_firmware(fw);
  1103. if (error) {
  1104. dev_err(&ts->client->dev,
  1105. "Firmware update failed: %d\n", error);
  1106. return error;
  1107. }
  1108. return count;
  1109. }
  1110. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
  1111. static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
  1112. struct device_attribute *attr,
  1113. char *buf)
  1114. {
  1115. struct i2c_client *client = to_i2c_client(dev);
  1116. struct mip4_ts *ts = i2c_get_clientdata(client);
  1117. size_t count;
  1118. /* Take lock to prevent racing with firmware update */
  1119. mutex_lock(&ts->input->mutex);
  1120. count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
  1121. ts->fw_version.boot, ts->fw_version.core,
  1122. ts->fw_version.app, ts->fw_version.param);
  1123. mutex_unlock(&ts->input->mutex);
  1124. return count;
  1125. }
  1126. static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
  1127. static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
  1128. struct device_attribute *attr,
  1129. char *buf)
  1130. {
  1131. struct i2c_client *client = to_i2c_client(dev);
  1132. struct mip4_ts *ts = i2c_get_clientdata(client);
  1133. size_t count;
  1134. /* Take lock to prevent racing with firmware update */
  1135. mutex_lock(&ts->input->mutex);
  1136. /*
  1137. * product_name shows the name or version of the hardware
  1138. * paired with current firmware in the chip.
  1139. */
  1140. count = snprintf(buf, PAGE_SIZE, "%.*s\n",
  1141. (int)sizeof(ts->product_name), ts->product_name);
  1142. mutex_unlock(&ts->input->mutex);
  1143. return count;
  1144. }
  1145. static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
  1146. static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
  1147. struct device_attribute *attr,
  1148. char *buf)
  1149. {
  1150. struct i2c_client *client = to_i2c_client(dev);
  1151. struct mip4_ts *ts = i2c_get_clientdata(client);
  1152. size_t count;
  1153. mutex_lock(&ts->input->mutex);
  1154. count = snprintf(buf, PAGE_SIZE, "%.*s\n",
  1155. (int)sizeof(ts->ic_name), ts->ic_name);
  1156. mutex_unlock(&ts->input->mutex);
  1157. return count;
  1158. }
  1159. static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
  1160. static struct attribute *mip4_attrs[] = {
  1161. &dev_attr_fw_version.attr,
  1162. &dev_attr_hw_version.attr,
  1163. &dev_attr_ic_name.attr,
  1164. &dev_attr_update_fw.attr,
  1165. NULL,
  1166. };
  1167. static const struct attribute_group mip4_attr_group = {
  1168. .attrs = mip4_attrs,
  1169. };
  1170. static void mip4_sysfs_remove(void *_data)
  1171. {
  1172. struct mip4_ts *ts = _data;
  1173. sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
  1174. }
  1175. static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1176. {
  1177. struct mip4_ts *ts;
  1178. struct input_dev *input;
  1179. int error;
  1180. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1181. dev_err(&client->dev, "Not supported I2C adapter\n");
  1182. return -ENXIO;
  1183. }
  1184. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  1185. if (!ts)
  1186. return -ENOMEM;
  1187. input = devm_input_allocate_device(&client->dev);
  1188. if (!input)
  1189. return -ENOMEM;
  1190. ts->client = client;
  1191. ts->input = input;
  1192. snprintf(ts->phys, sizeof(ts->phys),
  1193. "%s/input0", dev_name(&client->dev));
  1194. ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
  1195. "ce", GPIOD_OUT_LOW);
  1196. if (IS_ERR(ts->gpio_ce)) {
  1197. error = PTR_ERR(ts->gpio_ce);
  1198. if (error != EPROBE_DEFER)
  1199. dev_err(&client->dev,
  1200. "Failed to get gpio: %d\n", error);
  1201. return error;
  1202. }
  1203. error = mip4_power_on(ts);
  1204. if (error)
  1205. return error;
  1206. error = mip4_query_device(ts);
  1207. mip4_power_off(ts);
  1208. if (error)
  1209. return error;
  1210. input->name = "MELFAS MIP4 Touchscreen";
  1211. input->phys = ts->phys;
  1212. input->id.bustype = BUS_I2C;
  1213. input->id.vendor = 0x13c5;
  1214. input->open = mip4_input_open;
  1215. input->close = mip4_input_close;
  1216. input_set_drvdata(input, ts);
  1217. input->keycode = ts->key_code;
  1218. input->keycodesize = sizeof(*ts->key_code);
  1219. input->keycodemax = ts->key_num;
  1220. input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
  1221. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
  1222. input_set_abs_params(input, ABS_MT_PRESSURE,
  1223. MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
  1224. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
  1225. MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
  1226. input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
  1227. MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
  1228. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
  1229. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
  1230. error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
  1231. if (error)
  1232. return error;
  1233. i2c_set_clientdata(client, ts);
  1234. error = devm_request_threaded_irq(&client->dev, client->irq,
  1235. NULL, mip4_interrupt,
  1236. IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
  1237. if (error) {
  1238. dev_err(&client->dev,
  1239. "Failed to request interrupt %d: %d\n",
  1240. client->irq, error);
  1241. return error;
  1242. }
  1243. disable_irq(client->irq);
  1244. error = input_register_device(input);
  1245. if (error) {
  1246. dev_err(&client->dev,
  1247. "Failed to register input device: %d\n", error);
  1248. return error;
  1249. }
  1250. error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
  1251. if (error) {
  1252. dev_err(&client->dev,
  1253. "Failed to create sysfs attribute group: %d\n", error);
  1254. return error;
  1255. }
  1256. error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
  1257. if (error) {
  1258. mip4_sysfs_remove(ts);
  1259. dev_err(&client->dev,
  1260. "Failed to install sysfs remoce action: %d\n", error);
  1261. return error;
  1262. }
  1263. return 0;
  1264. }
  1265. static int __maybe_unused mip4_suspend(struct device *dev)
  1266. {
  1267. struct i2c_client *client = to_i2c_client(dev);
  1268. struct mip4_ts *ts = i2c_get_clientdata(client);
  1269. struct input_dev *input = ts->input;
  1270. mutex_lock(&input->mutex);
  1271. if (device_may_wakeup(dev))
  1272. ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
  1273. else if (input->users)
  1274. mip4_disable(ts);
  1275. mutex_unlock(&input->mutex);
  1276. return 0;
  1277. }
  1278. static int __maybe_unused mip4_resume(struct device *dev)
  1279. {
  1280. struct i2c_client *client = to_i2c_client(dev);
  1281. struct mip4_ts *ts = i2c_get_clientdata(client);
  1282. struct input_dev *input = ts->input;
  1283. mutex_lock(&input->mutex);
  1284. if (ts->wake_irq_enabled)
  1285. disable_irq_wake(client->irq);
  1286. else if (input->users)
  1287. mip4_enable(ts);
  1288. mutex_unlock(&input->mutex);
  1289. return 0;
  1290. }
  1291. static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
  1292. #ifdef CONFIG_OF
  1293. static const struct of_device_id mip4_of_match[] = {
  1294. { .compatible = "melfas,"MIP4_DEVICE_NAME, },
  1295. { },
  1296. };
  1297. MODULE_DEVICE_TABLE(of, mip4_of_match);
  1298. #endif
  1299. #ifdef CONFIG_ACPI
  1300. static const struct acpi_device_id mip4_acpi_match[] = {
  1301. { "MLFS0000", 0},
  1302. { },
  1303. };
  1304. MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
  1305. #endif
  1306. static const struct i2c_device_id mip4_i2c_ids[] = {
  1307. { MIP4_DEVICE_NAME, 0 },
  1308. { },
  1309. };
  1310. MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
  1311. static struct i2c_driver mip4_driver = {
  1312. .id_table = mip4_i2c_ids,
  1313. .probe = mip4_probe,
  1314. .driver = {
  1315. .name = MIP4_DEVICE_NAME,
  1316. .of_match_table = of_match_ptr(mip4_of_match),
  1317. .acpi_match_table = ACPI_PTR(mip4_acpi_match),
  1318. .pm = &mip4_pm_ops,
  1319. },
  1320. };
  1321. module_i2c_driver(mip4_driver);
  1322. MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
  1323. MODULE_VERSION("2016.09.28");
  1324. MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
  1325. MODULE_LICENSE("GPL");