adxl34x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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/init.h>
  11. #include <linux/delay.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) & 0x3) >> 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 = FIFO_STREAM,
  208. .watermark = 0,
  209. };
  210. static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
  211. {
  212. short buf[3];
  213. ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
  214. mutex_lock(&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. mutex_unlock(&ac->mutex);
  222. }
  223. static void adxl34x_service_ev_fifo(struct adxl34x *ac)
  224. {
  225. struct adxl34x_platform_data *pdata = &ac->pdata;
  226. struct axis_triple axis;
  227. adxl34x_get_triple(ac, &axis);
  228. input_event(ac->input, pdata->ev_type, pdata->ev_code_x,
  229. axis.x - ac->swcal.x);
  230. input_event(ac->input, pdata->ev_type, pdata->ev_code_y,
  231. axis.y - ac->swcal.y);
  232. input_event(ac->input, pdata->ev_type, pdata->ev_code_z,
  233. axis.z - ac->swcal.z);
  234. }
  235. static void adxl34x_report_key_single(struct input_dev *input, int key)
  236. {
  237. input_report_key(input, key, true);
  238. input_sync(input);
  239. input_report_key(input, key, false);
  240. }
  241. static void adxl34x_send_key_events(struct adxl34x *ac,
  242. struct adxl34x_platform_data *pdata, int status, int press)
  243. {
  244. int i;
  245. for (i = ADXL_X_AXIS; i <= ADXL_Z_AXIS; i++) {
  246. if (status & (1 << (ADXL_Z_AXIS - i)))
  247. input_report_key(ac->input,
  248. pdata->ev_code_tap[i], press);
  249. }
  250. }
  251. static void adxl34x_do_tap(struct adxl34x *ac,
  252. struct adxl34x_platform_data *pdata, int status)
  253. {
  254. adxl34x_send_key_events(ac, pdata, status, true);
  255. input_sync(ac->input);
  256. adxl34x_send_key_events(ac, pdata, status, false);
  257. }
  258. static irqreturn_t adxl34x_irq(int irq, void *handle)
  259. {
  260. struct adxl34x *ac = handle;
  261. struct adxl34x_platform_data *pdata = &ac->pdata;
  262. int int_stat, tap_stat, samples, orient, orient_code;
  263. /*
  264. * ACT_TAP_STATUS should be read before clearing the interrupt
  265. * Avoid reading ACT_TAP_STATUS in case TAP detection is disabled
  266. */
  267. if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
  268. tap_stat = AC_READ(ac, ACT_TAP_STATUS);
  269. else
  270. tap_stat = 0;
  271. int_stat = AC_READ(ac, INT_SOURCE);
  272. if (int_stat & FREE_FALL)
  273. adxl34x_report_key_single(ac->input, pdata->ev_code_ff);
  274. if (int_stat & OVERRUN)
  275. dev_dbg(ac->dev, "OVERRUN\n");
  276. if (int_stat & (SINGLE_TAP | DOUBLE_TAP)) {
  277. adxl34x_do_tap(ac, pdata, tap_stat);
  278. if (int_stat & DOUBLE_TAP)
  279. adxl34x_do_tap(ac, pdata, tap_stat);
  280. }
  281. if (pdata->ev_code_act_inactivity) {
  282. if (int_stat & ACTIVITY)
  283. input_report_key(ac->input,
  284. pdata->ev_code_act_inactivity, 1);
  285. if (int_stat & INACTIVITY)
  286. input_report_key(ac->input,
  287. pdata->ev_code_act_inactivity, 0);
  288. }
  289. /*
  290. * ORIENTATION SENSING ADXL346 only
  291. */
  292. if (pdata->orientation_enable) {
  293. orient = AC_READ(ac, ORIENT);
  294. if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_2D) &&
  295. (orient & ADXL346_2D_VALID)) {
  296. orient_code = ADXL346_2D_ORIENT(orient);
  297. /* Report orientation only when it changes */
  298. if (ac->orient2d_saved != orient_code) {
  299. ac->orient2d_saved = orient_code;
  300. adxl34x_report_key_single(ac->input,
  301. pdata->ev_codes_orient_2d[orient_code]);
  302. }
  303. }
  304. if ((pdata->orientation_enable & ADXL_EN_ORIENTATION_3D) &&
  305. (orient & ADXL346_3D_VALID)) {
  306. orient_code = ADXL346_3D_ORIENT(orient) - 1;
  307. /* Report orientation only when it changes */
  308. if (ac->orient3d_saved != orient_code) {
  309. ac->orient3d_saved = orient_code;
  310. adxl34x_report_key_single(ac->input,
  311. pdata->ev_codes_orient_3d[orient_code]);
  312. }
  313. }
  314. }
  315. if (int_stat & (DATA_READY | WATERMARK)) {
  316. if (pdata->fifo_mode)
  317. samples = ENTRIES(AC_READ(ac, FIFO_STATUS)) + 1;
  318. else
  319. samples = 1;
  320. for (; samples > 0; samples--) {
  321. adxl34x_service_ev_fifo(ac);
  322. /*
  323. * To ensure that the FIFO has
  324. * completely popped, there must be at least 5 us between
  325. * the end of reading the data registers, signified by the
  326. * transition to register 0x38 from 0x37 or the CS pin
  327. * going high, and the start of new reads of the FIFO or
  328. * reading the FIFO_STATUS register. For SPI operation at
  329. * 1.5 MHz or lower, the register addressing portion of the
  330. * transmission is sufficient delay to ensure the FIFO has
  331. * completely popped. It is necessary for SPI operation
  332. * greater than 1.5 MHz to de-assert the CS pin to ensure a
  333. * total of 5 us, which is at most 3.4 us at 5 MHz
  334. * operation.
  335. */
  336. if (ac->fifo_delay && (samples > 1))
  337. udelay(3);
  338. }
  339. }
  340. input_sync(ac->input);
  341. return IRQ_HANDLED;
  342. }
  343. static void __adxl34x_disable(struct adxl34x *ac)
  344. {
  345. /*
  346. * A '0' places the ADXL34x into standby mode
  347. * with minimum power consumption.
  348. */
  349. AC_WRITE(ac, POWER_CTL, 0);
  350. }
  351. static void __adxl34x_enable(struct adxl34x *ac)
  352. {
  353. AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
  354. }
  355. void adxl34x_suspend(struct adxl34x *ac)
  356. {
  357. mutex_lock(&ac->mutex);
  358. if (!ac->suspended && !ac->disabled && ac->opened)
  359. __adxl34x_disable(ac);
  360. ac->suspended = true;
  361. mutex_unlock(&ac->mutex);
  362. }
  363. EXPORT_SYMBOL_GPL(adxl34x_suspend);
  364. void adxl34x_resume(struct adxl34x *ac)
  365. {
  366. mutex_lock(&ac->mutex);
  367. if (ac->suspended && !ac->disabled && ac->opened)
  368. __adxl34x_enable(ac);
  369. ac->suspended = false;
  370. mutex_unlock(&ac->mutex);
  371. }
  372. EXPORT_SYMBOL_GPL(adxl34x_resume);
  373. static ssize_t adxl34x_disable_show(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. struct adxl34x *ac = dev_get_drvdata(dev);
  377. return sprintf(buf, "%u\n", ac->disabled);
  378. }
  379. static ssize_t adxl34x_disable_store(struct device *dev,
  380. struct device_attribute *attr,
  381. const char *buf, size_t count)
  382. {
  383. struct adxl34x *ac = dev_get_drvdata(dev);
  384. unsigned int val;
  385. int error;
  386. error = kstrtouint(buf, 10, &val);
  387. if (error)
  388. return error;
  389. mutex_lock(&ac->mutex);
  390. if (!ac->suspended && ac->opened) {
  391. if (val) {
  392. if (!ac->disabled)
  393. __adxl34x_disable(ac);
  394. } else {
  395. if (ac->disabled)
  396. __adxl34x_enable(ac);
  397. }
  398. }
  399. ac->disabled = !!val;
  400. mutex_unlock(&ac->mutex);
  401. return count;
  402. }
  403. static DEVICE_ATTR(disable, 0664, adxl34x_disable_show, adxl34x_disable_store);
  404. static ssize_t adxl34x_calibrate_show(struct device *dev,
  405. struct device_attribute *attr, char *buf)
  406. {
  407. struct adxl34x *ac = dev_get_drvdata(dev);
  408. ssize_t count;
  409. mutex_lock(&ac->mutex);
  410. count = sprintf(buf, "%d,%d,%d\n",
  411. ac->hwcal.x * 4 + ac->swcal.x,
  412. ac->hwcal.y * 4 + ac->swcal.y,
  413. ac->hwcal.z * 4 + ac->swcal.z);
  414. mutex_unlock(&ac->mutex);
  415. return count;
  416. }
  417. static ssize_t adxl34x_calibrate_store(struct device *dev,
  418. struct device_attribute *attr,
  419. const char *buf, size_t count)
  420. {
  421. struct adxl34x *ac = dev_get_drvdata(dev);
  422. /*
  423. * Hardware offset calibration has a resolution of 15.6 mg/LSB.
  424. * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
  425. */
  426. mutex_lock(&ac->mutex);
  427. ac->hwcal.x -= (ac->saved.x / 4);
  428. ac->swcal.x = ac->saved.x % 4;
  429. ac->hwcal.y -= (ac->saved.y / 4);
  430. ac->swcal.y = ac->saved.y % 4;
  431. ac->hwcal.z -= (ac->saved.z / 4);
  432. ac->swcal.z = ac->saved.z % 4;
  433. AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
  434. AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
  435. AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
  436. mutex_unlock(&ac->mutex);
  437. return count;
  438. }
  439. static DEVICE_ATTR(calibrate, 0664,
  440. adxl34x_calibrate_show, adxl34x_calibrate_store);
  441. static ssize_t adxl34x_rate_show(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. struct adxl34x *ac = dev_get_drvdata(dev);
  445. return sprintf(buf, "%u\n", RATE(ac->pdata.data_rate));
  446. }
  447. static ssize_t adxl34x_rate_store(struct device *dev,
  448. struct device_attribute *attr,
  449. const char *buf, size_t count)
  450. {
  451. struct adxl34x *ac = dev_get_drvdata(dev);
  452. unsigned char val;
  453. int error;
  454. error = kstrtou8(buf, 10, &val);
  455. if (error)
  456. return error;
  457. mutex_lock(&ac->mutex);
  458. ac->pdata.data_rate = RATE(val);
  459. AC_WRITE(ac, BW_RATE,
  460. ac->pdata.data_rate |
  461. (ac->pdata.low_power_mode ? LOW_POWER : 0));
  462. mutex_unlock(&ac->mutex);
  463. return count;
  464. }
  465. static DEVICE_ATTR(rate, 0664, adxl34x_rate_show, adxl34x_rate_store);
  466. static ssize_t adxl34x_autosleep_show(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct adxl34x *ac = dev_get_drvdata(dev);
  470. return sprintf(buf, "%u\n",
  471. ac->pdata.power_mode & (PCTL_AUTO_SLEEP | PCTL_LINK) ? 1 : 0);
  472. }
  473. static ssize_t adxl34x_autosleep_store(struct device *dev,
  474. struct device_attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. struct adxl34x *ac = dev_get_drvdata(dev);
  478. unsigned int val;
  479. int error;
  480. error = kstrtouint(buf, 10, &val);
  481. if (error)
  482. return error;
  483. mutex_lock(&ac->mutex);
  484. if (val)
  485. ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
  486. else
  487. ac->pdata.power_mode &= ~(PCTL_AUTO_SLEEP | PCTL_LINK);
  488. if (!ac->disabled && !ac->suspended && ac->opened)
  489. AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
  490. mutex_unlock(&ac->mutex);
  491. return count;
  492. }
  493. static DEVICE_ATTR(autosleep, 0664,
  494. adxl34x_autosleep_show, adxl34x_autosleep_store);
  495. static ssize_t adxl34x_position_show(struct device *dev,
  496. struct device_attribute *attr, char *buf)
  497. {
  498. struct adxl34x *ac = dev_get_drvdata(dev);
  499. ssize_t count;
  500. mutex_lock(&ac->mutex);
  501. count = sprintf(buf, "(%d, %d, %d)\n",
  502. ac->saved.x, ac->saved.y, ac->saved.z);
  503. mutex_unlock(&ac->mutex);
  504. return count;
  505. }
  506. static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
  507. #ifdef ADXL_DEBUG
  508. static ssize_t adxl34x_write_store(struct device *dev,
  509. struct device_attribute *attr,
  510. const char *buf, size_t count)
  511. {
  512. struct adxl34x *ac = dev_get_drvdata(dev);
  513. unsigned int val;
  514. int error;
  515. /*
  516. * This allows basic ADXL register write access for debug purposes.
  517. */
  518. error = kstrtouint(buf, 16, &val);
  519. if (error)
  520. return error;
  521. mutex_lock(&ac->mutex);
  522. AC_WRITE(ac, val >> 8, val & 0xFF);
  523. mutex_unlock(&ac->mutex);
  524. return count;
  525. }
  526. static DEVICE_ATTR(write, 0664, NULL, adxl34x_write_store);
  527. #endif
  528. static struct attribute *adxl34x_attributes[] = {
  529. &dev_attr_disable.attr,
  530. &dev_attr_calibrate.attr,
  531. &dev_attr_rate.attr,
  532. &dev_attr_autosleep.attr,
  533. &dev_attr_position.attr,
  534. #ifdef ADXL_DEBUG
  535. &dev_attr_write.attr,
  536. #endif
  537. NULL
  538. };
  539. static const struct attribute_group adxl34x_attr_group = {
  540. .attrs = adxl34x_attributes,
  541. };
  542. static int adxl34x_input_open(struct input_dev *input)
  543. {
  544. struct adxl34x *ac = input_get_drvdata(input);
  545. mutex_lock(&ac->mutex);
  546. if (!ac->suspended && !ac->disabled)
  547. __adxl34x_enable(ac);
  548. ac->opened = true;
  549. mutex_unlock(&ac->mutex);
  550. return 0;
  551. }
  552. static void adxl34x_input_close(struct input_dev *input)
  553. {
  554. struct adxl34x *ac = input_get_drvdata(input);
  555. mutex_lock(&ac->mutex);
  556. if (!ac->suspended && !ac->disabled)
  557. __adxl34x_disable(ac);
  558. ac->opened = false;
  559. mutex_unlock(&ac->mutex);
  560. }
  561. struct adxl34x *adxl34x_probe(struct device *dev, int irq,
  562. bool fifo_delay_default,
  563. const struct adxl34x_bus_ops *bops)
  564. {
  565. struct adxl34x *ac;
  566. struct input_dev *input_dev;
  567. const struct adxl34x_platform_data *pdata;
  568. int err, range, i;
  569. unsigned char revid;
  570. if (!irq) {
  571. dev_err(dev, "no IRQ?\n");
  572. err = -ENODEV;
  573. goto err_out;
  574. }
  575. ac = kzalloc(sizeof(*ac), GFP_KERNEL);
  576. input_dev = input_allocate_device();
  577. if (!ac || !input_dev) {
  578. err = -ENOMEM;
  579. goto err_free_mem;
  580. }
  581. ac->fifo_delay = fifo_delay_default;
  582. pdata = dev->platform_data;
  583. if (!pdata) {
  584. dev_dbg(dev,
  585. "No platform data: Using default initialization\n");
  586. pdata = &adxl34x_default_init;
  587. }
  588. ac->pdata = *pdata;
  589. pdata = &ac->pdata;
  590. ac->input = input_dev;
  591. ac->dev = dev;
  592. ac->irq = irq;
  593. ac->bops = bops;
  594. mutex_init(&ac->mutex);
  595. input_dev->name = "ADXL34x accelerometer";
  596. revid = ac->bops->read(dev, DEVID);
  597. switch (revid) {
  598. case ID_ADXL345:
  599. ac->model = 345;
  600. break;
  601. case ID_ADXL346:
  602. ac->model = 346;
  603. break;
  604. default:
  605. dev_err(dev, "Failed to probe %s\n", input_dev->name);
  606. err = -ENODEV;
  607. goto err_free_mem;
  608. }
  609. snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
  610. input_dev->phys = ac->phys;
  611. input_dev->dev.parent = dev;
  612. input_dev->id.product = ac->model;
  613. input_dev->id.bustype = bops->bustype;
  614. input_dev->open = adxl34x_input_open;
  615. input_dev->close = adxl34x_input_close;
  616. input_set_drvdata(input_dev, ac);
  617. __set_bit(ac->pdata.ev_type, input_dev->evbit);
  618. if (ac->pdata.ev_type == EV_REL) {
  619. __set_bit(REL_X, input_dev->relbit);
  620. __set_bit(REL_Y, input_dev->relbit);
  621. __set_bit(REL_Z, input_dev->relbit);
  622. } else {
  623. /* EV_ABS */
  624. __set_bit(ABS_X, input_dev->absbit);
  625. __set_bit(ABS_Y, input_dev->absbit);
  626. __set_bit(ABS_Z, input_dev->absbit);
  627. if (pdata->data_range & FULL_RES)
  628. range = ADXL_FULLRES_MAX_VAL; /* Signed 13-bit */
  629. else
  630. range = ADXL_FIXEDRES_MAX_VAL; /* Signed 10-bit */
  631. input_set_abs_params(input_dev, ABS_X, -range, range, 3, 3);
  632. input_set_abs_params(input_dev, ABS_Y, -range, range, 3, 3);
  633. input_set_abs_params(input_dev, ABS_Z, -range, range, 3, 3);
  634. }
  635. __set_bit(EV_KEY, input_dev->evbit);
  636. __set_bit(pdata->ev_code_tap[ADXL_X_AXIS], input_dev->keybit);
  637. __set_bit(pdata->ev_code_tap[ADXL_Y_AXIS], input_dev->keybit);
  638. __set_bit(pdata->ev_code_tap[ADXL_Z_AXIS], input_dev->keybit);
  639. if (pdata->ev_code_ff) {
  640. ac->int_mask = FREE_FALL;
  641. __set_bit(pdata->ev_code_ff, input_dev->keybit);
  642. }
  643. if (pdata->ev_code_act_inactivity)
  644. __set_bit(pdata->ev_code_act_inactivity, input_dev->keybit);
  645. ac->int_mask |= ACTIVITY | INACTIVITY;
  646. if (pdata->watermark) {
  647. ac->int_mask |= WATERMARK;
  648. if (!FIFO_MODE(pdata->fifo_mode))
  649. ac->pdata.fifo_mode |= FIFO_STREAM;
  650. } else {
  651. ac->int_mask |= DATA_READY;
  652. }
  653. if (pdata->tap_axis_control & (TAP_X_EN | TAP_Y_EN | TAP_Z_EN))
  654. ac->int_mask |= SINGLE_TAP | DOUBLE_TAP;
  655. if (FIFO_MODE(pdata->fifo_mode) == FIFO_BYPASS)
  656. ac->fifo_delay = false;
  657. ac->bops->write(dev, POWER_CTL, 0);
  658. err = request_threaded_irq(ac->irq, NULL, adxl34x_irq,
  659. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  660. dev_name(dev), ac);
  661. if (err) {
  662. dev_err(dev, "irq %d busy?\n", ac->irq);
  663. goto err_free_mem;
  664. }
  665. err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
  666. if (err)
  667. goto err_free_irq;
  668. err = input_register_device(input_dev);
  669. if (err)
  670. goto err_remove_attr;
  671. AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
  672. AC_WRITE(ac, OFSX, pdata->x_axis_offset);
  673. ac->hwcal.x = pdata->x_axis_offset;
  674. AC_WRITE(ac, OFSY, pdata->y_axis_offset);
  675. ac->hwcal.y = pdata->y_axis_offset;
  676. AC_WRITE(ac, OFSZ, pdata->z_axis_offset);
  677. ac->hwcal.z = pdata->z_axis_offset;
  678. AC_WRITE(ac, THRESH_TAP, pdata->tap_threshold);
  679. AC_WRITE(ac, DUR, pdata->tap_duration);
  680. AC_WRITE(ac, LATENT, pdata->tap_latency);
  681. AC_WRITE(ac, WINDOW, pdata->tap_window);
  682. AC_WRITE(ac, THRESH_ACT, pdata->activity_threshold);
  683. AC_WRITE(ac, THRESH_INACT, pdata->inactivity_threshold);
  684. AC_WRITE(ac, TIME_INACT, pdata->inactivity_time);
  685. AC_WRITE(ac, THRESH_FF, pdata->free_fall_threshold);
  686. AC_WRITE(ac, TIME_FF, pdata->free_fall_time);
  687. AC_WRITE(ac, TAP_AXES, pdata->tap_axis_control);
  688. AC_WRITE(ac, ACT_INACT_CTL, pdata->act_axis_control);
  689. AC_WRITE(ac, BW_RATE, RATE(ac->pdata.data_rate) |
  690. (pdata->low_power_mode ? LOW_POWER : 0));
  691. AC_WRITE(ac, DATA_FORMAT, pdata->data_range);
  692. AC_WRITE(ac, FIFO_CTL, FIFO_MODE(pdata->fifo_mode) |
  693. SAMPLES(pdata->watermark));
  694. if (pdata->use_int2) {
  695. /* Map all INTs to INT2 */
  696. AC_WRITE(ac, INT_MAP, ac->int_mask | OVERRUN);
  697. } else {
  698. /* Map all INTs to INT1 */
  699. AC_WRITE(ac, INT_MAP, 0);
  700. }
  701. if (ac->model == 346 && ac->pdata.orientation_enable) {
  702. AC_WRITE(ac, ORIENT_CONF,
  703. ORIENT_DEADZONE(ac->pdata.deadzone_angle) |
  704. ORIENT_DIVISOR(ac->pdata.divisor_length));
  705. ac->orient2d_saved = 1234;
  706. ac->orient3d_saved = 1234;
  707. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_3D)
  708. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_3d); i++)
  709. __set_bit(pdata->ev_codes_orient_3d[i],
  710. input_dev->keybit);
  711. if (pdata->orientation_enable & ADXL_EN_ORIENTATION_2D)
  712. for (i = 0; i < ARRAY_SIZE(pdata->ev_codes_orient_2d); i++)
  713. __set_bit(pdata->ev_codes_orient_2d[i],
  714. input_dev->keybit);
  715. } else {
  716. ac->pdata.orientation_enable = 0;
  717. }
  718. AC_WRITE(ac, INT_ENABLE, ac->int_mask | OVERRUN);
  719. ac->pdata.power_mode &= (PCTL_AUTO_SLEEP | PCTL_LINK);
  720. return ac;
  721. err_remove_attr:
  722. sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
  723. err_free_irq:
  724. free_irq(ac->irq, ac);
  725. err_free_mem:
  726. input_free_device(input_dev);
  727. kfree(ac);
  728. err_out:
  729. return ERR_PTR(err);
  730. }
  731. EXPORT_SYMBOL_GPL(adxl34x_probe);
  732. int adxl34x_remove(struct adxl34x *ac)
  733. {
  734. sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
  735. free_irq(ac->irq, ac);
  736. input_unregister_device(ac->input);
  737. dev_dbg(ac->dev, "unregistered accelerometer\n");
  738. kfree(ac);
  739. return 0;
  740. }
  741. EXPORT_SYMBOL_GPL(adxl34x_remove);
  742. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  743. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer Driver");
  744. MODULE_LICENSE("GPL");