adxl34x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * ADXL345/346 Three-Axis Digital Accelerometers
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  7. * Licensed under the GPL-2 or later.
  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. short buf[3];
  212. ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
  213. mutex_lock(&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. mutex_unlock(&ac->mutex);
  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. void adxl34x_suspend(struct adxl34x *ac)
  355. {
  356. mutex_lock(&ac->mutex);
  357. if (!ac->suspended && !ac->disabled && ac->opened)
  358. __adxl34x_disable(ac);
  359. ac->suspended = true;
  360. mutex_unlock(&ac->mutex);
  361. }
  362. EXPORT_SYMBOL_GPL(adxl34x_suspend);
  363. void adxl34x_resume(struct adxl34x *ac)
  364. {
  365. mutex_lock(&ac->mutex);
  366. if (ac->suspended && !ac->disabled && ac->opened)
  367. __adxl34x_enable(ac);
  368. ac->suspended = false;
  369. mutex_unlock(&ac->mutex);
  370. }
  371. EXPORT_SYMBOL_GPL(adxl34x_resume);
  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. mutex_lock(&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. mutex_unlock(&ac->mutex);
  400. return count;
  401. }
  402. static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
  403. static ssize_t adxl34x_calibrate_show(struct device *dev,
  404. struct device_attribute *attr, char *buf)
  405. {
  406. struct adxl34x *ac = dev_get_drvdata(dev);
  407. ssize_t count;
  408. mutex_lock(&ac->mutex);
  409. count = sprintf(buf, "%d,%d,%d\n",
  410. ac->hwcal.x * 4 + ac->swcal.x,
  411. ac->hwcal.y * 4 + ac->swcal.y,
  412. ac->hwcal.z * 4 + ac->swcal.z);
  413. mutex_unlock(&ac->mutex);
  414. return count;
  415. }
  416. static ssize_t adxl34x_calibrate_store(struct device *dev,
  417. struct device_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct adxl34x *ac = dev_get_drvdata(dev);
  421. /*
  422. * Hardware offset calibration has a resolution of 15.6 mg/LSB.
  423. * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
  424. */
  425. mutex_lock(&ac->mutex);
  426. ac->hwcal.x -= (ac->saved.x / 4);
  427. ac->swcal.x = ac->saved.x % 4;
  428. ac->hwcal.y -= (ac->saved.y / 4);
  429. ac->swcal.y = ac->saved.y % 4;
  430. ac->hwcal.z -= (ac->saved.z / 4);
  431. ac->swcal.z = ac->saved.z % 4;
  432. AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
  433. AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
  434. AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
  435. mutex_unlock(&ac->mutex);
  436. return count;
  437. }
  438. static DEVICE_ATTR(calibrate, 0664,
  439. adxl34x_calibrate_show, adxl34x_calibrate_store);
  440. static ssize_t adxl34x_rate_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct adxl34x *ac = dev_get_drvdata(dev);
  444. return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
  445. }
  446. static ssize_t adxl34x_rate_store(struct device *dev,
  447. struct device_attribute *attr,
  448. const char *buf, size_t count)
  449. {
  450. struct adxl34x *ac = dev_get_drvdata(dev);
  451. unsigned char val;
  452. int error;
  453. error = kstrtou8(buf, 10, &val);
  454. if (error)
  455. return error;
  456. mutex_lock(&ac->mutex);
  457. ac->pdata.data_rate = RATE(val);
  458. AC_WRITE(ac, BW_RATE,
  459. ac->pdata.data_rate |
  460. (ac->pdata.low_power_mode ? LOW_POWER : 0));
  461. mutex_unlock(&ac->mutex);
  462. return count;
  463. }
  464. static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
  465. static ssize_t adxl34x_autosleep_show(struct device *dev,
  466. struct device_attribute *attr, char *buf)
  467. {
  468. struct adxl34x *ac = dev_get_drvdata(dev);
  469. return sprintf(buf, "%u\n",
  470. ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
  471. }
  472. static ssize_t adxl34x_autosleep_store(struct device *dev,
  473. struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. struct adxl34x *ac = dev_get_drvdata(dev);
  477. unsigned int val;
  478. int error;
  479. error = kstrtouint(buf, 10, &val);
  480. if (error)
  481. return error;
  482. mutex_lock(&ac->mutex);
  483. if (val)
  484. ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
  485. else
  486. ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
  487. if (!ac->disabled && !ac->suspended && ac->opened)
  488. AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
  489. mutex_unlock(&ac->mutex);
  490. return count;
  491. }
  492. static DEVICE_ATTR(autosleep, 0664,
  493. adxl34x_autosleep_show, adxl34x_autosleep_store);
  494. static ssize_t adxl34x_position_show(struct device *dev,
  495. struct device_attribute *attr, char *buf)
  496. {
  497. struct adxl34x *ac = dev_get_drvdata(dev);
  498. ssize_t count;
  499. mutex_lock(&ac->mutex);
  500. count = sprintf(buf, "(%d, %d, %d)\n",
  501. ac->saved.x, ac->saved.y, ac->saved.z);
  502. mutex_unlock(&ac->mutex);
  503. return count;
  504. }
  505. static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
  506. #ifdef ADXL_DEBUG
  507. static ssize_t adxl34x_write_store(struct device *dev,
  508. struct device_attribute *attr,
  509. const char *buf, size_t count)
  510. {
  511. struct adxl34x *ac = dev_get_drvdata(dev);
  512. unsigned int val;
  513. int error;
  514. /*
  515. * This allows basic ADXL register write access for debug purposes.
  516. */
  517. error = kstrtouint(buf, 16, &val);
  518. if (error)
  519. return error;
  520. mutex_lock(&ac->mutex);
  521. AC_WRITE(ac, val >> 8, val & 0xFF);
  522. mutex_unlock(&ac->mutex);
  523. return count;
  524. }
  525. static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
  526. #endif
  527. static struct attribute *adxl34x_attributes[] = {
  528. &dev_attr_disable.attr,
  529. &dev_attr_calibrate.attr,
  530. &dev_attr_rate.attr,
  531. &dev_attr_autosleep.attr,
  532. &dev_attr_position.attr,
  533. #ifdef ADXL_DEBUG
  534. &dev_attr_write.attr,
  535. #endif
  536. NULL
  537. };
  538. static const struct attribute_group adxl34x_attr_group = {
  539. .attrs = adxl34x_attributes,
  540. };
  541. static int adxl34x_input_open(struct input_dev *input)
  542. {
  543. struct adxl34x *ac = input_get_drvdata(input);
  544. mutex_lock(&ac->mutex);
  545. if (!ac->suspended && !ac->disabled)
  546. __adxl34x_enable(ac);
  547. ac->opened = true;
  548. mutex_unlock(&ac->mutex);
  549. return 0;
  550. }
  551. static void adxl34x_input_close(struct input_dev *input)
  552. {
  553. struct adxl34x *ac = input_get_drvdata(input);
  554. mutex_lock(&ac->mutex);
  555. if (!ac->suspended && !ac->disabled)
  556. __adxl34x_disable(ac);
  557. ac->opened = false;
  558. mutex_unlock(&ac->mutex);
  559. }
  560. struct adxl34x *adxl34x_probe(struct device *dev, int irq,
  561. bool fifo_delay_default,
  562. const struct adxl34x_bus_ops *bops)
  563. {
  564. struct adxl34x *ac;
  565. struct input_dev *input_dev;
  566. const struct adxl34x_platform_data *pdata;
  567. int err, range, i;
  568. unsigned char revid;
  569. if (!irq) {
  570. dev_err(dev, "no IRQ?\n");
  571. err = -ENODEV;
  572. goto err_out;
  573. }
  574. ac = kzalloc(sizeof(*ac), GFP_KERNEL);
  575. input_dev = input_allocate_device();
  576. if (!ac || !input_dev) {
  577. err = -ENOMEM;
  578. goto err_free_mem;
  579. }
  580. ac->fifo_delay = fifo_delay_default;
  581. pdata = dev_get_platdata(dev);
  582. if (!pdata) {
  583. dev_dbg(dev,
  584. "No platform data: Using default initialization\n");
  585. pdata = &adxl34x_default_init;
  586. }
  587. ac->pdata = *pdata;
  588. pdata = &ac->pdata;
  589. ac->input = input_dev;
  590. ac->dev = dev;
  591. ac->irq = irq;
  592. ac->bops = bops;
  593. mutex_init(&ac->mutex);
  594. input_dev->name = "ADXL34x accelerometer";
  595. revid = AC_READ(ac, DEVID);
  596. switch (revid) {
  597. case ID_ADXL345:
  598. ac->model = 345;
  599. break;
  600. case ID_ADXL346:
  601. ac->model = 346;
  602. break;
  603. default:
  604. dev_err(dev, "Failed to probe %s\n", input_dev->name);
  605. err = -ENODEV;
  606. goto err_free_mem;
  607. }
  608. snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
  609. input_dev->phys = ac->phys;
  610. input_dev->dev.parent = dev;
  611. input_dev->id.product = ac->model;
  612. input_dev->id.bustype = bops->bustype;
  613. input_dev->open = adxl34x_input_open;
  614. input_dev->close = adxl34x_input_close;
  615. input_set_drvdata(input_dev, ac);
  616. __set_bit(ac->pdata.ev_type, input_dev->evbit);
  617. if (ac->pdata.ev_type == EV_REL) {
  618. __set_bit(REL_X, input_dev->relbit);
  619. __set_bit(REL_Y, input_dev->relbit);
  620. __set_bit(REL_Z, input_dev->relbit);
  621. } else {
  622. /* EV_ABS */
  623. __set_bit(ABS_X, input_dev->absbit);
  624. __set_bit(ABS_Y, input_dev->absbit);
  625. __set_bit(ABS_Z, input_dev->absbit);
  626. if (pdata->data_range & FULL_RES)
  627. range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
  628. else
  629. range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
  630. input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
  631. input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
  632. input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
  633. }
  634. __set_bit(EV_KEY, input_dev->evbit);
  635. __set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
  636. __set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
  637. __set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
  638. if (pdata->ev_code_ff) {
  639. ac->int_mask = FREE_FALL;
  640. __set_bit(pdata->ev_code_ff, input_dev->keybit);
  641. }
  642. if (pdata->ev_code_act_inactivity)
  643. __set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
  644. ac->int_mask |= ACTIVITY | INACTIVITY;
  645. if (pdata->watermark) {
  646. ac->int_mask |= WATERMARK;
  647. if (!FIFO_MODE(pdata->fifo_mode))
  648. ac->pdata.fifo_mode |= FIFO_STREAM;
  649. } else {
  650. ac->int_mask |= DATA_READY;
  651. }
  652. if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
  653. ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
  654. if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
  655. ac->fifo_delay = false;
  656. AC_WRITE(ac, POWER_CTL, 0);
  657. err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
  658. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  659. dev_name(dev), ac);
  660. if (err) {
  661. dev_err(dev, "irq %d busy?\n", ac->irq);
  662. goto err_free_mem;
  663. }
  664. err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
  665. if (err)
  666. goto err_free_irq;
  667. err = input_register_device(input_dev);
  668. if (err)
  669. goto err_remove_attr;
  670. AC_WRITE(ac, OFSX, pdata->x_axis_offset);
  671. ac->hwcal.x = pdata->x_axis_offset;
  672. AC_WRITE(ac, OFSY, pdata->y_axis_offset);
  673. ac->hwcal.y = pdata->y_axis_offset;
  674. AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
  675. ac->hwcal.z = pdata->z_axis_offset;
  676. AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
  677. AC_WRITE(ac, DUR, pdata->tap_duration);
  678. AC_WRITE(ac, LATENT, pdata->tap_latency);
  679. AC_WRITE(ac, WINDOW, pdata->tap_window);
  680. AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
  681. AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
  682. AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
  683. AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
  684. AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
  685. AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
  686. AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
  687. AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
  688. (pdata->low_power_mode ? LOW_POWER : 0));
  689. AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
  690. AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
  691. SAMPLES(pdata->watermark));
  692. if (pdata->use_int2) {
  693. /* Map all INTs to INT2 */
  694. AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
  695. } else {
  696. /* Map all INTs to INT1 */
  697. AC_WRITE(ac, INT_MAP, 0);
  698. }
  699. if (ac->model == 346 && ac->pdata.orientation_enable) {
  700. AC_WRITE(ac, ORIENT_CONF,
  701. ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
  702. ORIENT_DIVISOR(ac->pdata.divisor_length));
  703. ac->orient2d_saved = 1234;
  704. ac->orient3d_saved = 1234;
  705. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
  706. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
  707. __set_bit(pdata->ev_codes_orient_3d[i],
  708. input_dev->keybit);
  709. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
  710. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
  711. __set_bit(pdata->ev_codes_orient_2d[i],
  712. input_dev->keybit);
  713. } else {
  714. ac->pdata.orientation_enable = 0;
  715. }
  716. AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
  717. ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
  718. return ac;
  719. err_remove_attr:
  720. sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
  721. err_free_irq:
  722. free_irq(ac->irq, ac);
  723. err_free_mem:
  724. input_free_device(input_dev);
  725. kfree(ac);
  726. err_out:
  727. return ERR_PTR(err);
  728. }
  729. EXPORT_SYMBOL_GPL(adxl34x_probe);
  730. int adxl34x_remove(struct adxl34x *ac)
  731. {
  732. sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
  733. free_irq(ac->irq, ac);
  734. input_unregister_device(ac->input);
  735. dev_dbg(ac->dev, "unregistered accelerometer\n");
  736. kfree(ac);
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(adxl34x_remove);
  740. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  741. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
  742. MODULE_LICENSE("GPL");