cyapa.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. * Cypress APA trackpad with I2C interface
  3. *
  4. * Author: Dudley Du <dudl@cypress.com>
  5. * Further cleanup and restructuring by:
  6. * Daniel Kurtz <djkurtz@chromium.org>
  7. * Benson Leung <bleung@chromium.org>
  8. *
  9. * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
  10. * Copyright (C) 2011-2012 Google, Inc.
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file COPYING in the main directory of this archive for
  14. * more details.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/acpi.h>
  27. #include "cyapa.h"
  28. #define CYAPA_ADAPTER_FUNC_NONE 0
  29. #define CYAPA_ADAPTER_FUNC_I2C 1
  30. #define CYAPA_ADAPTER_FUNC_SMBUS 2
  31. #define CYAPA_ADAPTER_FUNC_BOTH 3
  32. #define CYAPA_FW_NAME "cyapa.bin"
  33. const char product_id[] = "CYTRA";
  34. static int cyapa_reinitialize(struct cyapa *cyapa);
  35. static inline bool cyapa_is_bootloader_mode(struct cyapa *cyapa)
  36. {
  37. if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL)
  38. return true;
  39. if (cyapa->gen == CYAPA_GEN3 &&
  40. cyapa->state >= CYAPA_STATE_BL_BUSY &&
  41. cyapa->state <= CYAPA_STATE_BL_ACTIVE)
  42. return true;
  43. return false;
  44. }
  45. static inline bool cyapa_is_operational_mode(struct cyapa *cyapa)
  46. {
  47. if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP)
  48. return true;
  49. if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
  50. return true;
  51. return false;
  52. }
  53. /* Returns 0 on success, else negative errno on failure. */
  54. static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
  55. u8 *values)
  56. {
  57. struct i2c_client *client = cyapa->client;
  58. struct i2c_msg msgs[] = {
  59. {
  60. .addr = client->addr,
  61. .flags = 0,
  62. .len = 1,
  63. .buf = &reg,
  64. },
  65. {
  66. .addr = client->addr,
  67. .flags = I2C_M_RD,
  68. .len = len,
  69. .buf = values,
  70. },
  71. };
  72. int ret;
  73. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  74. if (ret != ARRAY_SIZE(msgs))
  75. return ret < 0 ? ret : -EIO;
  76. return 0;
  77. }
  78. /**
  79. * cyapa_i2c_write - Execute i2c block data write operation
  80. * @cyapa: Handle to this driver
  81. * @ret: Offset of the data to written in the register map
  82. * @len: number of bytes to write
  83. * @values: Data to be written
  84. *
  85. * Return negative errno code on error; return zero when success.
  86. */
  87. static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
  88. size_t len, const void *values)
  89. {
  90. struct i2c_client *client = cyapa->client;
  91. char buf[32];
  92. int ret;
  93. if (len > sizeof(buf) - 1)
  94. return -ENOMEM;
  95. buf[0] = reg;
  96. memcpy(&buf[1], values, len);
  97. ret = i2c_master_send(client, buf, len + 1);
  98. if (ret != len + 1)
  99. return ret < 0 ? ret : -EIO;
  100. return 0;
  101. }
  102. static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
  103. {
  104. u8 ret = CYAPA_ADAPTER_FUNC_NONE;
  105. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  106. ret |= CYAPA_ADAPTER_FUNC_I2C;
  107. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  108. I2C_FUNC_SMBUS_BLOCK_DATA |
  109. I2C_FUNC_SMBUS_I2C_BLOCK))
  110. ret |= CYAPA_ADAPTER_FUNC_SMBUS;
  111. return ret;
  112. }
  113. /*
  114. * Query device for its current operating state.
  115. */
  116. static int cyapa_get_state(struct cyapa *cyapa)
  117. {
  118. u8 status[BL_STATUS_SIZE];
  119. u8 cmd[32];
  120. /* The i2c address of gen4 and gen5 trackpad device must be even. */
  121. bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
  122. bool smbus = false;
  123. int retries = 2;
  124. int error;
  125. cyapa->state = CYAPA_STATE_NO_DEVICE;
  126. /*
  127. * Get trackpad status by reading 3 registers starting from 0.
  128. * If the device is in the bootloader, this will be BL_HEAD.
  129. * If the device is in operation mode, this will be the DATA regs.
  130. *
  131. */
  132. error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
  133. status);
  134. /*
  135. * On smbus systems in OP mode, the i2c_reg_read will fail with
  136. * -ETIMEDOUT. In this case, try again using the smbus equivalent
  137. * command. This should return a BL_HEAD indicating CYAPA_STATE_OP.
  138. */
  139. if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) {
  140. if (!even_addr)
  141. error = cyapa_read_block(cyapa,
  142. CYAPA_CMD_BL_STATUS, status);
  143. smbus = true;
  144. }
  145. if (error != BL_STATUS_SIZE)
  146. goto error;
  147. /*
  148. * Detect trackpad protocol based on characteristic registers and bits.
  149. */
  150. do {
  151. cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
  152. cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
  153. cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
  154. if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
  155. cyapa->gen == CYAPA_GEN3) {
  156. error = cyapa_gen3_ops.state_parse(cyapa,
  157. status, BL_STATUS_SIZE);
  158. if (!error)
  159. goto out_detected;
  160. }
  161. if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
  162. cyapa->gen == CYAPA_GEN5) &&
  163. !smbus && even_addr) {
  164. error = cyapa_gen5_ops.state_parse(cyapa,
  165. status, BL_STATUS_SIZE);
  166. if (!error)
  167. goto out_detected;
  168. }
  169. /*
  170. * Write 0x00 0x00 to trackpad device to force update its
  171. * status, then redo the detection again.
  172. */
  173. if (!smbus) {
  174. cmd[0] = 0x00;
  175. cmd[1] = 0x00;
  176. error = cyapa_i2c_write(cyapa, 0, 2, cmd);
  177. if (error)
  178. goto error;
  179. msleep(50);
  180. error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
  181. BL_STATUS_SIZE, status);
  182. if (error)
  183. goto error;
  184. }
  185. } while (--retries > 0 && !smbus);
  186. goto error;
  187. out_detected:
  188. if (cyapa->state <= CYAPA_STATE_BL_BUSY)
  189. return -EAGAIN;
  190. return 0;
  191. error:
  192. return (error < 0) ? error : -EAGAIN;
  193. }
  194. /*
  195. * Poll device for its status in a loop, waiting up to timeout for a response.
  196. *
  197. * When the device switches state, it usually takes ~300 ms.
  198. * However, when running a new firmware image, the device must calibrate its
  199. * sensors, which can take as long as 2 seconds.
  200. *
  201. * Note: The timeout has granularity of the polling rate, which is 100 ms.
  202. *
  203. * Returns:
  204. * 0 when the device eventually responds with a valid non-busy state.
  205. * -ETIMEDOUT if device never responds (too many -EAGAIN)
  206. * -EAGAIN if bootload is busy, or unknown state.
  207. * < 0 other errors
  208. */
  209. int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
  210. {
  211. int error;
  212. int tries = timeout / 100;
  213. do {
  214. error = cyapa_get_state(cyapa);
  215. if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
  216. return 0;
  217. msleep(100);
  218. } while (tries--);
  219. return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
  220. }
  221. /*
  222. * Check if device is operational.
  223. *
  224. * An operational device is responding, has exited bootloader, and has
  225. * firmware supported by this driver.
  226. *
  227. * Returns:
  228. * -ENODEV no device
  229. * -EBUSY no device or in bootloader
  230. * -EIO failure while reading from device
  231. * -ETIMEDOUT timeout failure for bus idle or bus no response
  232. * -EAGAIN device is still in bootloader
  233. * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
  234. * -EINVAL device is in operational mode, but not supported by this driver
  235. * 0 device is supported
  236. */
  237. static int cyapa_check_is_operational(struct cyapa *cyapa)
  238. {
  239. int error;
  240. error = cyapa_poll_state(cyapa, 4000);
  241. if (error)
  242. return error;
  243. switch (cyapa->gen) {
  244. case CYAPA_GEN5:
  245. cyapa->ops = &cyapa_gen5_ops;
  246. break;
  247. case CYAPA_GEN3:
  248. cyapa->ops = &cyapa_gen3_ops;
  249. break;
  250. default:
  251. return -ENODEV;
  252. }
  253. error = cyapa->ops->operational_check(cyapa);
  254. if (!error && cyapa_is_operational_mode(cyapa))
  255. cyapa->operational = true;
  256. else
  257. cyapa->operational = false;
  258. return error;
  259. }
  260. /*
  261. * Returns 0 on device detected, negative errno on no device detected.
  262. * And when the device is detected and opertaional, it will be reset to
  263. * full power active mode automatically.
  264. */
  265. static int cyapa_detect(struct cyapa *cyapa)
  266. {
  267. struct device *dev = &cyapa->client->dev;
  268. int error;
  269. error = cyapa_check_is_operational(cyapa);
  270. if (error) {
  271. if (error != -ETIMEDOUT && error != -ENODEV &&
  272. cyapa_is_bootloader_mode(cyapa)) {
  273. dev_warn(dev, "device detected but not operational\n");
  274. return 0;
  275. }
  276. dev_err(dev, "no device detected: %d\n", error);
  277. return error;
  278. }
  279. return 0;
  280. }
  281. static int cyapa_open(struct input_dev *input)
  282. {
  283. struct cyapa *cyapa = input_get_drvdata(input);
  284. struct i2c_client *client = cyapa->client;
  285. int error;
  286. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  287. if (error)
  288. return error;
  289. if (cyapa->operational) {
  290. /*
  291. * though failed to set active power mode,
  292. * but still may be able to work in lower scan rate
  293. * when in operational mode.
  294. */
  295. error = cyapa->ops->set_power_mode(cyapa,
  296. PWR_MODE_FULL_ACTIVE, 0);
  297. if (error) {
  298. dev_warn(&client->dev,
  299. "set active power failed: %d\n", error);
  300. goto out;
  301. }
  302. } else {
  303. error = cyapa_reinitialize(cyapa);
  304. if (error || !cyapa->operational) {
  305. error = error ? error : -EAGAIN;
  306. goto out;
  307. }
  308. }
  309. enable_irq(client->irq);
  310. if (!pm_runtime_enabled(&client->dev)) {
  311. pm_runtime_set_active(&client->dev);
  312. pm_runtime_enable(&client->dev);
  313. }
  314. out:
  315. mutex_unlock(&cyapa->state_sync_lock);
  316. return error;
  317. }
  318. static void cyapa_close(struct input_dev *input)
  319. {
  320. struct cyapa *cyapa = input_get_drvdata(input);
  321. struct i2c_client *client = cyapa->client;
  322. mutex_lock(&cyapa->state_sync_lock);
  323. disable_irq(client->irq);
  324. if (pm_runtime_enabled(&client->dev))
  325. pm_runtime_disable(&client->dev);
  326. pm_runtime_set_suspended(&client->dev);
  327. if (cyapa->operational)
  328. cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
  329. mutex_unlock(&cyapa->state_sync_lock);
  330. }
  331. static int cyapa_create_input_dev(struct cyapa *cyapa)
  332. {
  333. struct device *dev = &cyapa->client->dev;
  334. struct input_dev *input;
  335. int error;
  336. if (!cyapa->physical_size_x || !cyapa->physical_size_y)
  337. return -EINVAL;
  338. input = devm_input_allocate_device(dev);
  339. if (!input) {
  340. dev_err(dev, "failed to allocate memory for input device.\n");
  341. return -ENOMEM;
  342. }
  343. input->name = CYAPA_NAME;
  344. input->phys = cyapa->phys;
  345. input->id.bustype = BUS_I2C;
  346. input->id.version = 1;
  347. input->id.product = 0; /* Means any product in eventcomm. */
  348. input->dev.parent = &cyapa->client->dev;
  349. input->open = cyapa_open;
  350. input->close = cyapa_close;
  351. input_set_drvdata(input, cyapa);
  352. __set_bit(EV_ABS, input->evbit);
  353. /* Finger position */
  354. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
  355. 0);
  356. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
  357. 0);
  358. input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
  359. if (cyapa->gen > CYAPA_GEN3) {
  360. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  361. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
  362. /*
  363. * Orientation is the angle between the vertical axis and
  364. * the major axis of the contact ellipse.
  365. * The range is -127 to 127.
  366. * the positive direction is clockwise form the vertical axis.
  367. * If the ellipse of contact degenerates into a circle,
  368. * orientation is reported as 0.
  369. *
  370. * Also, for Gen5 trackpad the accurate of this orientation
  371. * value is value + (-30 ~ 30).
  372. */
  373. input_set_abs_params(input, ABS_MT_ORIENTATION,
  374. -127, 127, 0, 0);
  375. }
  376. if (cyapa->gen >= CYAPA_GEN5) {
  377. input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  378. input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
  379. }
  380. input_abs_set_res(input, ABS_MT_POSITION_X,
  381. cyapa->max_abs_x / cyapa->physical_size_x);
  382. input_abs_set_res(input, ABS_MT_POSITION_Y,
  383. cyapa->max_abs_y / cyapa->physical_size_y);
  384. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  385. __set_bit(BTN_LEFT, input->keybit);
  386. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  387. __set_bit(BTN_MIDDLE, input->keybit);
  388. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  389. __set_bit(BTN_RIGHT, input->keybit);
  390. if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
  391. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  392. /* Handle pointer emulation and unused slots in core */
  393. error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
  394. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
  395. if (error) {
  396. dev_err(dev, "failed to initialize MT slots: %d\n", error);
  397. return error;
  398. }
  399. /* Register the device in input subsystem */
  400. error = input_register_device(input);
  401. if (error) {
  402. dev_err(dev, "failed to register input device: %d\n", error);
  403. return error;
  404. }
  405. cyapa->input = input;
  406. return 0;
  407. }
  408. static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
  409. {
  410. struct input_dev *input = cyapa->input;
  411. if (!input || !input->users) {
  412. /*
  413. * When input is NULL, TP must be in deep sleep mode.
  414. * In this mode, later non-power I2C command will always failed
  415. * if not bring it out of deep sleep mode firstly,
  416. * so must command TP to active mode here.
  417. */
  418. if (!input || cyapa->operational)
  419. cyapa->ops->set_power_mode(cyapa,
  420. PWR_MODE_FULL_ACTIVE, 0);
  421. /* Gen3 always using polling mode for command. */
  422. if (cyapa->gen >= CYAPA_GEN5)
  423. enable_irq(cyapa->client->irq);
  424. }
  425. }
  426. static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
  427. {
  428. struct input_dev *input = cyapa->input;
  429. if (!input || !input->users) {
  430. if (cyapa->gen >= CYAPA_GEN5)
  431. disable_irq(cyapa->client->irq);
  432. if (!input || cyapa->operational)
  433. cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
  434. }
  435. }
  436. /*
  437. * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
  438. *
  439. * These are helper functions that convert to and from integer idle
  440. * times and register settings to write to the PowerMode register.
  441. * The trackpad supports between 20ms to 1000ms scan intervals.
  442. * The time will be increased in increments of 10ms from 20ms to 100ms.
  443. * From 100ms to 1000ms, time will be increased in increments of 20ms.
  444. *
  445. * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
  446. * Idle_Command = Idle Time / 10;
  447. * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
  448. * Idle_Command = Idle Time / 20 + 5;
  449. */
  450. u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
  451. {
  452. u16 encoded_time;
  453. sleep_time = clamp_val(sleep_time, 20, 1000);
  454. encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
  455. return (encoded_time << 2) & PWR_MODE_MASK;
  456. }
  457. u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
  458. {
  459. u8 encoded_time = pwr_mode >> 2;
  460. return (encoded_time < 10) ? encoded_time * 10
  461. : (encoded_time - 5) * 20;
  462. }
  463. /* 0 on driver initialize and detected successfully, negative on failure. */
  464. static int cyapa_initialize(struct cyapa *cyapa)
  465. {
  466. int error = 0;
  467. cyapa->state = CYAPA_STATE_NO_DEVICE;
  468. cyapa->gen = CYAPA_GEN_UNKNOWN;
  469. mutex_init(&cyapa->state_sync_lock);
  470. /*
  471. * Set to hard code default, they will be updated with trackpad set
  472. * default values after probe and initialized.
  473. */
  474. cyapa->suspend_power_mode = PWR_MODE_SLEEP;
  475. cyapa->suspend_sleep_time =
  476. cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
  477. /* ops.initialize() is aimed to prepare for module communications. */
  478. error = cyapa_gen3_ops.initialize(cyapa);
  479. if (!error)
  480. error = cyapa_gen5_ops.initialize(cyapa);
  481. if (error)
  482. return error;
  483. error = cyapa_detect(cyapa);
  484. if (error)
  485. return error;
  486. /* Power down the device until we need it. */
  487. if (cyapa->operational)
  488. cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
  489. return 0;
  490. }
  491. static int cyapa_reinitialize(struct cyapa *cyapa)
  492. {
  493. struct device *dev = &cyapa->client->dev;
  494. struct input_dev *input = cyapa->input;
  495. int error;
  496. if (pm_runtime_enabled(dev))
  497. pm_runtime_disable(dev);
  498. /* Avoid command failures when TP was in OFF state. */
  499. if (cyapa->operational)
  500. cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0);
  501. error = cyapa_detect(cyapa);
  502. if (error)
  503. goto out;
  504. if (!input && cyapa->operational) {
  505. error = cyapa_create_input_dev(cyapa);
  506. if (error) {
  507. dev_err(dev, "create input_dev instance failed: %d\n",
  508. error);
  509. goto out;
  510. }
  511. }
  512. out:
  513. if (!input || !input->users) {
  514. /* Reset to power OFF state to save power when no user open. */
  515. if (cyapa->operational)
  516. cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
  517. } else if (!error && cyapa->operational) {
  518. /*
  519. * Make sure only enable runtime PM when device is
  520. * in operational mode and input->users > 0.
  521. */
  522. pm_runtime_set_active(dev);
  523. pm_runtime_enable(dev);
  524. }
  525. return error;
  526. }
  527. static irqreturn_t cyapa_irq(int irq, void *dev_id)
  528. {
  529. struct cyapa *cyapa = dev_id;
  530. struct device *dev = &cyapa->client->dev;
  531. pm_runtime_get_sync(dev);
  532. if (device_may_wakeup(dev))
  533. pm_wakeup_event(dev, 0);
  534. /* Interrupt event maybe cuased by host command to trackpad device. */
  535. if (cyapa->ops->irq_cmd_handler(cyapa)) {
  536. /*
  537. * Interrupt event maybe from trackpad device input reporting.
  538. */
  539. if (!cyapa->input) {
  540. /*
  541. * Still in probling or in firware image
  542. * udpating or reading.
  543. */
  544. cyapa->ops->sort_empty_output_data(cyapa,
  545. NULL, NULL, NULL);
  546. goto out;
  547. }
  548. if (!cyapa->operational || cyapa->ops->irq_handler(cyapa)) {
  549. if (!mutex_trylock(&cyapa->state_sync_lock)) {
  550. cyapa->ops->sort_empty_output_data(cyapa,
  551. NULL, NULL, NULL);
  552. goto out;
  553. }
  554. cyapa_reinitialize(cyapa);
  555. mutex_unlock(&cyapa->state_sync_lock);
  556. }
  557. }
  558. out:
  559. pm_runtime_mark_last_busy(dev);
  560. pm_runtime_put_sync_autosuspend(dev);
  561. return IRQ_HANDLED;
  562. }
  563. /*
  564. **************************************************************
  565. * sysfs interface
  566. **************************************************************
  567. */
  568. #ifdef CONFIG_PM_SLEEP
  569. static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
  570. struct device_attribute *attr,
  571. char *buf)
  572. {
  573. struct cyapa *cyapa = dev_get_drvdata(dev);
  574. u8 pwr_cmd = cyapa->suspend_power_mode;
  575. u16 sleep_time;
  576. int len;
  577. int error;
  578. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  579. if (error)
  580. return error;
  581. pwr_cmd = cyapa->suspend_power_mode;
  582. sleep_time = cyapa->suspend_sleep_time;
  583. mutex_unlock(&cyapa->state_sync_lock);
  584. switch (pwr_cmd) {
  585. case PWR_MODE_BTN_ONLY:
  586. len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
  587. break;
  588. case PWR_MODE_OFF:
  589. len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
  590. break;
  591. default:
  592. len = scnprintf(buf, PAGE_SIZE, "%u\n",
  593. cyapa->gen == CYAPA_GEN3 ?
  594. cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
  595. sleep_time);
  596. break;
  597. }
  598. return len;
  599. }
  600. static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
  601. struct device_attribute *attr,
  602. const char *buf, size_t count)
  603. {
  604. struct cyapa *cyapa = dev_get_drvdata(dev);
  605. u16 sleep_time;
  606. int error;
  607. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  608. if (error)
  609. return error;
  610. if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
  611. cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
  612. } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
  613. cyapa->suspend_power_mode = PWR_MODE_OFF;
  614. } else if (!kstrtou16(buf, 10, &sleep_time)) {
  615. cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
  616. cyapa->suspend_power_mode =
  617. cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
  618. } else {
  619. count = -EINVAL;
  620. }
  621. mutex_unlock(&cyapa->state_sync_lock);
  622. return count;
  623. }
  624. static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
  625. cyapa_show_suspend_scanrate,
  626. cyapa_update_suspend_scanrate);
  627. static struct attribute *cyapa_power_wakeup_entries[] = {
  628. &dev_attr_suspend_scanrate_ms.attr,
  629. NULL,
  630. };
  631. static const struct attribute_group cyapa_power_wakeup_group = {
  632. .name = power_group_name,
  633. .attrs = cyapa_power_wakeup_entries,
  634. };
  635. static void cyapa_remove_power_wakeup_group(void *data)
  636. {
  637. struct cyapa *cyapa = data;
  638. sysfs_unmerge_group(&cyapa->client->dev.kobj,
  639. &cyapa_power_wakeup_group);
  640. }
  641. static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
  642. {
  643. struct i2c_client *client = cyapa->client;
  644. struct device *dev = &client->dev;
  645. int error;
  646. if (device_can_wakeup(dev)) {
  647. error = sysfs_merge_group(&client->dev.kobj,
  648. &cyapa_power_wakeup_group);
  649. if (error) {
  650. dev_err(dev, "failed to add power wakeup group: %d\n",
  651. error);
  652. return error;
  653. }
  654. error = devm_add_action(dev,
  655. cyapa_remove_power_wakeup_group, cyapa);
  656. if (error) {
  657. cyapa_remove_power_wakeup_group(cyapa);
  658. dev_err(dev, "failed to add power cleanup action: %d\n",
  659. error);
  660. return error;
  661. }
  662. }
  663. return 0;
  664. }
  665. #else
  666. static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
  667. {
  668. return 0;
  669. }
  670. #endif /* CONFIG_PM_SLEEP */
  671. #ifdef CONFIG_PM
  672. static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
  673. struct device_attribute *attr,
  674. char *buf)
  675. {
  676. struct cyapa *cyapa = dev_get_drvdata(dev);
  677. u8 pwr_cmd;
  678. u16 sleep_time;
  679. int error;
  680. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  681. if (error)
  682. return error;
  683. pwr_cmd = cyapa->runtime_suspend_power_mode;
  684. sleep_time = cyapa->runtime_suspend_sleep_time;
  685. mutex_unlock(&cyapa->state_sync_lock);
  686. return scnprintf(buf, PAGE_SIZE, "%u\n",
  687. cyapa->gen == CYAPA_GEN3 ?
  688. cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
  689. sleep_time);
  690. }
  691. static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
  692. struct device_attribute *attr,
  693. const char *buf, size_t count)
  694. {
  695. struct cyapa *cyapa = dev_get_drvdata(dev);
  696. u16 time;
  697. int error;
  698. if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
  699. dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
  700. return -EINVAL;
  701. }
  702. /*
  703. * When the suspend scanrate is changed, pm_runtime_get to resume
  704. * a potentially suspended device, update to the new pwr_cmd
  705. * and then pm_runtime_put to suspend into the new power mode.
  706. */
  707. pm_runtime_get_sync(dev);
  708. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  709. if (error)
  710. return error;
  711. cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
  712. cyapa->runtime_suspend_power_mode =
  713. cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
  714. mutex_unlock(&cyapa->state_sync_lock);
  715. pm_runtime_put_sync_autosuspend(dev);
  716. return count;
  717. }
  718. static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
  719. cyapa_show_rt_suspend_scanrate,
  720. cyapa_update_rt_suspend_scanrate);
  721. static struct attribute *cyapa_power_runtime_entries[] = {
  722. &dev_attr_runtime_suspend_scanrate_ms.attr,
  723. NULL,
  724. };
  725. static const struct attribute_group cyapa_power_runtime_group = {
  726. .name = power_group_name,
  727. .attrs = cyapa_power_runtime_entries,
  728. };
  729. static void cyapa_remove_power_runtime_group(void *data)
  730. {
  731. struct cyapa *cyapa = data;
  732. sysfs_unmerge_group(&cyapa->client->dev.kobj,
  733. &cyapa_power_runtime_group);
  734. }
  735. static int cyapa_start_runtime(struct cyapa *cyapa)
  736. {
  737. struct device *dev = &cyapa->client->dev;
  738. int error;
  739. cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
  740. cyapa->runtime_suspend_sleep_time =
  741. cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
  742. error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
  743. if (error) {
  744. dev_err(dev,
  745. "failed to create power runtime group: %d\n", error);
  746. return error;
  747. }
  748. error = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa);
  749. if (error) {
  750. cyapa_remove_power_runtime_group(cyapa);
  751. dev_err(dev,
  752. "failed to add power runtime cleanup action: %d\n",
  753. error);
  754. return error;
  755. }
  756. /* runtime is enabled until device is operational and opened. */
  757. pm_runtime_set_suspended(dev);
  758. pm_runtime_use_autosuspend(dev);
  759. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
  760. return 0;
  761. }
  762. #else
  763. static inline int cyapa_start_runtime(struct cyapa *cyapa)
  764. {
  765. return 0;
  766. }
  767. #endif /* CONFIG_PM */
  768. static ssize_t cyapa_show_fm_ver(struct device *dev,
  769. struct device_attribute *attr, char *buf)
  770. {
  771. int error;
  772. struct cyapa *cyapa = dev_get_drvdata(dev);
  773. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  774. if (error)
  775. return error;
  776. error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
  777. cyapa->fw_min_ver);
  778. mutex_unlock(&cyapa->state_sync_lock);
  779. return error;
  780. }
  781. static ssize_t cyapa_show_product_id(struct device *dev,
  782. struct device_attribute *attr, char *buf)
  783. {
  784. struct cyapa *cyapa = dev_get_drvdata(dev);
  785. int size;
  786. int error;
  787. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  788. if (error)
  789. return error;
  790. size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
  791. mutex_unlock(&cyapa->state_sync_lock);
  792. return size;
  793. }
  794. static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
  795. {
  796. struct device *dev = &cyapa->client->dev;
  797. const struct firmware *fw;
  798. int error;
  799. error = request_firmware(&fw, fw_name, dev);
  800. if (error) {
  801. dev_err(dev, "Could not load firmware from %s: %d\n",
  802. fw_name, error);
  803. return error;
  804. }
  805. error = cyapa->ops->check_fw(cyapa, fw);
  806. if (error) {
  807. dev_err(dev, "Invalid CYAPA firmware image: %s\n",
  808. fw_name);
  809. goto done;
  810. }
  811. /*
  812. * Resume the potentially suspended device because doing FW
  813. * update on a device not in the FULL mode has a chance to
  814. * fail.
  815. */
  816. pm_runtime_get_sync(dev);
  817. /* Require IRQ support for firmware update commands. */
  818. cyapa_enable_irq_for_cmd(cyapa);
  819. error = cyapa->ops->bl_enter(cyapa);
  820. if (error) {
  821. dev_err(dev, "bl_enter failed, %d\n", error);
  822. goto err_detect;
  823. }
  824. error = cyapa->ops->bl_activate(cyapa);
  825. if (error) {
  826. dev_err(dev, "bl_activate failed, %d\n", error);
  827. goto err_detect;
  828. }
  829. error = cyapa->ops->bl_initiate(cyapa, fw);
  830. if (error) {
  831. dev_err(dev, "bl_initiate failed, %d\n", error);
  832. goto err_detect;
  833. }
  834. error = cyapa->ops->update_fw(cyapa, fw);
  835. if (error) {
  836. dev_err(dev, "update_fw failed, %d\n", error);
  837. goto err_detect;
  838. }
  839. err_detect:
  840. cyapa_disable_irq_for_cmd(cyapa);
  841. pm_runtime_put_noidle(dev);
  842. done:
  843. release_firmware(fw);
  844. return error;
  845. }
  846. static ssize_t cyapa_update_fw_store(struct device *dev,
  847. struct device_attribute *attr,
  848. const char *buf, size_t count)
  849. {
  850. struct cyapa *cyapa = dev_get_drvdata(dev);
  851. char fw_name[NAME_MAX];
  852. int ret, error;
  853. if (count >= NAME_MAX) {
  854. dev_err(dev, "File name too long\n");
  855. return -EINVAL;
  856. }
  857. memcpy(fw_name, buf, count);
  858. if (fw_name[count - 1] == '\n')
  859. fw_name[count - 1] = '\0';
  860. else
  861. fw_name[count] = '\0';
  862. if (cyapa->input) {
  863. /*
  864. * Force the input device to be registered after the firmware
  865. * image is updated, so if the corresponding parameters updated
  866. * in the new firmware image can taken effect immediately.
  867. */
  868. input_unregister_device(cyapa->input);
  869. cyapa->input = NULL;
  870. }
  871. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  872. if (error) {
  873. /*
  874. * Whatever, do reinitialize to try to recover TP state to
  875. * previous state just as it entered fw update entrance.
  876. */
  877. cyapa_reinitialize(cyapa);
  878. return error;
  879. }
  880. error = cyapa_firmware(cyapa, fw_name);
  881. if (error)
  882. dev_err(dev, "firmware update failed: %d\n", error);
  883. else
  884. dev_dbg(dev, "firmware update successfully done.\n");
  885. /*
  886. * Redetect trackpad device states because firmware update process
  887. * will reset trackpad device into bootloader mode.
  888. */
  889. ret = cyapa_reinitialize(cyapa);
  890. if (ret) {
  891. dev_err(dev, "failed to redetect after updated: %d\n", ret);
  892. error = error ? error : ret;
  893. }
  894. mutex_unlock(&cyapa->state_sync_lock);
  895. return error ? error : count;
  896. }
  897. static ssize_t cyapa_calibrate_store(struct device *dev,
  898. struct device_attribute *attr,
  899. const char *buf, size_t count)
  900. {
  901. struct cyapa *cyapa = dev_get_drvdata(dev);
  902. int error;
  903. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  904. if (error)
  905. return error;
  906. if (cyapa->operational) {
  907. cyapa_enable_irq_for_cmd(cyapa);
  908. error = cyapa->ops->calibrate_store(dev, attr, buf, count);
  909. cyapa_disable_irq_for_cmd(cyapa);
  910. } else {
  911. error = -EBUSY; /* Still running in bootloader mode. */
  912. }
  913. mutex_unlock(&cyapa->state_sync_lock);
  914. return error < 0 ? error : count;
  915. }
  916. static ssize_t cyapa_show_baseline(struct device *dev,
  917. struct device_attribute *attr, char *buf)
  918. {
  919. struct cyapa *cyapa = dev_get_drvdata(dev);
  920. ssize_t error;
  921. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  922. if (error)
  923. return error;
  924. if (cyapa->operational) {
  925. cyapa_enable_irq_for_cmd(cyapa);
  926. error = cyapa->ops->show_baseline(dev, attr, buf);
  927. cyapa_disable_irq_for_cmd(cyapa);
  928. } else {
  929. error = -EBUSY; /* Still running in bootloader mode. */
  930. }
  931. mutex_unlock(&cyapa->state_sync_lock);
  932. return error;
  933. }
  934. static char *cyapa_state_to_string(struct cyapa *cyapa)
  935. {
  936. switch (cyapa->state) {
  937. case CYAPA_STATE_BL_BUSY:
  938. return "bootloader busy";
  939. case CYAPA_STATE_BL_IDLE:
  940. return "bootloader idle";
  941. case CYAPA_STATE_BL_ACTIVE:
  942. return "bootloader active";
  943. case CYAPA_STATE_GEN5_BL:
  944. return "bootloader";
  945. case CYAPA_STATE_OP:
  946. case CYAPA_STATE_GEN5_APP:
  947. return "operational"; /* Normal valid state. */
  948. default:
  949. return "invalid mode";
  950. }
  951. }
  952. static ssize_t cyapa_show_mode(struct device *dev,
  953. struct device_attribute *attr, char *buf)
  954. {
  955. struct cyapa *cyapa = dev_get_drvdata(dev);
  956. int size;
  957. int error;
  958. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  959. if (error)
  960. return error;
  961. size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
  962. cyapa->gen, cyapa_state_to_string(cyapa));
  963. mutex_unlock(&cyapa->state_sync_lock);
  964. return size;
  965. }
  966. static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
  967. static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
  968. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
  969. static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
  970. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
  971. static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
  972. static struct attribute *cyapa_sysfs_entries[] = {
  973. &dev_attr_firmware_version.attr,
  974. &dev_attr_product_id.attr,
  975. &dev_attr_update_fw.attr,
  976. &dev_attr_baseline.attr,
  977. &dev_attr_calibrate.attr,
  978. &dev_attr_mode.attr,
  979. NULL,
  980. };
  981. static const struct attribute_group cyapa_sysfs_group = {
  982. .attrs = cyapa_sysfs_entries,
  983. };
  984. static void cyapa_remove_sysfs_group(void *data)
  985. {
  986. struct cyapa *cyapa = data;
  987. sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group);
  988. }
  989. static int cyapa_probe(struct i2c_client *client,
  990. const struct i2c_device_id *dev_id)
  991. {
  992. struct device *dev = &client->dev;
  993. struct cyapa *cyapa;
  994. u8 adapter_func;
  995. union i2c_smbus_data dummy;
  996. int error;
  997. adapter_func = cyapa_check_adapter_functionality(client);
  998. if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
  999. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  1000. return -EIO;
  1001. }
  1002. /* Make sure there is something at this address */
  1003. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  1004. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
  1005. return -ENODEV;
  1006. cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
  1007. if (!cyapa)
  1008. return -ENOMEM;
  1009. /* i2c isn't supported, use smbus */
  1010. if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
  1011. cyapa->smbus = true;
  1012. cyapa->client = client;
  1013. i2c_set_clientdata(client, cyapa);
  1014. sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
  1015. client->addr);
  1016. error = cyapa_initialize(cyapa);
  1017. if (error) {
  1018. dev_err(dev, "failed to detect and initialize tp device.\n");
  1019. return error;
  1020. }
  1021. error = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group);
  1022. if (error) {
  1023. dev_err(dev, "failed to create sysfs entries: %d\n", error);
  1024. return error;
  1025. }
  1026. error = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa);
  1027. if (error) {
  1028. cyapa_remove_sysfs_group(cyapa);
  1029. dev_err(dev, "failed to add sysfs cleanup action: %d\n", error);
  1030. return error;
  1031. }
  1032. error = cyapa_prepare_wakeup_controls(cyapa);
  1033. if (error) {
  1034. dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
  1035. return error;
  1036. }
  1037. error = cyapa_start_runtime(cyapa);
  1038. if (error) {
  1039. dev_err(dev, "failed to start pm_runtime: %d\n", error);
  1040. return error;
  1041. }
  1042. error = devm_request_threaded_irq(dev, client->irq,
  1043. NULL, cyapa_irq,
  1044. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1045. "cyapa", cyapa);
  1046. if (error) {
  1047. dev_err(dev, "failed to request threaded irq: %d\n", error);
  1048. return error;
  1049. }
  1050. /* Disable IRQ until the device is opened */
  1051. disable_irq(client->irq);
  1052. /*
  1053. * Register the device in the input subsystem when it's operational.
  1054. * Otherwise, keep in this driver, so it can be be recovered or updated
  1055. * through the sysfs mode and update_fw interfaces by user or apps.
  1056. */
  1057. if (cyapa->operational) {
  1058. error = cyapa_create_input_dev(cyapa);
  1059. if (error) {
  1060. dev_err(dev, "create input_dev instance failed: %d\n",
  1061. error);
  1062. return error;
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. static int __maybe_unused cyapa_suspend(struct device *dev)
  1068. {
  1069. struct i2c_client *client = to_i2c_client(dev);
  1070. struct cyapa *cyapa = i2c_get_clientdata(client);
  1071. u8 power_mode;
  1072. int error;
  1073. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  1074. if (error)
  1075. return error;
  1076. /*
  1077. * Runtime PM is enable only when device is in operational mode and
  1078. * users in use, so need check it before disable it to
  1079. * avoid unbalance warning.
  1080. */
  1081. if (pm_runtime_enabled(dev))
  1082. pm_runtime_disable(dev);
  1083. disable_irq(client->irq);
  1084. /*
  1085. * Set trackpad device to idle mode if wakeup is allowed,
  1086. * otherwise turn off.
  1087. */
  1088. if (cyapa->operational) {
  1089. power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
  1090. : PWR_MODE_OFF;
  1091. error = cyapa->ops->set_power_mode(cyapa, power_mode,
  1092. cyapa->suspend_sleep_time);
  1093. if (error)
  1094. dev_err(dev, "suspend set power mode failed: %d\n",
  1095. error);
  1096. }
  1097. if (device_may_wakeup(dev))
  1098. cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
  1099. mutex_unlock(&cyapa->state_sync_lock);
  1100. return 0;
  1101. }
  1102. static int __maybe_unused cyapa_resume(struct device *dev)
  1103. {
  1104. struct i2c_client *client = to_i2c_client(dev);
  1105. struct cyapa *cyapa = i2c_get_clientdata(client);
  1106. int error;
  1107. mutex_lock(&cyapa->state_sync_lock);
  1108. if (device_may_wakeup(dev) && cyapa->irq_wake) {
  1109. disable_irq_wake(client->irq);
  1110. cyapa->irq_wake = false;
  1111. }
  1112. /* Update device states and runtime PM states. */
  1113. error = cyapa_reinitialize(cyapa);
  1114. if (error)
  1115. dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
  1116. enable_irq(client->irq);
  1117. mutex_unlock(&cyapa->state_sync_lock);
  1118. return 0;
  1119. }
  1120. static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
  1121. {
  1122. struct cyapa *cyapa = dev_get_drvdata(dev);
  1123. int error;
  1124. error = cyapa->ops->set_power_mode(cyapa,
  1125. cyapa->runtime_suspend_power_mode,
  1126. cyapa->runtime_suspend_sleep_time);
  1127. if (error)
  1128. dev_warn(dev, "runtime suspend failed: %d\n", error);
  1129. return 0;
  1130. }
  1131. static int __maybe_unused cyapa_runtime_resume(struct device *dev)
  1132. {
  1133. struct cyapa *cyapa = dev_get_drvdata(dev);
  1134. int error;
  1135. error = cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0);
  1136. if (error)
  1137. dev_warn(dev, "runtime resume failed: %d\n", error);
  1138. return 0;
  1139. }
  1140. static const struct dev_pm_ops cyapa_pm_ops = {
  1141. SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
  1142. SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
  1143. };
  1144. static const struct i2c_device_id cyapa_id_table[] = {
  1145. { "cyapa", 0 },
  1146. { },
  1147. };
  1148. MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
  1149. #ifdef CONFIG_ACPI
  1150. static const struct acpi_device_id cyapa_acpi_id[] = {
  1151. { "CYAP0000", 0 }, /* Gen3 trackpad with 0x67 I2C address. */
  1152. { "CYAP0001", 0 }, /* Gen5 trackpad with 0x24 I2C address. */
  1153. { }
  1154. };
  1155. MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
  1156. #endif
  1157. static struct i2c_driver cyapa_driver = {
  1158. .driver = {
  1159. .name = "cyapa",
  1160. .owner = THIS_MODULE,
  1161. .pm = &cyapa_pm_ops,
  1162. .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
  1163. },
  1164. .probe = cyapa_probe,
  1165. .id_table = cyapa_id_table,
  1166. };
  1167. module_i2c_driver(cyapa_driver);
  1168. MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
  1169. MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
  1170. MODULE_LICENSE("GPL");