adxl34x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ADXL345/346 Three-Axis Digital Accelerometers
  4. *
  5. * Enter bugs at http://blackfin.uclinux.org/
  6. *
  7. * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/delay.h>
  11. #include <linux/export.h>
  12. #include <linux/input.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/input/adxl34x.h>
  18. #include <linux/module.h>
  19. #include "adxl34x.h"
  20. /* ADXL345/6 Register Map */
  21. #define DEVID 0x00 /* R Device ID */
  22. #define THRESH_TAP 0x1D /* R/W Tap threshold */
  23. #define OFSX 0x1E /* R/W X-axis offset */
  24. #define OFSY 0x1F /* R/W Y-axis offset */
  25. #define OFSZ 0x20 /* R/W Z-axis offset */
  26. #define DUR 0x21 /* R/W Tap duration */
  27. #define LATENT 0x22 /* R/W Tap latency */
  28. #define WINDOW 0x23 /* R/W Tap window */
  29. #define THRESH_ACT 0x24 /* R/W Activity threshold */
  30. #define THRESH_INACT 0x25 /* R/W Inactivity threshold */
  31. #define TIME_INACT 0x26 /* R/W Inactivity time */
  32. #define ACT_INACT_CTL 0x27 /* R/W Axis enable control for activity and */
  33. /* inactivity detection */
  34. #define THRESH_FF 0x28 /* R/W Free-fall threshold */
  35. #define TIME_FF 0x29 /* R/W Free-fall time */
  36. #define TAP_AXES 0x2A /* R/W Axis control for tap/double tap */
  37. #define ACT_TAP_STATUS 0x2B /* R Source of tap/double tap */
  38. #define BW_RATE 0x2C /* R/W Data rate and power mode control */
  39. #define POWER_CTL 0x2D /* R/W Power saving features control */
  40. #define INT_ENABLE 0x2E /* R/W Interrupt enable control */
  41. #define INT_MAP 0x2F /* R/W Interrupt mapping control */
  42. #define INT_SOURCE 0x30 /* R Source of interrupts */
  43. #define DATA_FORMAT 0x31 /* R/W Data format control */
  44. #define DATAX0 0x32 /* R X-Axis Data 0 */
  45. #define DATAX1 0x33 /* R X-Axis Data 1 */
  46. #define DATAY0 0x34 /* R Y-Axis Data 0 */
  47. #define DATAY1 0x35 /* R Y-Axis Data 1 */
  48. #define DATAZ0 0x36 /* R Z-Axis Data 0 */
  49. #define DATAZ1 0x37 /* R Z-Axis Data 1 */
  50. #define FIFO_CTL 0x38 /* R/W FIFO control */
  51. #define FIFO_STATUS 0x39 /* R FIFO status */
  52. #define TAP_SIGN 0x3A /* R Sign and source for tap/double tap */
  53. /* Orientation ADXL346 only */
  54. #define ORIENT_CONF 0x3B /* R/W Orientation configuration */
  55. #define ORIENT 0x3C /* R Orientation status */
  56. /* DEVIDs */
  57. #define ID_ADXL345 0xE5
  58. #define ID_ADXL346 0xE6
  59. /* INT_ENABLE/INT_MAP/INT_SOURCE Bits */
  60. #define DATA_READY (1 << 7)
  61. #define SINGLE_TAP (1 << 6)
  62. #define DOUBLE_TAP (1 << 5)
  63. #define ACTIVITY (1 << 4)
  64. #define INACTIVITY (1 << 3)
  65. #define FREE_FALL (1 << 2)
  66. #define WATERMARK (1 << 1)
  67. #define OVERRUN (1 << 0)
  68. /* ACT_INACT_CONTROL Bits */
  69. #define ACT_ACDC (1 << 7)
  70. #define ACT_X_EN (1 << 6)
  71. #define ACT_Y_EN (1 << 5)
  72. #define ACT_Z_EN (1 << 4)
  73. #define INACT_ACDC (1 << 3)
  74. #define INACT_X_EN (1 << 2)
  75. #define INACT_Y_EN (1 << 1)
  76. #define INACT_Z_EN (1 << 0)
  77. /* TAP_AXES Bits */
  78. #define SUPPRESS (1 << 3)
  79. #define TAP_X_EN (1 << 2)
  80. #define TAP_Y_EN (1 << 1)
  81. #define TAP_Z_EN (1 << 0)
  82. /* ACT_TAP_STATUS Bits */
  83. #define ACT_X_SRC (1 << 6)
  84. #define ACT_Y_SRC (1 << 5)
  85. #define ACT_Z_SRC (1 << 4)
  86. #define ASLEEP (1 << 3)
  87. #define TAP_X_SRC (1 << 2)
  88. #define TAP_Y_SRC (1 << 1)
  89. #define TAP_Z_SRC (1 << 0)
  90. /* BW_RATE Bits */
  91. #define LOW_POWER (1 << 4)
  92. #define RATE(x) ((x) & 0xF)
  93. /* POWER_CTL Bits */
  94. #define PCTL_LINK (1 << 5)
  95. #define PCTL_AUTO_SLEEP (1 << 4)
  96. #define PCTL_MEASURE (1 << 3)
  97. #define PCTL_SLEEP (1 << 2)
  98. #define PCTL_WAKEUP(x) ((x) & 0x3)
  99. /* DATA_FORMAT Bits */
  100. #define SELF_TEST (1 << 7)
  101. #define SPI (1 << 6)
  102. #define INT_INVERT (1 << 5)
  103. #define FULL_RES (1 << 3)
  104. #define JUSTIFY (1 << 2)
  105. #define RANGE(x) ((x) & 0x3)
  106. #define RANGE_PM_2g 0
  107. #define RANGE_PM_4g 1
  108. #define RANGE_PM_8g 2
  109. #define RANGE_PM_16g 3
  110. /*
  111. * Maximum value our axis may get in full res mode for the input device
  112. * (signed 13 bits)
  113. */
  114. #define ADXL_FULLRES_MAX_VAL 4096
  115. /*
  116. * Maximum value our axis may get in fixed res mode for the input device
  117. * (signed 10 bits)
  118. */
  119. #define ADXL_FIXEDRES_MAX_VAL 512
  120. /* FIFO_CTL Bits */
  121. #define FIFO_MODE(x) (((x) & 0x3) << 6)
  122. #define FIFO_BYPASS 0
  123. #define FIFO_FIFO 1
  124. #define FIFO_STREAM 2
  125. #define FIFO_TRIGGER 3
  126. #define TRIGGER (1 << 5)
  127. #define SAMPLES(x) ((x) & 0x1F)
  128. /* FIFO_STATUS Bits */
  129. #define FIFO_TRIG (1 << 7)
  130. #define ENTRIES(x) ((x) & 0x3F)
  131. /* TAP_SIGN Bits ADXL346 only */
  132. #define XSIGN (1 << 6)
  133. #define YSIGN (1 << 5)
  134. #define ZSIGN (1 << 4)
  135. #define XTAP (1 << 3)
  136. #define YTAP (1 << 2)
  137. #define ZTAP (1 << 1)
  138. /* ORIENT_CONF ADXL346 only */
  139. #define ORIENT_DEADZONE(x) (((x) & 0x7) << 4)
  140. #define ORIENT_DIVISOR(x) ((x) & 0x7)
  141. /* ORIENT ADXL346 only */
  142. #define ADXL346_2D_VALID (1 << 6)
  143. #define ADXL346_2D_ORIENT(x) (((x) & 0x30) >> 4)
  144. #define ADXL346_3D_VALID (1 << 3)
  145. #define ADXL346_3D_ORIENT(x) ((x) & 0x7)
  146. #define ADXL346_2D_PORTRAIT_POS 0 /* +X */
  147. #define ADXL346_2D_PORTRAIT_NEG 1 /* -X */
  148. #define ADXL346_2D_LANDSCAPE_POS 2 /* +Y */
  149. #define ADXL346_2D_LANDSCAPE_NEG 3 /* -Y */
  150. #define ADXL346_3D_FRONT 3 /* +X */
  151. #define ADXL346_3D_BACK 4 /* -X */
  152. #define ADXL346_3D_RIGHT 2 /* +Y */
  153. #define ADXL346_3D_LEFT 5 /* -Y */
  154. #define ADXL346_3D_TOP 1 /* +Z */
  155. #define ADXL346_3D_BOTTOM 6 /* -Z */
  156. #undef ADXL_DEBUG
  157. #define ADXL_X_AXIS 0
  158. #define ADXL_Y_AXIS 1
  159. #define ADXL_Z_AXIS 2
  160. #define AC_READ(ac, reg) ((ac)->bops->read((ac)->dev, reg))
  161. #define AC_WRITE(ac, reg, val) ((ac)->bops->write((ac)->dev, reg, val))
  162. struct axis_triple {
  163. int x;
  164. int y;
  165. int z;
  166. };
  167. struct adxl34x {
  168. struct device *dev;
  169. struct input_dev *input;
  170. struct mutex mutex; /* reentrant protection for struct */
  171. struct adxl34x_platform_data pdata;
  172. struct axis_triple swcal;
  173. struct axis_triple hwcal;
  174. struct axis_triple saved;
  175. char phys[32];
  176. unsigned orient2d_saved;
  177. unsigned orient3d_saved;
  178. bool disabled; /* P: mutex */
  179. bool opened; /* P: mutex */
  180. bool suspended; /* P: mutex */
  181. bool fifo_delay;
  182. int irq;
  183. unsigned model;
  184. unsigned int_mask;
  185. const struct adxl34x_bus_ops *bops;
  186. };
  187. static const struct adxl34x_platform_data adxl34x_default_init = {
  188. .tap_threshold = 35,
  189. .tap_duration = 3,
  190. .tap_latency = 20,
  191. .tap_window = 20,
  192. .tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
  193. .act_axis_control = 0xFF,
  194. .activity_threshold = 6,
  195. .inactivity_threshold = 4,
  196. .inactivity_time = 3,
  197. .free_fall_threshold = 8,
  198. .free_fall_time = 0x20,
  199. .data_rate = 8,
  200. .data_range = ADXL_FULL_RES,
  201. .ev_type = EV_ABS,
  202. .ev_code_x = ABS_X, /* EV_REL */
  203. .ev_code_y = ABS_Y, /* EV_REL */
  204. .ev_code_z = ABS_Z, /* EV_REL */
  205. .ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
  206. .power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
  207. .fifo_mode = ADXL_FIFO_STREAM,
  208. .watermark = 0,
  209. };
  210. static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
  211. {
  212. __le16 buf[3];
  213. ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
  214. guard(mutex)(&ac->mutex);
  215. ac->saved.x = (s16) le16_to_cpu(buf[0]);
  216. axis->x = ac->saved.x;
  217. ac->saved.y = (s16) le16_to_cpu(buf[1]);
  218. axis->y = ac->saved.y;
  219. ac->saved.z = (s16) le16_to_cpu(buf[2]);
  220. axis->z = ac->saved.z;
  221. }
  222. static void adxl34x_service_ev_fifo(struct adxl34x *ac)
  223. {
  224. struct adxl34x_platform_data *pdata = &ac->pdata;
  225. struct axis_triple axis;
  226. adxl34x_get_triple(ac, &axis);
  227. input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
  228. axis.x - ac->swcal.x);
  229. input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
  230. axis.y - ac->swcal.y);
  231. input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
  232. axis.z - ac->swcal.z);
  233. }
  234. static void adxl34x_report_key_single(struct input_dev *input, int key)
  235. {
  236. input_report_key(input, key, true);
  237. input_sync(input);
  238. input_report_key(input, key, false);
  239. }
  240. static void adxl34x_send_key_events(struct adxl34x *ac,
  241. struct adxl34x_platform_data *pdata, int status, int press)
  242. {
  243. int i;
  244. for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
  245. if (status & (1 << (ADXL_Z_AXIS - i)))
  246. input_report_key(ac->input,
  247. pdata->ev_code_tap[i], press);
  248. }
  249. }
  250. static void adxl34x_do_tap(struct adxl34x *ac,
  251. struct adxl34x_platform_data *pdata, int status)
  252. {
  253. adxl34x_send_key_events(ac, pdata, status, true);
  254. input_sync(ac->input);
  255. adxl34x_send_key_events(ac, pdata, status, false);
  256. }
  257. static irqreturn_t adxl34x_irq(int irq, void *handle)
  258. {
  259. struct adxl34x *ac = handle;
  260. struct adxl34x_platform_data *pdata = &ac->pdata;
  261. int int_stat, tap_stat, samples, orient, orient_code;
  262. /*
  263. * ACT_TAP_STATUS should be read before clearing the interrupt
  264. * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
  265. */
  266. if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
  267. tap_stat = AC_READ(ac, ACT_TAP_STATUS);
  268. else
  269. tap_stat = 0;
  270. int_stat = AC_READ(ac, INT_SOURCE);
  271. if (int_stat & FREE_FALL)
  272. adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
  273. if (int_stat & OVERRUN)
  274. dev_dbg(ac->dev, "OVERRUN\n");
  275. if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
  276. adxl34x_do_tap(ac, pdata, tap_stat);
  277. if (int_stat & DOUBLE_TAP)
  278. adxl34x_do_tap(ac, pdata, tap_stat);
  279. }
  280. if (pdata->ev_code_act_inactivity) {
  281. if (int_stat & ACTIVITY)
  282. input_report_key(ac->input,
  283. pdata->ev_code_act_inactivity, 1);
  284. if (int_stat & INACTIVITY)
  285. input_report_key(ac->input,
  286. pdata->ev_code_act_inactivity, 0);
  287. }
  288. /*
  289. * ORIENTATION SENSING ADXL346 only
  290. */
  291. if (pdata->orientation_enable) {
  292. orient = AC_READ(ac, ORIENT);
  293. if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
  294. (orient & ADXL346_2D_VALID)) {
  295. orient_code = ADXL346_2D_ORIENT(orient);
  296. /* Report orientation only when it changes */
  297. if (ac->orient2d_saved != orient_code) {
  298. ac->orient2d_saved = orient_code;
  299. adxl34x_report_key_single(ac->input,
  300. pdata->ev_codes_orient_2d[orient_code]);
  301. }
  302. }
  303. if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
  304. (orient & ADXL346_3D_VALID)) {
  305. orient_code = ADXL346_3D_ORIENT(orient) - 1;
  306. /* Report orientation only when it changes */
  307. if (ac->orient3d_saved != orient_code) {
  308. ac->orient3d_saved = orient_code;
  309. adxl34x_report_key_single(ac->input,
  310. pdata->ev_codes_orient_3d[orient_code]);
  311. }
  312. }
  313. }
  314. if (int_stat & (DATA_READY | WATERMARK)) {
  315. if (pdata->fifo_mode)
  316. samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
  317. else
  318. samples = 1;
  319. for (; samples > 0; samples--) {
  320. adxl34x_service_ev_fifo(ac);
  321. /*
  322. * To ensure that the FIFO has
  323. * completely popped, there must be at least 5 us between
  324. * the end of reading the data registers, signified by the
  325. * transition to register 0x38 from 0x37 or the CS pin
  326. * going high, and the start of new reads of the FIFO or
  327. * reading the FIFO_STATUS register. For SPI operation at
  328. * 1.5 MHz or lower, the register addressing portion of the
  329. * transmission is sufficient delay to ensure the FIFO has
  330. * completely popped. It is necessary for SPI operation
  331. * greater than 1.5 MHz to de-assert the CS pin to ensure a
  332. * total of 5 us, which is at most 3.4 us at 5 MHz
  333. * operation.
  334. */
  335. if (ac->fifo_delay && (samples > 1))
  336. udelay(3);
  337. }
  338. }
  339. input_sync(ac->input);
  340. return IRQ_HANDLED;
  341. }
  342. static void __adxl34x_disable(struct adxl34x *ac)
  343. {
  344. /*
  345. * A '0' places the ADXL34x into standby mode
  346. * with minimum power consumption.
  347. */
  348. AC_WRITE(ac, POWER_CTL, 0);
  349. }
  350. static void __adxl34x_enable(struct adxl34x *ac)
  351. {
  352. AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
  353. }
  354. static int adxl34x_suspend(struct device *dev)
  355. {
  356. struct adxl34x *ac = dev_get_drvdata(dev);
  357. guard(mutex)(&ac->mutex);
  358. if (!ac->suspended && !ac->disabled && ac->opened)
  359. __adxl34x_disable(ac);
  360. ac->suspended = true;
  361. return 0;
  362. }
  363. static int adxl34x_resume(struct device *dev)
  364. {
  365. struct adxl34x *ac = dev_get_drvdata(dev);
  366. guard(mutex)(&ac->mutex);
  367. if (ac->suspended && !ac->disabled && ac->opened)
  368. __adxl34x_enable(ac);
  369. ac->suspended = false;
  370. return 0;
  371. }
  372. static ssize_t adxl34x_disable_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct adxl34x *ac = dev_get_drvdata(dev);
  376. return sprintf(buf, "%u\n", ac->disabled);
  377. }
  378. static ssize_t adxl34x_disable_store(struct device *dev,
  379. struct device_attribute *attr,
  380. const char *buf, size_t count)
  381. {
  382. struct adxl34x *ac = dev_get_drvdata(dev);
  383. unsigned int val;
  384. int error;
  385. error = kstrtouint(buf, 10, &val);
  386. if (error)
  387. return error;
  388. guard(mutex)(&ac->mutex);
  389. if (!ac->suspended && ac->opened) {
  390. if (val) {
  391. if (!ac->disabled)
  392. __adxl34x_disable(ac);
  393. } else {
  394. if (ac->disabled)
  395. __adxl34x_enable(ac);
  396. }
  397. }
  398. ac->disabled = !!val;
  399. return count;
  400. }
  401. static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
  402. static ssize_t adxl34x_calibrate_show(struct device *dev,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. struct adxl34x *ac = dev_get_drvdata(dev);
  406. guard(mutex)(&ac->mutex);
  407. return sprintf(buf, "%d,%d,%d\n",
  408. ac->hwcal.x * 4 + ac->swcal.x,
  409. ac->hwcal.y * 4 + ac->swcal.y,
  410. ac->hwcal.z * 4 + ac->swcal.z);
  411. }
  412. static ssize_t adxl34x_calibrate_store(struct device *dev,
  413. struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct adxl34x *ac = dev_get_drvdata(dev);
  417. /*
  418. * Hardware offset calibration has a resolution of 15.6 mg/LSB.
  419. * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
  420. */
  421. guard(mutex)(&ac->mutex);
  422. ac->hwcal.x -= (ac->saved.x / 4);
  423. ac->swcal.x = ac->saved.x % 4;
  424. ac->hwcal.y -= (ac->saved.y / 4);
  425. ac->swcal.y = ac->saved.y % 4;
  426. ac->hwcal.z -= (ac->saved.z / 4);
  427. ac->swcal.z = ac->saved.z % 4;
  428. AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
  429. AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
  430. AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
  431. return count;
  432. }
  433. static DEVICE_ATTR(calibrate, 0664,
  434. adxl34x_calibrate_show, adxl34x_calibrate_store);
  435. static ssize_t adxl34x_rate_show(struct device *dev,
  436. struct device_attribute *attr, char *buf)
  437. {
  438. struct adxl34x *ac = dev_get_drvdata(dev);
  439. return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
  440. }
  441. static ssize_t adxl34x_rate_store(struct device *dev,
  442. struct device_attribute *attr,
  443. const char *buf, size_t count)
  444. {
  445. struct adxl34x *ac = dev_get_drvdata(dev);
  446. unsigned char val;
  447. int error;
  448. error = kstrtou8(buf, 10, &val);
  449. if (error)
  450. return error;
  451. guard(mutex)(&ac->mutex);
  452. ac->pdata.data_rate = RATE(val);
  453. AC_WRITE(ac, BW_RATE,
  454. ac->pdata.data_rate |
  455. (ac->pdata.low_power_mode ? LOW_POWER : 0));
  456. return count;
  457. }
  458. static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
  459. static ssize_t adxl34x_autosleep_show(struct device *dev,
  460. struct device_attribute *attr, char *buf)
  461. {
  462. struct adxl34x *ac = dev_get_drvdata(dev);
  463. return sprintf(buf, "%u\n",
  464. ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
  465. }
  466. static ssize_t adxl34x_autosleep_store(struct device *dev,
  467. struct device_attribute *attr,
  468. const char *buf, size_t count)
  469. {
  470. struct adxl34x *ac = dev_get_drvdata(dev);
  471. unsigned int val;
  472. int error;
  473. error = kstrtouint(buf, 10, &val);
  474. if (error)
  475. return error;
  476. guard(mutex)(&ac->mutex);
  477. if (val)
  478. ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
  479. else
  480. ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
  481. if (!ac->disabled && !ac->suspended && ac->opened)
  482. AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
  483. return count;
  484. }
  485. static DEVICE_ATTR(autosleep, 0664,
  486. adxl34x_autosleep_show, adxl34x_autosleep_store);
  487. static ssize_t adxl34x_position_show(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. struct adxl34x *ac = dev_get_drvdata(dev);
  491. guard(mutex)(&ac->mutex);
  492. return sprintf(buf, "(%d, %d, %d)\n",
  493. ac->saved.x, ac->saved.y, ac->saved.z);
  494. }
  495. static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
  496. #ifdef ADXL_DEBUG
  497. static ssize_t adxl34x_write_store(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct adxl34x *ac = dev_get_drvdata(dev);
  502. unsigned int val;
  503. int error;
  504. /*
  505. * This allows basic ADXL register write access for debug purposes.
  506. */
  507. error = kstrtouint(buf, 16, &val);
  508. if (error)
  509. return error;
  510. guard(mutex)(&ac->mutex);
  511. AC_WRITE(ac, val >> 8, val & 0xFF);
  512. return count;
  513. }
  514. static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
  515. #endif
  516. static struct attribute *adxl34x_attributes[] = {
  517. &dev_attr_disable.attr,
  518. &dev_attr_calibrate.attr,
  519. &dev_attr_rate.attr,
  520. &dev_attr_autosleep.attr,
  521. &dev_attr_position.attr,
  522. #ifdef ADXL_DEBUG
  523. &dev_attr_write.attr,
  524. #endif
  525. NULL
  526. };
  527. static const struct attribute_group adxl34x_attr_group = {
  528. .attrs = adxl34x_attributes,
  529. };
  530. const struct attribute_group *adxl34x_groups[] = {
  531. &adxl34x_attr_group,
  532. NULL
  533. };
  534. EXPORT_SYMBOL_GPL(adxl34x_groups);
  535. static int adxl34x_input_open(struct input_dev *input)
  536. {
  537. struct adxl34x *ac = input_get_drvdata(input);
  538. guard(mutex)(&ac->mutex);
  539. if (!ac->suspended && !ac->disabled)
  540. __adxl34x_enable(ac);
  541. ac->opened = true;
  542. return 0;
  543. }
  544. static void adxl34x_input_close(struct input_dev *input)
  545. {
  546. struct adxl34x *ac = input_get_drvdata(input);
  547. guard(mutex)(&ac->mutex);
  548. if (!ac->suspended && !ac->disabled)
  549. __adxl34x_disable(ac);
  550. ac->opened = false;
  551. }
  552. struct adxl34x *adxl34x_probe(struct device *dev, int irq,
  553. bool fifo_delay_default,
  554. const struct adxl34x_bus_ops *bops)
  555. {
  556. struct adxl34x *ac;
  557. struct input_dev *input_dev;
  558. const struct adxl34x_platform_data *pdata;
  559. int error, range, i;
  560. int revid;
  561. if (!irq) {
  562. dev_err(dev, "no IRQ?\n");
  563. return ERR_PTR(-ENODEV);
  564. }
  565. ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
  566. if (!ac)
  567. return ERR_PTR(-ENOMEM);
  568. input_dev = devm_input_allocate_device(dev);
  569. if (!input_dev)
  570. return ERR_PTR(-ENOMEM);
  571. ac->fifo_delay = fifo_delay_default;
  572. pdata = dev_get_platdata(dev);
  573. if (!pdata) {
  574. dev_dbg(dev,
  575. "No platform data: Using default initialization\n");
  576. pdata = &adxl34x_default_init;
  577. }
  578. ac->pdata = *pdata;
  579. pdata = &ac->pdata;
  580. ac->input = input_dev;
  581. ac->dev = dev;
  582. ac->irq = irq;
  583. ac->bops = bops;
  584. mutex_init(&ac->mutex);
  585. input_dev->name = "ADXL34x accelerometer";
  586. revid = AC_READ(ac, DEVID);
  587. switch (revid) {
  588. case ID_ADXL345:
  589. ac->model = 345;
  590. break;
  591. case ID_ADXL346:
  592. ac->model = 346;
  593. break;
  594. default:
  595. dev_err(dev, "Failed to probe %s\n", input_dev->name);
  596. return ERR_PTR(-ENODEV);
  597. }
  598. snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
  599. input_dev->phys = ac->phys;
  600. input_dev->id.product = ac->model;
  601. input_dev->id.bustype = bops->bustype;
  602. input_dev->open = adxl34x_input_open;
  603. input_dev->close = adxl34x_input_close;
  604. input_set_drvdata(input_dev, ac);
  605. if (ac->pdata.ev_type == EV_REL) {
  606. input_set_capability(input_dev, EV_REL, REL_X);
  607. input_set_capability(input_dev, EV_REL, REL_Y);
  608. input_set_capability(input_dev, EV_REL, REL_Z);
  609. } else {
  610. /* EV_ABS */
  611. if (pdata->data_range & FULL_RES)
  612. range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
  613. else
  614. range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
  615. input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
  616. input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
  617. input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
  618. }
  619. input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_X_AXIS]);
  620. input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Y_AXIS]);
  621. input_set_capability(input_dev, EV_KEY, pdata->ev_code_tap[ADXL_Z_AXIS]);
  622. if (pdata->ev_code_ff) {
  623. ac->int_mask = FREE_FALL;
  624. input_set_capability(input_dev, EV_KEY, pdata->ev_code_ff);
  625. }
  626. if (pdata->ev_code_act_inactivity)
  627. input_set_capability(input_dev, EV_KEY,
  628. pdata->ev_code_act_inactivity);
  629. ac->int_mask |= ACTIVITY | INACTIVITY;
  630. if (pdata->watermark) {
  631. ac->int_mask |= WATERMARK;
  632. if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
  633. ac->pdata.fifo_mode |= FIFO_STREAM;
  634. } else {
  635. ac->int_mask |= DATA_READY;
  636. }
  637. if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
  638. ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
  639. if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
  640. ac->fifo_delay = false;
  641. AC_WRITE(ac, POWER_CTL, 0);
  642. error = devm_request_threaded_irq(dev, ac->irq, NULL, adxl34x_irq,
  643. IRQF_ONESHOT, dev_name(dev), ac);
  644. if (error) {
  645. dev_err(dev, "irq %d busy?\n", ac->irq);
  646. return ERR_PTR(error);
  647. }
  648. error = input_register_device(input_dev);
  649. if (error)
  650. return ERR_PTR(error);
  651. AC_WRITE(ac, OFSX, pdata->x_axis_offset);
  652. ac->hwcal.x = pdata->x_axis_offset;
  653. AC_WRITE(ac, OFSY, pdata->y_axis_offset);
  654. ac->hwcal.y = pdata->y_axis_offset;
  655. AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
  656. ac->hwcal.z = pdata->z_axis_offset;
  657. AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
  658. AC_WRITE(ac, DUR, pdata->tap_duration);
  659. AC_WRITE(ac, LATENT, pdata->tap_latency);
  660. AC_WRITE(ac, WINDOW, pdata->tap_window);
  661. AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
  662. AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
  663. AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
  664. AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
  665. AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
  666. AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
  667. AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
  668. AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
  669. (pdata->low_power_mode ? LOW_POWER : 0));
  670. AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
  671. AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
  672. SAMPLES(pdata->watermark));
  673. if (pdata->use_int2) {
  674. /* Map all INTs to INT2 */
  675. AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
  676. } else {
  677. /* Map all INTs to INT1 */
  678. AC_WRITE(ac, INT_MAP, 0);
  679. }
  680. if (ac->model == 346 && ac->pdata.orientation_enable) {
  681. AC_WRITE(ac, ORIENT_CONF,
  682. ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
  683. ORIENT_DIVISOR(ac->pdata.divisor_length));
  684. ac->orient2d_saved = 1234;
  685. ac->orient3d_saved = 1234;
  686. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
  687. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
  688. input_set_capability(input_dev, EV_KEY,
  689. pdata->ev_codes_orient_3d[i]);
  690. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
  691. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
  692. input_set_capability(input_dev, EV_KEY,
  693. pdata->ev_codes_orient_2d[i]);
  694. } else {
  695. ac->pdata.orientation_enable = 0;
  696. }
  697. AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
  698. ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
  699. return ac;
  700. }
  701. EXPORT_SYMBOL_GPL(adxl34x_probe);
  702. EXPORT_GPL_SIMPLE_DEV_PM_OPS(adxl34x_pm, adxl34x_suspend, adxl34x_resume);
  703. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  704. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
  705. MODULE_LICENSE("GPL");