cyapa.c 37 KB

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