cyapa.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /*
  2. * Cypress APA trackpad with I2C interface
  3. *
  4. * Author: Dudley Du <[email protected]>
  5. * Further cleanup and restructuring by:
  6. * Daniel Kurtz <[email protected]>
  7. * Benson Leung <[email protected]>
  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. * @reg: 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_device_enabled(input)) {
  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_device_enabled(input)) {
  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_device_enabled(input)) {
  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_or_reset(dev,
  713. cyapa_remove_power_wakeup_group, cyapa);
  714. if (error) {
  715. dev_err(dev, "failed to add power cleanup action: %d\n",
  716. error);
  717. return error;
  718. }
  719. }
  720. return 0;
  721. }
  722. #else
  723. static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
  724. {
  725. return 0;
  726. }
  727. #endif /* CONFIG_PM_SLEEP */
  728. #ifdef CONFIG_PM
  729. static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
  730. struct device_attribute *attr,
  731. char *buf)
  732. {
  733. struct cyapa *cyapa = dev_get_drvdata(dev);
  734. u8 pwr_cmd;
  735. u16 sleep_time;
  736. int error;
  737. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  738. if (error)
  739. return error;
  740. pwr_cmd = cyapa->runtime_suspend_power_mode;
  741. sleep_time = cyapa->runtime_suspend_sleep_time;
  742. mutex_unlock(&cyapa->state_sync_lock);
  743. return scnprintf(buf, PAGE_SIZE, "%u\n",
  744. cyapa->gen == CYAPA_GEN3 ?
  745. cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
  746. sleep_time);
  747. }
  748. static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
  749. struct device_attribute *attr,
  750. const char *buf, size_t count)
  751. {
  752. struct cyapa *cyapa = dev_get_drvdata(dev);
  753. u16 time;
  754. int error;
  755. if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
  756. dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
  757. return -EINVAL;
  758. }
  759. /*
  760. * When the suspend scanrate is changed, pm_runtime_get to resume
  761. * a potentially suspended device, update to the new pwr_cmd
  762. * and then pm_runtime_put to suspend into the new power mode.
  763. */
  764. pm_runtime_get_sync(dev);
  765. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  766. if (error)
  767. return error;
  768. cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
  769. cyapa->runtime_suspend_power_mode =
  770. cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
  771. mutex_unlock(&cyapa->state_sync_lock);
  772. pm_runtime_put_sync_autosuspend(dev);
  773. return count;
  774. }
  775. static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
  776. cyapa_show_rt_suspend_scanrate,
  777. cyapa_update_rt_suspend_scanrate);
  778. static struct attribute *cyapa_power_runtime_entries[] = {
  779. &dev_attr_runtime_suspend_scanrate_ms.attr,
  780. NULL,
  781. };
  782. static const struct attribute_group cyapa_power_runtime_group = {
  783. .name = power_group_name,
  784. .attrs = cyapa_power_runtime_entries,
  785. };
  786. static void cyapa_remove_power_runtime_group(void *data)
  787. {
  788. struct cyapa *cyapa = data;
  789. sysfs_unmerge_group(&cyapa->client->dev.kobj,
  790. &cyapa_power_runtime_group);
  791. }
  792. static int cyapa_start_runtime(struct cyapa *cyapa)
  793. {
  794. struct device *dev = &cyapa->client->dev;
  795. int error;
  796. cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
  797. cyapa->runtime_suspend_sleep_time =
  798. cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
  799. error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
  800. if (error) {
  801. dev_err(dev,
  802. "failed to create power runtime group: %d\n", error);
  803. return error;
  804. }
  805. error = devm_add_action_or_reset(dev, cyapa_remove_power_runtime_group,
  806. cyapa);
  807. if (error) {
  808. dev_err(dev,
  809. "failed to add power runtime cleanup action: %d\n",
  810. error);
  811. return error;
  812. }
  813. /* runtime is enabled until device is operational and opened. */
  814. pm_runtime_set_suspended(dev);
  815. pm_runtime_use_autosuspend(dev);
  816. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
  817. return 0;
  818. }
  819. #else
  820. static inline int cyapa_start_runtime(struct cyapa *cyapa)
  821. {
  822. return 0;
  823. }
  824. #endif /* CONFIG_PM */
  825. static ssize_t cyapa_show_fm_ver(struct device *dev,
  826. struct device_attribute *attr, char *buf)
  827. {
  828. int error;
  829. struct cyapa *cyapa = dev_get_drvdata(dev);
  830. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  831. if (error)
  832. return error;
  833. error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
  834. cyapa->fw_min_ver);
  835. mutex_unlock(&cyapa->state_sync_lock);
  836. return error;
  837. }
  838. static ssize_t cyapa_show_product_id(struct device *dev,
  839. struct device_attribute *attr, char *buf)
  840. {
  841. struct cyapa *cyapa = dev_get_drvdata(dev);
  842. int size;
  843. int error;
  844. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  845. if (error)
  846. return error;
  847. size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
  848. mutex_unlock(&cyapa->state_sync_lock);
  849. return size;
  850. }
  851. static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
  852. {
  853. struct device *dev = &cyapa->client->dev;
  854. const struct firmware *fw;
  855. int error;
  856. error = request_firmware(&fw, fw_name, dev);
  857. if (error) {
  858. dev_err(dev, "Could not load firmware from %s: %d\n",
  859. fw_name, error);
  860. return error;
  861. }
  862. error = cyapa->ops->check_fw(cyapa, fw);
  863. if (error) {
  864. dev_err(dev, "Invalid CYAPA firmware image: %s\n",
  865. fw_name);
  866. goto done;
  867. }
  868. /*
  869. * Resume the potentially suspended device because doing FW
  870. * update on a device not in the FULL mode has a chance to
  871. * fail.
  872. */
  873. pm_runtime_get_sync(dev);
  874. /* Require IRQ support for firmware update commands. */
  875. cyapa_enable_irq_for_cmd(cyapa);
  876. error = cyapa->ops->bl_enter(cyapa);
  877. if (error) {
  878. dev_err(dev, "bl_enter failed, %d\n", error);
  879. goto err_detect;
  880. }
  881. error = cyapa->ops->bl_activate(cyapa);
  882. if (error) {
  883. dev_err(dev, "bl_activate failed, %d\n", error);
  884. goto err_detect;
  885. }
  886. error = cyapa->ops->bl_initiate(cyapa, fw);
  887. if (error) {
  888. dev_err(dev, "bl_initiate failed, %d\n", error);
  889. goto err_detect;
  890. }
  891. error = cyapa->ops->update_fw(cyapa, fw);
  892. if (error) {
  893. dev_err(dev, "update_fw failed, %d\n", error);
  894. goto err_detect;
  895. }
  896. err_detect:
  897. cyapa_disable_irq_for_cmd(cyapa);
  898. pm_runtime_put_noidle(dev);
  899. done:
  900. release_firmware(fw);
  901. return error;
  902. }
  903. static ssize_t cyapa_update_fw_store(struct device *dev,
  904. struct device_attribute *attr,
  905. const char *buf, size_t count)
  906. {
  907. struct cyapa *cyapa = dev_get_drvdata(dev);
  908. char fw_name[NAME_MAX];
  909. int ret, error;
  910. if (count >= NAME_MAX) {
  911. dev_err(dev, "File name too long\n");
  912. return -EINVAL;
  913. }
  914. memcpy(fw_name, buf, count);
  915. if (fw_name[count - 1] == '\n')
  916. fw_name[count - 1] = '\0';
  917. else
  918. fw_name[count] = '\0';
  919. if (cyapa->input) {
  920. /*
  921. * Force the input device to be registered after the firmware
  922. * image is updated, so if the corresponding parameters updated
  923. * in the new firmware image can taken effect immediately.
  924. */
  925. input_unregister_device(cyapa->input);
  926. cyapa->input = NULL;
  927. }
  928. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  929. if (error) {
  930. /*
  931. * Whatever, do reinitialize to try to recover TP state to
  932. * previous state just as it entered fw update entrance.
  933. */
  934. cyapa_reinitialize(cyapa);
  935. return error;
  936. }
  937. error = cyapa_firmware(cyapa, fw_name);
  938. if (error)
  939. dev_err(dev, "firmware update failed: %d\n", error);
  940. else
  941. dev_dbg(dev, "firmware update successfully done.\n");
  942. /*
  943. * Re-detect trackpad device states because firmware update process
  944. * will reset trackpad device into bootloader mode.
  945. */
  946. ret = cyapa_reinitialize(cyapa);
  947. if (ret) {
  948. dev_err(dev, "failed to re-detect after updated: %d\n", ret);
  949. error = error ? error : ret;
  950. }
  951. mutex_unlock(&cyapa->state_sync_lock);
  952. return error ? error : count;
  953. }
  954. static ssize_t cyapa_calibrate_store(struct device *dev,
  955. struct device_attribute *attr,
  956. const char *buf, size_t count)
  957. {
  958. struct cyapa *cyapa = dev_get_drvdata(dev);
  959. int error;
  960. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  961. if (error)
  962. return error;
  963. if (cyapa->operational) {
  964. cyapa_enable_irq_for_cmd(cyapa);
  965. error = cyapa->ops->calibrate_store(dev, attr, buf, count);
  966. cyapa_disable_irq_for_cmd(cyapa);
  967. } else {
  968. error = -EBUSY; /* Still running in bootloader mode. */
  969. }
  970. mutex_unlock(&cyapa->state_sync_lock);
  971. return error < 0 ? error : count;
  972. }
  973. static ssize_t cyapa_show_baseline(struct device *dev,
  974. struct device_attribute *attr, char *buf)
  975. {
  976. struct cyapa *cyapa = dev_get_drvdata(dev);
  977. ssize_t error;
  978. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  979. if (error)
  980. return error;
  981. if (cyapa->operational) {
  982. cyapa_enable_irq_for_cmd(cyapa);
  983. error = cyapa->ops->show_baseline(dev, attr, buf);
  984. cyapa_disable_irq_for_cmd(cyapa);
  985. } else {
  986. error = -EBUSY; /* Still running in bootloader mode. */
  987. }
  988. mutex_unlock(&cyapa->state_sync_lock);
  989. return error;
  990. }
  991. static char *cyapa_state_to_string(struct cyapa *cyapa)
  992. {
  993. switch (cyapa->state) {
  994. case CYAPA_STATE_BL_BUSY:
  995. return "bootloader busy";
  996. case CYAPA_STATE_BL_IDLE:
  997. return "bootloader idle";
  998. case CYAPA_STATE_BL_ACTIVE:
  999. return "bootloader active";
  1000. case CYAPA_STATE_GEN5_BL:
  1001. case CYAPA_STATE_GEN6_BL:
  1002. return "bootloader";
  1003. case CYAPA_STATE_OP:
  1004. case CYAPA_STATE_GEN5_APP:
  1005. case CYAPA_STATE_GEN6_APP:
  1006. return "operational"; /* Normal valid state. */
  1007. default:
  1008. return "invalid mode";
  1009. }
  1010. }
  1011. static ssize_t cyapa_show_mode(struct device *dev,
  1012. struct device_attribute *attr, char *buf)
  1013. {
  1014. struct cyapa *cyapa = dev_get_drvdata(dev);
  1015. int size;
  1016. int error;
  1017. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  1018. if (error)
  1019. return error;
  1020. size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
  1021. cyapa->gen, cyapa_state_to_string(cyapa));
  1022. mutex_unlock(&cyapa->state_sync_lock);
  1023. return size;
  1024. }
  1025. static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
  1026. static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
  1027. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
  1028. static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
  1029. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
  1030. static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
  1031. static struct attribute *cyapa_sysfs_entries[] = {
  1032. &dev_attr_firmware_version.attr,
  1033. &dev_attr_product_id.attr,
  1034. &dev_attr_update_fw.attr,
  1035. &dev_attr_baseline.attr,
  1036. &dev_attr_calibrate.attr,
  1037. &dev_attr_mode.attr,
  1038. NULL,
  1039. };
  1040. static const struct attribute_group cyapa_sysfs_group = {
  1041. .attrs = cyapa_sysfs_entries,
  1042. };
  1043. static void cyapa_disable_regulator(void *data)
  1044. {
  1045. struct cyapa *cyapa = data;
  1046. regulator_disable(cyapa->vcc);
  1047. }
  1048. static int cyapa_probe(struct i2c_client *client,
  1049. const struct i2c_device_id *dev_id)
  1050. {
  1051. struct device *dev = &client->dev;
  1052. struct cyapa *cyapa;
  1053. u8 adapter_func;
  1054. union i2c_smbus_data dummy;
  1055. int error;
  1056. adapter_func = cyapa_check_adapter_functionality(client);
  1057. if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
  1058. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  1059. return -EIO;
  1060. }
  1061. /* Make sure there is something at this address */
  1062. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  1063. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
  1064. return -ENODEV;
  1065. cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
  1066. if (!cyapa)
  1067. return -ENOMEM;
  1068. /* i2c isn't supported, use smbus */
  1069. if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
  1070. cyapa->smbus = true;
  1071. cyapa->client = client;
  1072. i2c_set_clientdata(client, cyapa);
  1073. sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
  1074. client->addr);
  1075. cyapa->vcc = devm_regulator_get(dev, "vcc");
  1076. if (IS_ERR(cyapa->vcc)) {
  1077. error = PTR_ERR(cyapa->vcc);
  1078. dev_err(dev, "failed to get vcc regulator: %d\n", error);
  1079. return error;
  1080. }
  1081. error = regulator_enable(cyapa->vcc);
  1082. if (error) {
  1083. dev_err(dev, "failed to enable regulator: %d\n", error);
  1084. return error;
  1085. }
  1086. error = devm_add_action_or_reset(dev, cyapa_disable_regulator, cyapa);
  1087. if (error) {
  1088. dev_err(dev, "failed to add disable regulator action: %d\n",
  1089. error);
  1090. return error;
  1091. }
  1092. error = cyapa_initialize(cyapa);
  1093. if (error) {
  1094. dev_err(dev, "failed to detect and initialize tp device.\n");
  1095. return error;
  1096. }
  1097. error = devm_device_add_group(dev, &cyapa_sysfs_group);
  1098. if (error) {
  1099. dev_err(dev, "failed to create sysfs entries: %d\n", error);
  1100. return error;
  1101. }
  1102. error = cyapa_prepare_wakeup_controls(cyapa);
  1103. if (error) {
  1104. dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
  1105. return error;
  1106. }
  1107. error = cyapa_start_runtime(cyapa);
  1108. if (error) {
  1109. dev_err(dev, "failed to start pm_runtime: %d\n", error);
  1110. return error;
  1111. }
  1112. error = devm_request_threaded_irq(dev, client->irq,
  1113. NULL, cyapa_irq,
  1114. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1115. "cyapa", cyapa);
  1116. if (error) {
  1117. dev_err(dev, "failed to request threaded irq: %d\n", error);
  1118. return error;
  1119. }
  1120. /* Disable IRQ until the device is opened */
  1121. disable_irq(client->irq);
  1122. /*
  1123. * Register the device in the input subsystem when it's operational.
  1124. * Otherwise, keep in this driver, so it can be be recovered or updated
  1125. * through the sysfs mode and update_fw interfaces by user or apps.
  1126. */
  1127. if (cyapa->operational) {
  1128. error = cyapa_create_input_dev(cyapa);
  1129. if (error) {
  1130. dev_err(dev, "create input_dev instance failed: %d\n",
  1131. error);
  1132. return error;
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. static int __maybe_unused cyapa_suspend(struct device *dev)
  1138. {
  1139. struct i2c_client *client = to_i2c_client(dev);
  1140. struct cyapa *cyapa = i2c_get_clientdata(client);
  1141. u8 power_mode;
  1142. int error;
  1143. error = mutex_lock_interruptible(&cyapa->state_sync_lock);
  1144. if (error)
  1145. return error;
  1146. /*
  1147. * Runtime PM is enable only when device is in operational mode and
  1148. * users in use, so need check it before disable it to
  1149. * avoid unbalance warning.
  1150. */
  1151. if (pm_runtime_enabled(dev))
  1152. pm_runtime_disable(dev);
  1153. disable_irq(client->irq);
  1154. /*
  1155. * Set trackpad device to idle mode if wakeup is allowed,
  1156. * otherwise turn off.
  1157. */
  1158. if (cyapa->operational) {
  1159. power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
  1160. : PWR_MODE_OFF;
  1161. error = cyapa->ops->set_power_mode(cyapa, power_mode,
  1162. cyapa->suspend_sleep_time, CYAPA_PM_SUSPEND);
  1163. if (error)
  1164. dev_err(dev, "suspend set power mode failed: %d\n",
  1165. error);
  1166. }
  1167. /*
  1168. * Disable proximity interrupt when system idle, want true touch to
  1169. * wake the system.
  1170. */
  1171. if (cyapa->dev_pwr_mode != PWR_MODE_OFF)
  1172. cyapa->ops->set_proximity(cyapa, false);
  1173. if (device_may_wakeup(dev))
  1174. cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
  1175. mutex_unlock(&cyapa->state_sync_lock);
  1176. return 0;
  1177. }
  1178. static int __maybe_unused cyapa_resume(struct device *dev)
  1179. {
  1180. struct i2c_client *client = to_i2c_client(dev);
  1181. struct cyapa *cyapa = i2c_get_clientdata(client);
  1182. int error;
  1183. mutex_lock(&cyapa->state_sync_lock);
  1184. if (device_may_wakeup(dev) && cyapa->irq_wake) {
  1185. disable_irq_wake(client->irq);
  1186. cyapa->irq_wake = false;
  1187. }
  1188. /*
  1189. * Update device states and runtime PM states.
  1190. * Re-Enable proximity interrupt after enter operational mode.
  1191. */
  1192. error = cyapa_reinitialize(cyapa);
  1193. if (error)
  1194. dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
  1195. enable_irq(client->irq);
  1196. mutex_unlock(&cyapa->state_sync_lock);
  1197. return 0;
  1198. }
  1199. static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
  1200. {
  1201. struct cyapa *cyapa = dev_get_drvdata(dev);
  1202. int error;
  1203. error = cyapa->ops->set_power_mode(cyapa,
  1204. cyapa->runtime_suspend_power_mode,
  1205. cyapa->runtime_suspend_sleep_time,
  1206. CYAPA_PM_RUNTIME_SUSPEND);
  1207. if (error)
  1208. dev_warn(dev, "runtime suspend failed: %d\n", error);
  1209. return 0;
  1210. }
  1211. static int __maybe_unused cyapa_runtime_resume(struct device *dev)
  1212. {
  1213. struct cyapa *cyapa = dev_get_drvdata(dev);
  1214. int error;
  1215. error = cyapa->ops->set_power_mode(cyapa,
  1216. PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_RUNTIME_RESUME);
  1217. if (error)
  1218. dev_warn(dev, "runtime resume failed: %d\n", error);
  1219. return 0;
  1220. }
  1221. static const struct dev_pm_ops cyapa_pm_ops = {
  1222. SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
  1223. SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
  1224. };
  1225. static const struct i2c_device_id cyapa_id_table[] = {
  1226. { "cyapa", 0 },
  1227. { },
  1228. };
  1229. MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
  1230. #ifdef CONFIG_ACPI
  1231. static const struct acpi_device_id cyapa_acpi_id[] = {
  1232. { "CYAP0000", 0 }, /* Gen3 trackpad with 0x67 I2C address. */
  1233. { "CYAP0001", 0 }, /* Gen5 trackpad with 0x24 I2C address. */
  1234. { "CYAP0002", 0 }, /* Gen6 trackpad with 0x24 I2C address. */
  1235. { }
  1236. };
  1237. MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
  1238. #endif
  1239. #ifdef CONFIG_OF
  1240. static const struct of_device_id cyapa_of_match[] = {
  1241. { .compatible = "cypress,cyapa" },
  1242. { /* sentinel */ }
  1243. };
  1244. MODULE_DEVICE_TABLE(of, cyapa_of_match);
  1245. #endif
  1246. static struct i2c_driver cyapa_driver = {
  1247. .driver = {
  1248. .name = "cyapa",
  1249. .pm = &cyapa_pm_ops,
  1250. .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
  1251. .of_match_table = of_match_ptr(cyapa_of_match),
  1252. },
  1253. .probe = cyapa_probe,
  1254. .id_table = cyapa_id_table,
  1255. };
  1256. module_i2c_driver(cyapa_driver);
  1257. MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
  1258. MODULE_AUTHOR("Dudley Du <[email protected]>");
  1259. MODULE_LICENSE("GPL");