cyapa.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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, false);
  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, PWR_MODE_OFF, 0, false);
  363. mutex_unlock(&cyapa->state_sync_lock);
  364. }
  365. static int cyapa_create_input_dev(struct cyapa *cyapa)
  366. {
  367. struct device *dev = &cyapa->client->dev;
  368. struct input_dev *input;
  369. int error;
  370. if (!cyapa->physical_size_x || !cyapa->physical_size_y)
  371. return -EINVAL;
  372. input = devm_input_allocate_device(dev);
  373. if (!input) {
  374. dev_err(dev, "failed to allocate memory for input device.\n");
  375. return -ENOMEM;
  376. }
  377. input->name = CYAPA_NAME;
  378. input->phys = cyapa->phys;
  379. input->id.bustype = BUS_I2C;
  380. input->id.version = 1;
  381. input->id.product = 0; /* Means any product in eventcomm. */
  382. input->dev.parent = &cyapa->client->dev;
  383. input->open = cyapa_open;
  384. input->close = cyapa_close;
  385. input_set_drvdata(input, cyapa);
  386. __set_bit(EV_ABS, input->evbit);
  387. /* Finger position */
  388. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
  389. 0);
  390. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
  391. 0);
  392. input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
  393. if (cyapa->gen > CYAPA_GEN3) {
  394. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  395. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
  396. /*
  397. * Orientation is the angle between the vertical axis and
  398. * the major axis of the contact ellipse.
  399. * The range is -127 to 127.
  400. * the positive direction is clockwise form the vertical axis.
  401. * If the ellipse of contact degenerates into a circle,
  402. * orientation is reported as 0.
  403. *
  404. * Also, for Gen5 trackpad the accurate of this orientation
  405. * value is value + (-30 ~ 30).
  406. */
  407. input_set_abs_params(input, ABS_MT_ORIENTATION,
  408. -127, 127, 0, 0);
  409. }
  410. if (cyapa->gen >= CYAPA_GEN5) {
  411. input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  412. input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
  413. input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
  414. }
  415. input_abs_set_res(input, ABS_MT_POSITION_X,
  416. cyapa->max_abs_x / cyapa->physical_size_x);
  417. input_abs_set_res(input, ABS_MT_POSITION_Y,
  418. cyapa->max_abs_y / cyapa->physical_size_y);
  419. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  420. __set_bit(BTN_LEFT, input->keybit);
  421. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  422. __set_bit(BTN_MIDDLE, input->keybit);
  423. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  424. __set_bit(BTN_RIGHT, input->keybit);
  425. if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
  426. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  427. /* Handle pointer emulation and unused slots in core */
  428. error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
  429. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
  430. if (error) {
  431. dev_err(dev, "failed to initialize MT slots: %d\n", error);
  432. return error;
  433. }
  434. /* Register the device in input subsystem */
  435. error = input_register_device(input);
  436. if (error) {
  437. dev_err(dev, "failed to register input device: %d\n", error);
  438. return error;
  439. }
  440. cyapa->input = input;
  441. return 0;
  442. }
  443. static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
  444. {
  445. struct input_dev *input = cyapa->input;
  446. if (!input || !input->users) {
  447. /*
  448. * When input is NULL, TP must be in deep sleep mode.
  449. * In this mode, later non-power I2C command will always failed
  450. * if not bring it out of deep sleep mode firstly,
  451. * so must command TP to active mode here.
  452. */
  453. if (!input || cyapa->operational)
  454. cyapa->ops->set_power_mode(cyapa,
  455. PWR_MODE_FULL_ACTIVE, 0, false);
  456. /* Gen3 always using polling mode for command. */
  457. if (cyapa->gen >= CYAPA_GEN5)
  458. enable_irq(cyapa->client->irq);
  459. }
  460. }
  461. static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
  462. {
  463. struct input_dev *input = cyapa->input;
  464. if (!input || !input->users) {
  465. if (cyapa->gen >= CYAPA_GEN5)
  466. disable_irq(cyapa->client->irq);
  467. if (!input || cyapa->operational)
  468. cyapa->ops->set_power_mode(cyapa,
  469. PWR_MODE_OFF, 0, false);
  470. }
  471. }
  472. /*
  473. * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
  474. *
  475. * These are helper functions that convert to and from integer idle
  476. * times and register settings to write to the PowerMode register.
  477. * The trackpad supports between 20ms to 1000ms scan intervals.
  478. * The time will be increased in increments of 10ms from 20ms to 100ms.
  479. * From 100ms to 1000ms, time will be increased in increments of 20ms.
  480. *
  481. * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
  482. * Idle_Command = Idle Time / 10;
  483. * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
  484. * Idle_Command = Idle Time / 20 + 5;
  485. */
  486. u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
  487. {
  488. u16 encoded_time;
  489. sleep_time = clamp_val(sleep_time, 20, 1000);
  490. encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
  491. return (encoded_time << 2) & PWR_MODE_MASK;
  492. }
  493. u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
  494. {
  495. u8 encoded_time = pwr_mode >> 2;
  496. return (encoded_time < 10) ? encoded_time * 10
  497. : (encoded_time - 5) * 20;
  498. }
  499. /* 0 on driver initialize and detected successfully, negative on failure. */
  500. static int cyapa_initialize(struct cyapa *cyapa)
  501. {
  502. int error = 0;
  503. cyapa->state = CYAPA_STATE_NO_DEVICE;
  504. cyapa->gen = CYAPA_GEN_UNKNOWN;
  505. mutex_init(&cyapa->state_sync_lock);
  506. /*
  507. * Set to hard code default, they will be updated with trackpad set
  508. * default values after probe and initialized.
  509. */
  510. cyapa->suspend_power_mode = PWR_MODE_SLEEP;
  511. cyapa->suspend_sleep_time =
  512. cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
  513. /* ops.initialize() is aimed to prepare for module communications. */
  514. error = cyapa_gen3_ops.initialize(cyapa);
  515. if (!error)
  516. error = cyapa_gen5_ops.initialize(cyapa);
  517. if (!error)
  518. error = cyapa_gen6_ops.initialize(cyapa);
  519. if (error)
  520. return error;
  521. error = cyapa_detect(cyapa);
  522. if (error)
  523. return error;
  524. /* Power down the device until we need it. */
  525. if (cyapa->operational)
  526. cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0, false);
  527. return 0;
  528. }
  529. static int cyapa_reinitialize(struct cyapa *cyapa)
  530. {
  531. struct device *dev = &cyapa->client->dev;
  532. struct input_dev *input = cyapa->input;
  533. int error;
  534. if (pm_runtime_enabled(dev))
  535. pm_runtime_disable(dev);
  536. /* Avoid command failures when TP was in OFF state. */
  537. if (cyapa->operational)
  538. cyapa->ops->set_power_mode(cyapa,
  539. PWR_MODE_FULL_ACTIVE, 0, false);
  540. error = cyapa_detect(cyapa);
  541. if (error)
  542. goto out;
  543. if (!input && cyapa->operational) {
  544. error = cyapa_create_input_dev(cyapa);
  545. if (error) {
  546. dev_err(dev, "create input_dev instance failed: %d\n",
  547. error);
  548. goto out;
  549. }
  550. }
  551. out:
  552. if (!input || !input->users) {
  553. /* Reset to power OFF state to save power when no user open. */
  554. if (cyapa->operational)
  555. cyapa->ops->set_power_mode(cyapa,
  556. PWR_MODE_OFF, 0, false);
  557. } else if (!error && cyapa->operational) {
  558. /*
  559. * Make sure only enable runtime PM when device is
  560. * in operational mode and input->users > 0.
  561. */
  562. pm_runtime_set_active(dev);
  563. pm_runtime_enable(dev);
  564. pm_runtime_get_sync(dev);
  565. pm_runtime_mark_last_busy(dev);
  566. pm_runtime_put_sync_autosuspend(dev);
  567. }
  568. return error;
  569. }
  570. static irqreturn_t cyapa_irq(int irq, void *dev_id)
  571. {
  572. struct cyapa *cyapa = dev_id;
  573. struct device *dev = &cyapa->client->dev;
  574. int error;
  575. if (device_may_wakeup(dev))
  576. pm_wakeup_event(dev, 0);
  577. /* Interrupt event can be caused by host command to trackpad device. */
  578. if (cyapa->ops->irq_cmd_handler(cyapa)) {
  579. /*
  580. * Interrupt event maybe from trackpad device input reporting.
  581. */
  582. if (!cyapa->input) {
  583. /*
  584. * Still in probing or in firmware image
  585. * updating or reading.
  586. */
  587. cyapa->ops->sort_empty_output_data(cyapa,
  588. NULL, NULL, NULL);
  589. goto out;
  590. }
  591. if (cyapa->operational) {
  592. error = cyapa->ops->irq_handler(cyapa);
  593. /*
  594. * Apply runtime power management to touch report event
  595. * except the events caused by the command responses.
  596. * Note:
  597. * It will introduce about 20~40 ms additional delay
  598. * time in receiving for first valid touch report data.
  599. * The time is used to execute device runtime resume
  600. * process.
  601. */
  602. pm_runtime_get_sync(dev);
  603. pm_runtime_mark_last_busy(dev);
  604. pm_runtime_put_sync_autosuspend(dev);
  605. }
  606. if (!cyapa->operational || error) {
  607. if (!mutex_trylock(&cyapa->state_sync_lock)) {
  608. cyapa->ops->sort_empty_output_data(cyapa,
  609. NULL, NULL, NULL);
  610. goto out;
  611. }
  612. cyapa_reinitialize(cyapa);
  613. mutex_unlock(&cyapa->state_sync_lock);
  614. }
  615. }
  616. out:
  617. return IRQ_HANDLED;
  618. }
  619. /*
  620. **************************************************************
  621. * sysfs interface
  622. **************************************************************
  623. */
  624. #ifdef CONFIG_PM_SLEEP
  625. static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
  626. struct device_attribute *attr,
  627. char *buf)
  628. {
  629. struct cyapa *cyapa = dev_get_drvdata(dev);
  630. u8 pwr_cmd = cyapa->suspend_power_mode;
  631. u16 sleep_time;
  632. int len;
  633. int error;
  634. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  635. if (error)
  636. return error;
  637. pwr_cmd = cyapa->suspend_power_mode;
  638. sleep_time = cyapa->suspend_sleep_time;
  639. mutex_unlock(&cyapa->state_sync_lock);
  640. switch (pwr_cmd) {
  641. case PWR_MODE_BTN_ONLY:
  642. len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
  643. break;
  644. case PWR_MODE_OFF:
  645. len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
  646. break;
  647. default:
  648. len = scnprintf(buf, PAGE_SIZE, "%u\n",
  649. cyapa->gen == CYAPA_GEN3 ?
  650. cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
  651. sleep_time);
  652. break;
  653. }
  654. return len;
  655. }
  656. static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
  657. struct device_attribute *attr,
  658. const char *buf, size_t count)
  659. {
  660. struct cyapa *cyapa = dev_get_drvdata(dev);
  661. u16 sleep_time;
  662. int error;
  663. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  664. if (error)
  665. return error;
  666. if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
  667. cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
  668. } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
  669. cyapa->suspend_power_mode = PWR_MODE_OFF;
  670. } else if (!kstrtou16(buf, 10, &sleep_time)) {
  671. cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
  672. cyapa->suspend_power_mode =
  673. cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
  674. } else {
  675. count = -EINVAL;
  676. }
  677. mutex_unlock(&cyapa->state_sync_lock);
  678. return count;
  679. }
  680. static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
  681. cyapa_show_suspend_scanrate,
  682. cyapa_update_suspend_scanrate);
  683. static struct attribute *cyapa_power_wakeup_entries[] = {
  684. &dev_attr_suspend_scanrate_ms.attr,
  685. NULL,
  686. };
  687. static const struct attribute_group cyapa_power_wakeup_group = {
  688. .name = power_group_name,
  689. .attrs = cyapa_power_wakeup_entries,
  690. };
  691. static void cyapa_remove_power_wakeup_group(void *data)
  692. {
  693. struct cyapa *cyapa = data;
  694. sysfs_unmerge_group(&cyapa->client->dev.kobj,
  695. &cyapa_power_wakeup_group);
  696. }
  697. static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
  698. {
  699. struct i2c_client *client = cyapa->client;
  700. struct device *dev = &client->dev;
  701. int error;
  702. if (device_can_wakeup(dev)) {
  703. error = sysfs_merge_group(&client->dev.kobj,
  704. &cyapa_power_wakeup_group);
  705. if (error) {
  706. dev_err(dev, "failed to add power wakeup group: %d\n",
  707. error);
  708. return error;
  709. }
  710. error = devm_add_action(dev,
  711. cyapa_remove_power_wakeup_group, cyapa);
  712. if (error) {
  713. cyapa_remove_power_wakeup_group(cyapa);
  714. dev_err(dev, "failed to add power cleanup action: %d\n",
  715. error);
  716. return error;
  717. }
  718. }
  719. return 0;
  720. }
  721. #else
  722. static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
  723. {
  724. return 0;
  725. }
  726. #endif /* CONFIG_PM_SLEEP */
  727. #ifdef CONFIG_PM
  728. static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
  729. struct device_attribute *attr,
  730. char *buf)
  731. {
  732. struct cyapa *cyapa = dev_get_drvdata(dev);
  733. u8 pwr_cmd;
  734. u16 sleep_time;
  735. int error;
  736. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  737. if (error)
  738. return error;
  739. pwr_cmd = cyapa->runtime_suspend_power_mode;
  740. sleep_time = cyapa->runtime_suspend_sleep_time;
  741. mutex_unlock(&cyapa->state_sync_lock);
  742. return scnprintf(buf, PAGE_SIZE, "%u\n",
  743. cyapa->gen == CYAPA_GEN3 ?
  744. cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
  745. sleep_time);
  746. }
  747. static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
  748. struct device_attribute *attr,
  749. const char *buf, size_t count)
  750. {
  751. struct cyapa *cyapa = dev_get_drvdata(dev);
  752. u16 time;
  753. int error;
  754. if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
  755. dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
  756. return -EINVAL;
  757. }
  758. /*
  759. * When the suspend scanrate is changed, pm_runtime_get to resume
  760. * a potentially suspended device, update to the new pwr_cmd
  761. * and then pm_runtime_put to suspend into the new power mode.
  762. */
  763. pm_runtime_get_sync(dev);
  764. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  765. if (error)
  766. return error;
  767. cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
  768. cyapa->runtime_suspend_power_mode =
  769. cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
  770. mutex_unlock(&cyapa->state_sync_lock);
  771. pm_runtime_put_sync_autosuspend(dev);
  772. return count;
  773. }
  774. static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
  775. cyapa_show_rt_suspend_scanrate,
  776. cyapa_update_rt_suspend_scanrate);
  777. static struct attribute *cyapa_power_runtime_entries[] = {
  778. &dev_attr_runtime_suspend_scanrate_ms.attr,
  779. NULL,
  780. };
  781. static const struct attribute_group cyapa_power_runtime_group = {
  782. .name = power_group_name,
  783. .attrs = cyapa_power_runtime_entries,
  784. };
  785. static void cyapa_remove_power_runtime_group(void *data)
  786. {
  787. struct cyapa *cyapa = data;
  788. sysfs_unmerge_group(&cyapa->client->dev.kobj,
  789. &cyapa_power_runtime_group);
  790. }
  791. static int cyapa_start_runtime(struct cyapa *cyapa)
  792. {
  793. struct device *dev = &cyapa->client->dev;
  794. int error;
  795. cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
  796. cyapa->runtime_suspend_sleep_time =
  797. cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
  798. error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
  799. if (error) {
  800. dev_err(dev,
  801. "failed to create power runtime group: %d\n", error);
  802. return error;
  803. }
  804. error = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa);
  805. if (error) {
  806. cyapa_remove_power_runtime_group(cyapa);
  807. dev_err(dev,
  808. "failed to add power runtime cleanup action: %d\n",
  809. error);
  810. return error;
  811. }
  812. /* runtime is enabled until device is operational and opened. */
  813. pm_runtime_set_suspended(dev);
  814. pm_runtime_use_autosuspend(dev);
  815. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
  816. return 0;
  817. }
  818. #else
  819. static inline int cyapa_start_runtime(struct cyapa *cyapa)
  820. {
  821. return 0;
  822. }
  823. #endif /* CONFIG_PM */
  824. static ssize_t cyapa_show_fm_ver(struct device *dev,
  825. struct device_attribute *attr, char *buf)
  826. {
  827. int error;
  828. struct cyapa *cyapa = dev_get_drvdata(dev);
  829. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  830. if (error)
  831. return error;
  832. error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
  833. cyapa->fw_min_ver);
  834. mutex_unlock(&cyapa->state_sync_lock);
  835. return error;
  836. }
  837. static ssize_t cyapa_show_product_id(struct device *dev,
  838. struct device_attribute *attr, char *buf)
  839. {
  840. struct cyapa *cyapa = dev_get_drvdata(dev);
  841. int size;
  842. int error;
  843. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  844. if (error)
  845. return error;
  846. size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
  847. mutex_unlock(&cyapa->state_sync_lock);
  848. return size;
  849. }
  850. static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
  851. {
  852. struct device *dev = &cyapa->client->dev;
  853. const struct firmware *fw;
  854. int error;
  855. error = request_firmware(&fw, fw_name, dev);
  856. if (error) {
  857. dev_err(dev, "Could not load firmware from %s: %d\n",
  858. fw_name, error);
  859. return error;
  860. }
  861. error = cyapa->ops->check_fw(cyapa, fw);
  862. if (error) {
  863. dev_err(dev, "Invalid CYAPA firmware image: %s\n",
  864. fw_name);
  865. goto done;
  866. }
  867. /*
  868. * Resume the potentially suspended device because doing FW
  869. * update on a device not in the FULL mode has a chance to
  870. * fail.
  871. */
  872. pm_runtime_get_sync(dev);
  873. /* Require IRQ support for firmware update commands. */
  874. cyapa_enable_irq_for_cmd(cyapa);
  875. error = cyapa->ops->bl_enter(cyapa);
  876. if (error) {
  877. dev_err(dev, "bl_enter failed, %d\n", error);
  878. goto err_detect;
  879. }
  880. error = cyapa->ops->bl_activate(cyapa);
  881. if (error) {
  882. dev_err(dev, "bl_activate failed, %d\n", error);
  883. goto err_detect;
  884. }
  885. error = cyapa->ops->bl_initiate(cyapa, fw);
  886. if (error) {
  887. dev_err(dev, "bl_initiate failed, %d\n", error);
  888. goto err_detect;
  889. }
  890. error = cyapa->ops->update_fw(cyapa, fw);
  891. if (error) {
  892. dev_err(dev, "update_fw failed, %d\n", error);
  893. goto err_detect;
  894. }
  895. err_detect:
  896. cyapa_disable_irq_for_cmd(cyapa);
  897. pm_runtime_put_noidle(dev);
  898. done:
  899. release_firmware(fw);
  900. return error;
  901. }
  902. static ssize_t cyapa_update_fw_store(struct device *dev,
  903. struct device_attribute *attr,
  904. const char *buf, size_t count)
  905. {
  906. struct cyapa *cyapa = dev_get_drvdata(dev);
  907. char fw_name[NAME_MAX];
  908. int ret, error;
  909. if (count >= NAME_MAX) {
  910. dev_err(dev, "File name too long\n");
  911. return -EINVAL;
  912. }
  913. memcpy(fw_name, buf, count);
  914. if (fw_name[count - 1] == '\n')
  915. fw_name[count - 1] = '\0';
  916. else
  917. fw_name[count] = '\0';
  918. if (cyapa->input) {
  919. /*
  920. * Force the input device to be registered after the firmware
  921. * image is updated, so if the corresponding parameters updated
  922. * in the new firmware image can taken effect immediately.
  923. */
  924. input_unregister_device(cyapa->input);
  925. cyapa->input = NULL;
  926. }
  927. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  928. if (error) {
  929. /*
  930. * Whatever, do reinitialize to try to recover TP state to
  931. * previous state just as it entered fw update entrance.
  932. */
  933. cyapa_reinitialize(cyapa);
  934. return error;
  935. }
  936. error = cyapa_firmware(cyapa, fw_name);
  937. if (error)
  938. dev_err(dev, "firmware update failed: %d\n", error);
  939. else
  940. dev_dbg(dev, "firmware update successfully done.\n");
  941. /*
  942. * Re-detect trackpad device states because firmware update process
  943. * will reset trackpad device into bootloader mode.
  944. */
  945. ret = cyapa_reinitialize(cyapa);
  946. if (ret) {
  947. dev_err(dev, "failed to re-detect after updated: %d\n", ret);
  948. error = error ? error : ret;
  949. }
  950. mutex_unlock(&cyapa->state_sync_lock);
  951. return error ? error : count;
  952. }
  953. static ssize_t cyapa_calibrate_store(struct device *dev,
  954. struct device_attribute *attr,
  955. const char *buf, size_t count)
  956. {
  957. struct cyapa *cyapa = dev_get_drvdata(dev);
  958. int error;
  959. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  960. if (error)
  961. return error;
  962. if (cyapa->operational) {
  963. cyapa_enable_irq_for_cmd(cyapa);
  964. error = cyapa->ops->calibrate_store(dev, attr, buf, count);
  965. cyapa_disable_irq_for_cmd(cyapa);
  966. } else {
  967. error = -EBUSY; /* Still running in bootloader mode. */
  968. }
  969. mutex_unlock(&cyapa->state_sync_lock);
  970. return error < 0 ? error : count;
  971. }
  972. static ssize_t cyapa_show_baseline(struct device *dev,
  973. struct device_attribute *attr, char *buf)
  974. {
  975. struct cyapa *cyapa = dev_get_drvdata(dev);
  976. ssize_t error;
  977. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  978. if (error)
  979. return error;
  980. if (cyapa->operational) {
  981. cyapa_enable_irq_for_cmd(cyapa);
  982. error = cyapa->ops->show_baseline(dev, attr, buf);
  983. cyapa_disable_irq_for_cmd(cyapa);
  984. } else {
  985. error = -EBUSY; /* Still running in bootloader mode. */
  986. }
  987. mutex_unlock(&cyapa->state_sync_lock);
  988. return error;
  989. }
  990. static char *cyapa_state_to_string(struct cyapa *cyapa)
  991. {
  992. switch (cyapa->state) {
  993. case CYAPA_STATE_BL_BUSY:
  994. return "bootloader busy";
  995. case CYAPA_STATE_BL_IDLE:
  996. return "bootloader idle";
  997. case CYAPA_STATE_BL_ACTIVE:
  998. return "bootloader active";
  999. case CYAPA_STATE_GEN5_BL:
  1000. case CYAPA_STATE_GEN6_BL:
  1001. return "bootloader";
  1002. case CYAPA_STATE_OP:
  1003. case CYAPA_STATE_GEN5_APP:
  1004. case CYAPA_STATE_GEN6_APP:
  1005. return "operational"; /* Normal valid state. */
  1006. default:
  1007. return "invalid mode";
  1008. }
  1009. }
  1010. static ssize_t cyapa_show_mode(struct device *dev,
  1011. struct device_attribute *attr, char *buf)
  1012. {
  1013. struct cyapa *cyapa = dev_get_drvdata(dev);
  1014. int size;
  1015. int error;
  1016. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  1017. if (error)
  1018. return error;
  1019. size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
  1020. cyapa->gen, cyapa_state_to_string(cyapa));
  1021. mutex_unlock(&cyapa->state_sync_lock);
  1022. return size;
  1023. }
  1024. static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
  1025. static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
  1026. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
  1027. static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
  1028. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
  1029. static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
  1030. static struct attribute *cyapa_sysfs_entries[] = {
  1031. &dev_attr_firmware_version.attr,
  1032. &dev_attr_product_id.attr,
  1033. &dev_attr_update_fw.attr,
  1034. &dev_attr_baseline.attr,
  1035. &dev_attr_calibrate.attr,
  1036. &dev_attr_mode.attr,
  1037. NULL,
  1038. };
  1039. static const struct attribute_group cyapa_sysfs_group = {
  1040. .attrs = cyapa_sysfs_entries,
  1041. };
  1042. static void cyapa_remove_sysfs_group(void *data)
  1043. {
  1044. struct cyapa *cyapa = data;
  1045. sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group);
  1046. }
  1047. static void cyapa_disable_regulator(void *data)
  1048. {
  1049. struct cyapa *cyapa = data;
  1050. regulator_disable(cyapa->vcc);
  1051. }
  1052. static int cyapa_probe(struct i2c_client *client,
  1053. const struct i2c_device_id *dev_id)
  1054. {
  1055. struct device *dev = &client->dev;
  1056. struct cyapa *cyapa;
  1057. u8 adapter_func;
  1058. union i2c_smbus_data dummy;
  1059. int error;
  1060. adapter_func = cyapa_check_adapter_functionality(client);
  1061. if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
  1062. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  1063. return -EIO;
  1064. }
  1065. /* Make sure there is something at this address */
  1066. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  1067. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
  1068. return -ENODEV;
  1069. cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
  1070. if (!cyapa)
  1071. return -ENOMEM;
  1072. /* i2c isn't supported, use smbus */
  1073. if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
  1074. cyapa->smbus = true;
  1075. cyapa->client = client;
  1076. i2c_set_clientdata(client, cyapa);
  1077. sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
  1078. client->addr);
  1079. cyapa->vcc = devm_regulator_get(dev, "vcc");
  1080. if (IS_ERR(cyapa->vcc)) {
  1081. error = PTR_ERR(cyapa->vcc);
  1082. dev_err(dev, "failed to get vcc regulator: %d\n", error);
  1083. return error;
  1084. }
  1085. error = regulator_enable(cyapa->vcc);
  1086. if (error) {
  1087. dev_err(dev, "failed to enable regulator: %d\n", error);
  1088. return error;
  1089. }
  1090. error = devm_add_action(dev, cyapa_disable_regulator, cyapa);
  1091. if (error) {
  1092. cyapa_disable_regulator(cyapa);
  1093. dev_err(dev, "failed to add disable regulator action: %d\n",
  1094. error);
  1095. return error;
  1096. }
  1097. error = cyapa_initialize(cyapa);
  1098. if (error) {
  1099. dev_err(dev, "failed to detect and initialize tp device.\n");
  1100. return error;
  1101. }
  1102. error = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group);
  1103. if (error) {
  1104. dev_err(dev, "failed to create sysfs entries: %d\n", error);
  1105. return error;
  1106. }
  1107. error = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa);
  1108. if (error) {
  1109. cyapa_remove_sysfs_group(cyapa);
  1110. dev_err(dev, "failed to add sysfs cleanup action: %d\n", error);
  1111. return error;
  1112. }
  1113. error = cyapa_prepare_wakeup_controls(cyapa);
  1114. if (error) {
  1115. dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
  1116. return error;
  1117. }
  1118. error = cyapa_start_runtime(cyapa);
  1119. if (error) {
  1120. dev_err(dev, "failed to start pm_runtime: %d\n", error);
  1121. return error;
  1122. }
  1123. error = devm_request_threaded_irq(dev, client->irq,
  1124. NULL, cyapa_irq,
  1125. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1126. "cyapa", cyapa);
  1127. if (error) {
  1128. dev_err(dev, "failed to request threaded irq: %d\n", error);
  1129. return error;
  1130. }
  1131. /* Disable IRQ until the device is opened */
  1132. disable_irq(client->irq);
  1133. /*
  1134. * Register the device in the input subsystem when it's operational.
  1135. * Otherwise, keep in this driver, so it can be be recovered or updated
  1136. * through the sysfs mode and update_fw interfaces by user or apps.
  1137. */
  1138. if (cyapa->operational) {
  1139. error = cyapa_create_input_dev(cyapa);
  1140. if (error) {
  1141. dev_err(dev, "create input_dev instance failed: %d\n",
  1142. error);
  1143. return error;
  1144. }
  1145. }
  1146. return 0;
  1147. }
  1148. static int __maybe_unused cyapa_suspend(struct device *dev)
  1149. {
  1150. struct i2c_client *client = to_i2c_client(dev);
  1151. struct cyapa *cyapa = i2c_get_clientdata(client);
  1152. u8 power_mode;
  1153. int error;
  1154. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  1155. if (error)
  1156. return error;
  1157. /*
  1158. * Runtime PM is enable only when device is in operational mode and
  1159. * users in use, so need check it before disable it to
  1160. * avoid unbalance warning.
  1161. */
  1162. if (pm_runtime_enabled(dev))
  1163. pm_runtime_disable(dev);
  1164. disable_irq(client->irq);
  1165. /*
  1166. * Set trackpad device to idle mode if wakeup is allowed,
  1167. * otherwise turn off.
  1168. */
  1169. if (cyapa->operational) {
  1170. power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
  1171. : PWR_MODE_OFF;
  1172. error = cyapa->ops->set_power_mode(cyapa, power_mode,
  1173. cyapa->suspend_sleep_time, true);
  1174. if (error)
  1175. dev_err(dev, "suspend set power mode failed: %d\n",
  1176. error);
  1177. }
  1178. /*
  1179. * Disable proximity interrupt when system idle, want true touch to
  1180. * wake the system.
  1181. */
  1182. if (cyapa->dev_pwr_mode != PWR_MODE_OFF)
  1183. cyapa->ops->set_proximity(cyapa, false);
  1184. if (device_may_wakeup(dev))
  1185. cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
  1186. mutex_unlock(&cyapa->state_sync_lock);
  1187. return 0;
  1188. }
  1189. static int __maybe_unused cyapa_resume(struct device *dev)
  1190. {
  1191. struct i2c_client *client = to_i2c_client(dev);
  1192. struct cyapa *cyapa = i2c_get_clientdata(client);
  1193. int error;
  1194. mutex_lock(&cyapa->state_sync_lock);
  1195. if (device_may_wakeup(dev) && cyapa->irq_wake) {
  1196. disable_irq_wake(client->irq);
  1197. cyapa->irq_wake = false;
  1198. }
  1199. /*
  1200. * Update device states and runtime PM states.
  1201. * Re-Enable proximity interrupt after enter operational mode.
  1202. */
  1203. error = cyapa_reinitialize(cyapa);
  1204. if (error)
  1205. dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
  1206. enable_irq(client->irq);
  1207. mutex_unlock(&cyapa->state_sync_lock);
  1208. return 0;
  1209. }
  1210. static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
  1211. {
  1212. struct cyapa *cyapa = dev_get_drvdata(dev);
  1213. int error;
  1214. error = cyapa->ops->set_power_mode(cyapa,
  1215. cyapa->runtime_suspend_power_mode,
  1216. cyapa->runtime_suspend_sleep_time,
  1217. false);
  1218. if (error)
  1219. dev_warn(dev, "runtime suspend failed: %d\n", error);
  1220. return 0;
  1221. }
  1222. static int __maybe_unused cyapa_runtime_resume(struct device *dev)
  1223. {
  1224. struct cyapa *cyapa = dev_get_drvdata(dev);
  1225. int error;
  1226. error = cyapa->ops->set_power_mode(cyapa,
  1227. PWR_MODE_FULL_ACTIVE, 0, false);
  1228. if (error)
  1229. dev_warn(dev, "runtime resume failed: %d\n", error);
  1230. return 0;
  1231. }
  1232. static const struct dev_pm_ops cyapa_pm_ops = {
  1233. SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
  1234. SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
  1235. };
  1236. static const struct i2c_device_id cyapa_id_table[] = {
  1237. { "cyapa", 0 },
  1238. { },
  1239. };
  1240. MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
  1241. #ifdef CONFIG_ACPI
  1242. static const struct acpi_device_id cyapa_acpi_id[] = {
  1243. { "CYAP0000", 0 }, /* Gen3 trackpad with 0x67 I2C address. */
  1244. { "CYAP0001", 0 }, /* Gen5 trackpad with 0x24 I2C address. */
  1245. { "CYAP0002", 0 }, /* Gen6 trackpad with 0x24 I2C address. */
  1246. { }
  1247. };
  1248. MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
  1249. #endif
  1250. #ifdef CONFIG_OF
  1251. static const struct of_device_id cyapa_of_match[] = {
  1252. { .compatible = "cypress,cyapa" },
  1253. { /* sentinel */ }
  1254. };
  1255. MODULE_DEVICE_TABLE(of, cyapa_of_match);
  1256. #endif
  1257. static struct i2c_driver cyapa_driver = {
  1258. .driver = {
  1259. .name = "cyapa",
  1260. .pm = &cyapa_pm_ops,
  1261. .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
  1262. .of_match_table = of_match_ptr(cyapa_of_match),
  1263. },
  1264. .probe = cyapa_probe,
  1265. .id_table = cyapa_id_table,
  1266. };
  1267. module_i2c_driver(cyapa_driver);
  1268. MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
  1269. MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
  1270. MODULE_LICENSE("GPL");