adxl34x.c 22 KB

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