adxl345_core.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADXL345 3-Axis Digital Accelerometer IIO core driver
  4. *
  5. * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com>
  6. *
  7. * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. #include <linux/regmap.h>
  15. #include <linux/units.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/events.h>
  20. #include <linux/iio/kfifo_buf.h>
  21. #include "adxl345.h"
  22. #define ADXL345_FIFO_BYPASS 0
  23. #define ADXL345_FIFO_FIFO 1
  24. #define ADXL345_FIFO_STREAM 2
  25. #define ADXL345_DIRS 3
  26. #define ADXL345_INT_NONE 0xff
  27. #define ADXL345_INT1 0
  28. #define ADXL345_INT2 1
  29. #define ADXL345_REG_TAP_AXIS_MSK GENMASK(2, 0)
  30. #define ADXL345_REG_TAP_SUPPRESS_MSK BIT(3)
  31. #define ADXL345_REG_TAP_SUPPRESS BIT(3)
  32. #define ADXL345_POWER_CTL_INACT_MSK (ADXL345_POWER_CTL_AUTO_SLEEP | ADXL345_POWER_CTL_LINK)
  33. #define ADXL345_TAP_Z_EN BIT(0)
  34. #define ADXL345_TAP_Y_EN BIT(1)
  35. #define ADXL345_TAP_X_EN BIT(2)
  36. #define ADXL345_REG_TAP_SUPPRESS BIT(3)
  37. #define ADXL345_INACT_Z_EN BIT(0)
  38. #define ADXL345_INACT_Y_EN BIT(1)
  39. #define ADXL345_INACT_X_EN BIT(2)
  40. #define ADXL345_REG_INACT_ACDC BIT(3)
  41. #define ADXL345_ACT_INACT_NO_AXIS_EN 0x00
  42. #define ADXL345_INACT_XYZ_EN (ADXL345_INACT_Z_EN | ADXL345_INACT_Y_EN | ADXL345_INACT_X_EN)
  43. #define ADXL345_ACT_Z_EN BIT(4)
  44. #define ADXL345_ACT_Y_EN BIT(5)
  45. #define ADXL345_ACT_X_EN BIT(6)
  46. #define ADXL345_REG_ACT_ACDC BIT(7)
  47. #define ADXL345_ACT_XYZ_EN (ADXL345_ACT_Z_EN | ADXL345_ACT_Y_EN | ADXL345_ACT_X_EN)
  48. #define ADXL345_COUPLING_DC 0
  49. #define ADXL345_COUPLING_AC 1
  50. #define ADXL345_REG_NO_ACDC 0x00
  51. /* single/double tap */
  52. enum adxl345_tap_type {
  53. ADXL345_SINGLE_TAP,
  54. ADXL345_DOUBLE_TAP,
  55. };
  56. static const unsigned int adxl345_tap_int_reg[] = {
  57. [ADXL345_SINGLE_TAP] = ADXL345_INT_SINGLE_TAP,
  58. [ADXL345_DOUBLE_TAP] = ADXL345_INT_DOUBLE_TAP,
  59. };
  60. enum adxl345_tap_time_type {
  61. ADXL345_TAP_TIME_LATENT,
  62. ADXL345_TAP_TIME_WINDOW,
  63. ADXL345_TAP_TIME_DUR,
  64. };
  65. static const unsigned int adxl345_tap_time_reg[] = {
  66. [ADXL345_TAP_TIME_LATENT] = ADXL345_REG_LATENT,
  67. [ADXL345_TAP_TIME_WINDOW] = ADXL345_REG_WINDOW,
  68. [ADXL345_TAP_TIME_DUR] = ADXL345_REG_DUR,
  69. };
  70. /* activity/inactivity */
  71. enum adxl345_activity_type {
  72. ADXL345_ACTIVITY,
  73. ADXL345_INACTIVITY,
  74. ADXL345_ACTIVITY_AC,
  75. ADXL345_INACTIVITY_AC,
  76. ADXL345_INACTIVITY_FF,
  77. };
  78. static const unsigned int adxl345_act_int_reg[] = {
  79. [ADXL345_ACTIVITY] = ADXL345_INT_ACTIVITY,
  80. [ADXL345_INACTIVITY] = ADXL345_INT_INACTIVITY,
  81. [ADXL345_ACTIVITY_AC] = ADXL345_INT_ACTIVITY,
  82. [ADXL345_INACTIVITY_AC] = ADXL345_INT_INACTIVITY,
  83. [ADXL345_INACTIVITY_FF] = ADXL345_INT_FREE_FALL,
  84. };
  85. static const unsigned int adxl345_act_thresh_reg[] = {
  86. [ADXL345_ACTIVITY] = ADXL345_REG_THRESH_ACT,
  87. [ADXL345_INACTIVITY] = ADXL345_REG_THRESH_INACT,
  88. [ADXL345_ACTIVITY_AC] = ADXL345_REG_THRESH_ACT,
  89. [ADXL345_INACTIVITY_AC] = ADXL345_REG_THRESH_INACT,
  90. [ADXL345_INACTIVITY_FF] = ADXL345_REG_THRESH_FF,
  91. };
  92. static const unsigned int adxl345_act_acdc_msk[] = {
  93. [ADXL345_ACTIVITY] = ADXL345_REG_ACT_ACDC,
  94. [ADXL345_INACTIVITY] = ADXL345_REG_INACT_ACDC,
  95. [ADXL345_ACTIVITY_AC] = ADXL345_REG_ACT_ACDC,
  96. [ADXL345_INACTIVITY_AC] = ADXL345_REG_INACT_ACDC,
  97. [ADXL345_INACTIVITY_FF] = ADXL345_REG_NO_ACDC,
  98. };
  99. enum adxl345_odr {
  100. ADXL345_ODR_0P10HZ = 0,
  101. ADXL345_ODR_0P20HZ,
  102. ADXL345_ODR_0P39HZ,
  103. ADXL345_ODR_0P78HZ,
  104. ADXL345_ODR_1P56HZ,
  105. ADXL345_ODR_3P13HZ,
  106. ADXL345_ODR_6P25HZ,
  107. ADXL345_ODR_12P50HZ,
  108. ADXL345_ODR_25HZ,
  109. ADXL345_ODR_50HZ,
  110. ADXL345_ODR_100HZ,
  111. ADXL345_ODR_200HZ,
  112. ADXL345_ODR_400HZ,
  113. ADXL345_ODR_800HZ,
  114. ADXL345_ODR_1600HZ,
  115. ADXL345_ODR_3200HZ,
  116. };
  117. enum adxl345_range {
  118. ADXL345_2G_RANGE = 0,
  119. ADXL345_4G_RANGE,
  120. ADXL345_8G_RANGE,
  121. ADXL345_16G_RANGE,
  122. };
  123. /* Certain features recommend 12.5 Hz - 400 Hz ODR */
  124. static const int adxl345_odr_tbl[][2] = {
  125. [ADXL345_ODR_0P10HZ] = { 0, 97000 },
  126. [ADXL345_ODR_0P20HZ] = { 0, 195000 },
  127. [ADXL345_ODR_0P39HZ] = { 0, 390000 },
  128. [ADXL345_ODR_0P78HZ] = { 0, 781000 },
  129. [ADXL345_ODR_1P56HZ] = { 1, 562000 },
  130. [ADXL345_ODR_3P13HZ] = { 3, 125000 },
  131. [ADXL345_ODR_6P25HZ] = { 6, 250000 },
  132. [ADXL345_ODR_12P50HZ] = { 12, 500000 },
  133. [ADXL345_ODR_25HZ] = { 25, 0 },
  134. [ADXL345_ODR_50HZ] = { 50, 0 },
  135. [ADXL345_ODR_100HZ] = { 100, 0 },
  136. [ADXL345_ODR_200HZ] = { 200, 0 },
  137. [ADXL345_ODR_400HZ] = { 400, 0 },
  138. [ADXL345_ODR_800HZ] = { 800, 0 },
  139. [ADXL345_ODR_1600HZ] = { 1600, 0 },
  140. [ADXL345_ODR_3200HZ] = { 3200, 0 },
  141. };
  142. /*
  143. * Full resolution frequency table:
  144. * (g * 2 * 9.80665) / (2^(resolution) - 1)
  145. *
  146. * resolution := 13 (full)
  147. * g := 2|4|8|16
  148. *
  149. * 2g at 13bit: 0.004789
  150. * 4g at 13bit: 0.009578
  151. * 8g at 13bit: 0.019156
  152. * 16g at 16bit: 0.038312
  153. */
  154. static const int adxl345_fullres_range_tbl[][2] = {
  155. [ADXL345_2G_RANGE] = { 0, 4789 },
  156. [ADXL345_4G_RANGE] = { 0, 9578 },
  157. [ADXL345_8G_RANGE] = { 0, 19156 },
  158. [ADXL345_16G_RANGE] = { 0, 38312 },
  159. };
  160. /* scaling */
  161. static const int adxl345_range_factor_tbl[] = {
  162. [ADXL345_2G_RANGE] = 1,
  163. [ADXL345_4G_RANGE] = 2,
  164. [ADXL345_8G_RANGE] = 4,
  165. [ADXL345_16G_RANGE] = 8,
  166. };
  167. struct adxl345_state {
  168. const struct adxl345_chip_info *info;
  169. struct regmap *regmap;
  170. bool fifo_delay; /* delay: delay is needed for SPI */
  171. u8 watermark;
  172. u8 fifo_mode;
  173. u8 inact_threshold;
  174. u32 inact_time_ms;
  175. u32 tap_duration_us;
  176. u32 tap_latent_us;
  177. u32 tap_window_us;
  178. __le16 fifo_buf[ADXL345_DIRS * ADXL345_FIFO_SIZE + 1] __aligned(IIO_DMA_MINALIGN);
  179. };
  180. static const struct iio_event_spec adxl345_events[] = {
  181. {
  182. /* activity */
  183. .type = IIO_EV_TYPE_MAG,
  184. .dir = IIO_EV_DIR_RISING,
  185. .mask_shared_by_type =
  186. BIT(IIO_EV_INFO_ENABLE) |
  187. BIT(IIO_EV_INFO_VALUE),
  188. },
  189. {
  190. /* activity, ac bit set */
  191. .type = IIO_EV_TYPE_MAG_ADAPTIVE,
  192. .dir = IIO_EV_DIR_RISING,
  193. .mask_shared_by_type =
  194. BIT(IIO_EV_INFO_ENABLE) |
  195. BIT(IIO_EV_INFO_VALUE),
  196. },
  197. {
  198. /* single tap */
  199. .type = IIO_EV_TYPE_GESTURE,
  200. .dir = IIO_EV_DIR_SINGLETAP,
  201. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  202. .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
  203. BIT(IIO_EV_INFO_TIMEOUT),
  204. },
  205. {
  206. /* double tap */
  207. .type = IIO_EV_TYPE_GESTURE,
  208. .dir = IIO_EV_DIR_DOUBLETAP,
  209. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  210. BIT(IIO_EV_INFO_RESET_TIMEOUT) |
  211. BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
  212. },
  213. };
  214. #define ADXL345_CHANNEL(index, reg, axis) { \
  215. .type = IIO_ACCEL, \
  216. .modified = 1, \
  217. .channel2 = IIO_MOD_##axis, \
  218. .address = (reg), \
  219. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  220. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  221. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  222. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  223. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
  224. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  225. .scan_index = (index), \
  226. .scan_type = { \
  227. .sign = 's', \
  228. .realbits = 13, \
  229. .storagebits = 16, \
  230. .endianness = IIO_LE, \
  231. }, \
  232. .event_spec = adxl345_events, \
  233. .num_event_specs = ARRAY_SIZE(adxl345_events), \
  234. }
  235. enum adxl345_chans {
  236. chan_x, chan_y, chan_z,
  237. };
  238. static const struct iio_event_spec adxl345_fake_chan_events[] = {
  239. {
  240. /* inactivity */
  241. .type = IIO_EV_TYPE_MAG,
  242. .dir = IIO_EV_DIR_FALLING,
  243. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  244. .mask_shared_by_type =
  245. BIT(IIO_EV_INFO_VALUE) |
  246. BIT(IIO_EV_INFO_PERIOD),
  247. },
  248. {
  249. /* inactivity, AC bit set */
  250. .type = IIO_EV_TYPE_MAG_ADAPTIVE,
  251. .dir = IIO_EV_DIR_FALLING,
  252. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  253. .mask_shared_by_type =
  254. BIT(IIO_EV_INFO_VALUE) |
  255. BIT(IIO_EV_INFO_PERIOD),
  256. },
  257. };
  258. static const struct iio_chan_spec adxl345_channels[] = {
  259. ADXL345_CHANNEL(0, chan_x, X),
  260. ADXL345_CHANNEL(1, chan_y, Y),
  261. ADXL345_CHANNEL(2, chan_z, Z),
  262. {
  263. .type = IIO_ACCEL,
  264. .modified = 1,
  265. .channel2 = IIO_MOD_X_AND_Y_AND_Z,
  266. .scan_index = -1, /* Fake channel */
  267. .event_spec = adxl345_fake_chan_events,
  268. .num_event_specs = ARRAY_SIZE(adxl345_fake_chan_events),
  269. },
  270. };
  271. static const unsigned long adxl345_scan_masks[] = {
  272. BIT(chan_x) | BIT(chan_y) | BIT(chan_z),
  273. 0
  274. };
  275. bool adxl345_is_volatile_reg(struct device *dev, unsigned int reg)
  276. {
  277. switch (reg) {
  278. case ADXL345_REG_DATA_AXIS(0):
  279. case ADXL345_REG_DATA_AXIS(1):
  280. case ADXL345_REG_DATA_AXIS(2):
  281. case ADXL345_REG_DATA_AXIS(3):
  282. case ADXL345_REG_DATA_AXIS(4):
  283. case ADXL345_REG_DATA_AXIS(5):
  284. case ADXL345_REG_ACT_TAP_STATUS:
  285. case ADXL345_REG_FIFO_STATUS:
  286. case ADXL345_REG_INT_SOURCE:
  287. return true;
  288. default:
  289. return false;
  290. }
  291. }
  292. EXPORT_SYMBOL_NS_GPL(adxl345_is_volatile_reg, "IIO_ADXL345");
  293. /**
  294. * adxl345_set_measure_en() - Enable and disable measuring.
  295. *
  296. * @st: The device data.
  297. * @en: Enable measurements, else standby mode.
  298. *
  299. * For lowest power operation, standby mode can be used. In standby mode,
  300. * current consumption is supposed to be reduced to 0.1uA (typical). In this
  301. * mode no measurements are made. Placing the device into standby mode
  302. * preserves the contents of FIFO.
  303. *
  304. * Return: Returns 0 if successful, or a negative error value.
  305. */
  306. static int adxl345_set_measure_en(struct adxl345_state *st, bool en)
  307. {
  308. return regmap_assign_bits(st->regmap, ADXL345_REG_POWER_CTL,
  309. ADXL345_POWER_CTL_MEASURE, en);
  310. }
  311. /* activity / inactivity */
  312. static int adxl345_set_inact_threshold(struct adxl345_state *st,
  313. unsigned int threshold)
  314. {
  315. int ret;
  316. st->inact_threshold = min(U8_MAX, threshold);
  317. ret = regmap_write(st->regmap,
  318. adxl345_act_thresh_reg[ADXL345_INACTIVITY],
  319. st->inact_threshold);
  320. if (ret)
  321. return ret;
  322. return regmap_write(st->regmap,
  323. adxl345_act_thresh_reg[ADXL345_INACTIVITY_FF],
  324. st->inact_threshold);
  325. }
  326. static int adxl345_set_default_time(struct adxl345_state *st)
  327. {
  328. int max_boundary = U8_MAX;
  329. int min_boundary = 10;
  330. enum adxl345_odr odr;
  331. unsigned int regval;
  332. unsigned int val;
  333. int ret;
  334. /* Generated inactivity time based on ODR */
  335. ret = regmap_read(st->regmap, ADXL345_REG_BW_RATE, &regval);
  336. if (ret)
  337. return ret;
  338. odr = FIELD_GET(ADXL345_BW_RATE_MSK, regval);
  339. val = clamp(max_boundary - adxl345_odr_tbl[odr][0],
  340. min_boundary, max_boundary);
  341. st->inact_time_ms = MILLI * val;
  342. /* Inactivity time in s */
  343. return regmap_write(st->regmap, ADXL345_REG_TIME_INACT, val);
  344. }
  345. static int adxl345_set_inactivity_time(struct adxl345_state *st, u32 val_int)
  346. {
  347. st->inact_time_ms = MILLI * val_int;
  348. return regmap_write(st->regmap, ADXL345_REG_TIME_INACT, val_int);
  349. }
  350. static int adxl345_set_freefall_time(struct adxl345_state *st, u32 val_fract)
  351. {
  352. /*
  353. * Datasheet max. value is 255 * 5000 us = 1.275000 seconds.
  354. *
  355. * Recommended values between 100ms and 350ms (0x14 to 0x46)
  356. */
  357. st->inact_time_ms = DIV_ROUND_UP(val_fract, MILLI);
  358. return regmap_write(st->regmap, ADXL345_REG_TIME_FF,
  359. DIV_ROUND_CLOSEST(val_fract, 5));
  360. }
  361. /**
  362. * adxl345_set_inact_time - Configure inactivity time explicitly or by ODR.
  363. * @st: The sensor state instance.
  364. * @val_int: The inactivity time, integer part.
  365. * @val_fract: The inactivity time, fractional part when val_int is 0.
  366. *
  367. * Inactivity time can be configured between 1 and 255 seconds. If a user sets
  368. * val_s to 0, a default inactivity time is calculated automatically (since 0 is
  369. * also invalid and undefined by the sensor).
  370. *
  371. * In such cases, power consumption should be considered: the inactivity period
  372. * should be shorter at higher sampling frequencies and longer at lower ones.
  373. * Specifically, for frequencies above 255 Hz, the default is set to 10 seconds;
  374. * for frequencies below 10 Hz, it defaults to 255 seconds.
  375. *
  376. * The calculation method subtracts the integer part of the configured sample
  377. * frequency from 255 to estimate the inactivity time in seconds. Sub-Hertz
  378. * values are ignored in this approximation. Since the recommended output data
  379. * rates (ODRs) for features like activity/inactivity detection, sleep modes,
  380. * and free fall range between 12.5 Hz and 400 Hz, frequencies outside this
  381. * range will either use the defined boundary defaults or require explicit
  382. * configuration via val_s.
  383. *
  384. * Return: 0 or error value.
  385. */
  386. static int adxl345_set_inact_time(struct adxl345_state *st, u32 val_int,
  387. u32 val_fract)
  388. {
  389. if (val_int > 0) {
  390. /* Time >= 1s, inactivity */
  391. return adxl345_set_inactivity_time(st, val_int);
  392. } else if (val_int == 0) {
  393. if (val_fract > 0) {
  394. /* Time < 1s, free-fall */
  395. return adxl345_set_freefall_time(st, val_fract);
  396. } else if (val_fract == 0) {
  397. /* Time == 0.0s */
  398. return adxl345_set_default_time(st);
  399. }
  400. }
  401. /* Do not support negative or wrong input. */
  402. return -EINVAL;
  403. }
  404. /**
  405. * adxl345_is_act_inact_ac() - Verify if AC or DC coupling is currently enabled.
  406. *
  407. * @st: The device data.
  408. * @type: The activity or inactivity type.
  409. *
  410. * Given a type of activity / inactivity combined with either AC coupling set or
  411. * default to DC, this function verifies if the combination is currently
  412. * configured, hence enabled or not.
  413. *
  414. * Return: true if configured coupling matches the provided type, else a negative
  415. * error value.
  416. */
  417. static int adxl345_is_act_inact_ac(struct adxl345_state *st,
  418. enum adxl345_activity_type type)
  419. {
  420. unsigned int regval;
  421. bool coupling;
  422. int ret;
  423. if (type == ADXL345_INACTIVITY_FF)
  424. return true;
  425. ret = regmap_read(st->regmap, ADXL345_REG_ACT_INACT_CTRL, &regval);
  426. if (ret)
  427. return ret;
  428. coupling = adxl345_act_acdc_msk[type] & regval;
  429. switch (type) {
  430. case ADXL345_ACTIVITY:
  431. case ADXL345_INACTIVITY:
  432. return coupling == ADXL345_COUPLING_DC;
  433. case ADXL345_ACTIVITY_AC:
  434. case ADXL345_INACTIVITY_AC:
  435. return coupling == ADXL345_COUPLING_AC;
  436. default:
  437. return -EINVAL;
  438. }
  439. }
  440. /**
  441. * adxl345_set_act_inact_ac() - Configure AC coupling or DC coupling.
  442. *
  443. * @st: The device data.
  444. * @type: Provide a type of activity or inactivity.
  445. * @cmd_en: enable or disable AC coupling.
  446. *
  447. * Enables AC coupling or DC coupling depending on the provided type argument.
  448. * Note: Activity and inactivity can be either AC coupled or DC coupled not
  449. * both at the same time.
  450. *
  451. * Return: 0 if successful, else error value.
  452. */
  453. static int adxl345_set_act_inact_ac(struct adxl345_state *st,
  454. enum adxl345_activity_type type,
  455. bool cmd_en)
  456. {
  457. unsigned int act_inact_ac;
  458. if (type == ADXL345_ACTIVITY_AC || type == ADXL345_INACTIVITY_AC)
  459. act_inact_ac = ADXL345_COUPLING_AC && cmd_en;
  460. else
  461. act_inact_ac = ADXL345_COUPLING_DC && cmd_en;
  462. /*
  463. * A setting of false selects dc-coupled operation, and a setting of
  464. * true enables ac-coupled operation. In dc-coupled operation, the
  465. * current acceleration magnitude is compared directly with
  466. * ADXL345_REG_THRESH_ACT and ADXL345_REG_THRESH_INACT to determine
  467. * whether activity or inactivity is detected.
  468. *
  469. * In ac-coupled operation for activity detection, the acceleration
  470. * value at the start of activity detection is taken as a reference
  471. * value. New samples of acceleration are then compared to this
  472. * reference value, and if the magnitude of the difference exceeds the
  473. * ADXL345_REG_THRESH_ACT value, the device triggers an activity
  474. * interrupt.
  475. *
  476. * Similarly, in ac-coupled operation for inactivity detection, a
  477. * reference value is used for comparison and is updated whenever the
  478. * device exceeds the inactivity threshold. After the reference value
  479. * is selected, the device compares the magnitude of the difference
  480. * between the reference value and the current acceleration with
  481. * ADXL345_REG_THRESH_INACT. If the difference is less than the value in
  482. * ADXL345_REG_THRESH_INACT for the time in ADXL345_REG_TIME_INACT, the
  483. * device is considered inactive and the inactivity interrupt is
  484. * triggered. [quoted from p. 24, ADXL345 datasheet Rev. G]
  485. *
  486. * In a conclusion, the first acceleration snapshot sample which hit the
  487. * threshold in a particular direction is always taken as acceleration
  488. * reference value to that direction. Since for the hardware activity
  489. * and inactivity depend on the x/y/z axis, so do ac and dc coupling.
  490. * Note, this sw driver always enables or disables all three x/y/z axis
  491. * for detection via act_axis_ctrl and inact_axis_ctrl, respectively.
  492. * Where in dc-coupling samples are compared against the thresholds, in
  493. * ac-coupling measurement difference to the first acceleration
  494. * reference value are compared against the threshold. So, ac-coupling
  495. * allows for a bit more dynamic compensation depending on the initial
  496. * sample.
  497. */
  498. return regmap_assign_bits(st->regmap, ADXL345_REG_ACT_INACT_CTRL,
  499. adxl345_act_acdc_msk[type], act_inact_ac);
  500. }
  501. static int adxl345_is_act_inact_en(struct adxl345_state *st,
  502. enum adxl345_activity_type type)
  503. {
  504. unsigned int axis_ctrl;
  505. unsigned int regval;
  506. bool int_en, en;
  507. int ret;
  508. ret = regmap_read(st->regmap, ADXL345_REG_ACT_INACT_CTRL, &axis_ctrl);
  509. if (ret)
  510. return ret;
  511. /* Check if axis for activity are enabled */
  512. switch (type) {
  513. case ADXL345_ACTIVITY:
  514. case ADXL345_ACTIVITY_AC:
  515. en = FIELD_GET(ADXL345_ACT_XYZ_EN, axis_ctrl);
  516. if (!en)
  517. return false;
  518. break;
  519. case ADXL345_INACTIVITY:
  520. case ADXL345_INACTIVITY_AC:
  521. en = FIELD_GET(ADXL345_INACT_XYZ_EN, axis_ctrl);
  522. if (!en)
  523. return false;
  524. break;
  525. case ADXL345_INACTIVITY_FF:
  526. en = true;
  527. break;
  528. default:
  529. return -EINVAL;
  530. }
  531. /* Check if specific interrupt is enabled */
  532. ret = regmap_read(st->regmap, ADXL345_REG_INT_ENABLE, &regval);
  533. if (ret)
  534. return ret;
  535. int_en = adxl345_act_int_reg[type] & regval;
  536. if (!int_en)
  537. return false;
  538. /* Check if configured coupling matches provided type */
  539. return adxl345_is_act_inact_ac(st, type);
  540. }
  541. static int adxl345_set_act_inact_linkbit(struct adxl345_state *st,
  542. enum adxl345_activity_type type,
  543. bool en)
  544. {
  545. int act_ac_en, inact_ac_en;
  546. int act_en, inact_en;
  547. act_en = adxl345_is_act_inact_en(st, ADXL345_ACTIVITY);
  548. if (act_en < 0)
  549. return act_en;
  550. act_ac_en = adxl345_is_act_inact_en(st, ADXL345_ACTIVITY_AC);
  551. if (act_ac_en < 0)
  552. return act_ac_en;
  553. if (type == ADXL345_INACTIVITY_FF) {
  554. inact_en = false;
  555. } else {
  556. inact_en = adxl345_is_act_inact_en(st, ADXL345_INACTIVITY);
  557. if (inact_en < 0)
  558. return inact_en;
  559. inact_ac_en = adxl345_is_act_inact_en(st, ADXL345_INACTIVITY_AC);
  560. if (inact_ac_en < 0)
  561. return inact_ac_en;
  562. inact_en = inact_en || inact_ac_en;
  563. }
  564. act_en = act_en || act_ac_en;
  565. return regmap_assign_bits(st->regmap, ADXL345_REG_POWER_CTL,
  566. ADXL345_POWER_CTL_INACT_MSK,
  567. en && act_en && inact_en);
  568. }
  569. static int adxl345_set_act_inact_en(struct adxl345_state *st,
  570. enum adxl345_activity_type type,
  571. bool cmd_en)
  572. {
  573. unsigned int axis_ctrl;
  574. unsigned int threshold;
  575. unsigned int period;
  576. int ret;
  577. if (cmd_en) {
  578. /* When turning on, check if threshold is valid */
  579. if (type == ADXL345_ACTIVITY || type == ADXL345_ACTIVITY_AC) {
  580. ret = regmap_read(st->regmap,
  581. adxl345_act_thresh_reg[type],
  582. &threshold);
  583. if (ret)
  584. return ret;
  585. } else {
  586. threshold = st->inact_threshold;
  587. }
  588. if (!threshold) /* Just ignore the command if threshold is 0 */
  589. return 0;
  590. /* When turning on inactivity, check if inact time is valid */
  591. if (type == ADXL345_INACTIVITY || type == ADXL345_INACTIVITY_AC) {
  592. ret = regmap_read(st->regmap,
  593. ADXL345_REG_TIME_INACT,
  594. &period);
  595. if (ret)
  596. return ret;
  597. if (!period)
  598. return 0;
  599. }
  600. } else {
  601. /*
  602. * When turning off an activity, ensure that the correct
  603. * coupling event is specified. This step helps prevent misuse -
  604. * for example, if an AC-coupled activity is active and the
  605. * current call attempts to turn off a DC-coupled activity, this
  606. * inconsistency should be detected here.
  607. */
  608. if (adxl345_is_act_inact_ac(st, type) <= 0)
  609. return 0;
  610. }
  611. /* Start modifying configuration registers */
  612. ret = adxl345_set_measure_en(st, false);
  613. if (ret)
  614. return ret;
  615. /* Enable axis according to the command */
  616. switch (type) {
  617. case ADXL345_ACTIVITY:
  618. case ADXL345_ACTIVITY_AC:
  619. axis_ctrl = ADXL345_ACT_XYZ_EN;
  620. break;
  621. case ADXL345_INACTIVITY:
  622. case ADXL345_INACTIVITY_AC:
  623. axis_ctrl = ADXL345_INACT_XYZ_EN;
  624. break;
  625. case ADXL345_INACTIVITY_FF:
  626. axis_ctrl = ADXL345_ACT_INACT_NO_AXIS_EN;
  627. break;
  628. default:
  629. return -EINVAL;
  630. }
  631. ret = regmap_assign_bits(st->regmap, ADXL345_REG_ACT_INACT_CTRL,
  632. axis_ctrl, cmd_en);
  633. if (ret)
  634. return ret;
  635. /* Update AC/DC-coupling according to the command */
  636. ret = adxl345_set_act_inact_ac(st, type, cmd_en);
  637. if (ret)
  638. return ret;
  639. /* Enable the interrupt line, according to the command */
  640. ret = regmap_assign_bits(st->regmap, ADXL345_REG_INT_ENABLE,
  641. adxl345_act_int_reg[type], cmd_en);
  642. if (ret)
  643. return ret;
  644. /* Set link-bit and auto-sleep only when ACT and INACT are enabled */
  645. ret = adxl345_set_act_inact_linkbit(st, type, cmd_en);
  646. if (ret)
  647. return ret;
  648. return adxl345_set_measure_en(st, true);
  649. }
  650. /* tap */
  651. static int _adxl345_set_tap_int(struct adxl345_state *st,
  652. enum adxl345_tap_type type, bool state)
  653. {
  654. unsigned int int_map = 0x00;
  655. unsigned int tap_threshold;
  656. bool axis_valid;
  657. bool singletap_args_valid = false;
  658. bool doubletap_args_valid = false;
  659. bool en = false;
  660. u32 axis_ctrl;
  661. int ret;
  662. ret = regmap_read(st->regmap, ADXL345_REG_TAP_AXIS, &axis_ctrl);
  663. if (ret)
  664. return ret;
  665. axis_valid = FIELD_GET(ADXL345_REG_TAP_AXIS_MSK, axis_ctrl) > 0;
  666. ret = regmap_read(st->regmap, ADXL345_REG_THRESH_TAP, &tap_threshold);
  667. if (ret)
  668. return ret;
  669. /*
  670. * Note: A value of 0 for threshold and/or dur may result in undesirable
  671. * behavior if single tap/double tap interrupts are enabled.
  672. */
  673. singletap_args_valid = tap_threshold > 0 && st->tap_duration_us > 0;
  674. if (type == ADXL345_SINGLE_TAP) {
  675. en = axis_valid && singletap_args_valid;
  676. } else {
  677. /* doubletap: Window must be equal or greater than latent! */
  678. doubletap_args_valid = st->tap_latent_us > 0 &&
  679. st->tap_window_us > 0 &&
  680. st->tap_window_us >= st->tap_latent_us;
  681. en = axis_valid && singletap_args_valid && doubletap_args_valid;
  682. }
  683. if (state && en)
  684. int_map |= adxl345_tap_int_reg[type];
  685. return regmap_update_bits(st->regmap, ADXL345_REG_INT_ENABLE,
  686. adxl345_tap_int_reg[type], int_map);
  687. }
  688. static int adxl345_is_tap_en(struct adxl345_state *st,
  689. enum iio_modifier axis,
  690. enum adxl345_tap_type type, bool *en)
  691. {
  692. unsigned int regval;
  693. u32 axis_ctrl;
  694. int ret;
  695. ret = regmap_read(st->regmap, ADXL345_REG_TAP_AXIS, &axis_ctrl);
  696. if (ret)
  697. return ret;
  698. /* Verify if axis is enabled for the tap detection. */
  699. switch (axis) {
  700. case IIO_MOD_X:
  701. *en = FIELD_GET(ADXL345_TAP_X_EN, axis_ctrl);
  702. break;
  703. case IIO_MOD_Y:
  704. *en = FIELD_GET(ADXL345_TAP_Y_EN, axis_ctrl);
  705. break;
  706. case IIO_MOD_Z:
  707. *en = FIELD_GET(ADXL345_TAP_Z_EN, axis_ctrl);
  708. break;
  709. default:
  710. *en = false;
  711. return -EINVAL;
  712. }
  713. if (*en) {
  714. /*
  715. * If axis allow for tap detection, verify if the interrupt is
  716. * enabled for tap detection.
  717. */
  718. ret = regmap_read(st->regmap, ADXL345_REG_INT_ENABLE, &regval);
  719. if (ret)
  720. return ret;
  721. *en = adxl345_tap_int_reg[type] & regval;
  722. }
  723. return 0;
  724. }
  725. static int adxl345_set_singletap_en(struct adxl345_state *st,
  726. enum iio_modifier axis, bool en)
  727. {
  728. int ret;
  729. u32 axis_ctrl;
  730. switch (axis) {
  731. case IIO_MOD_X:
  732. axis_ctrl = ADXL345_TAP_X_EN;
  733. break;
  734. case IIO_MOD_Y:
  735. axis_ctrl = ADXL345_TAP_Y_EN;
  736. break;
  737. case IIO_MOD_Z:
  738. axis_ctrl = ADXL345_TAP_Z_EN;
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. if (en)
  744. ret = regmap_set_bits(st->regmap, ADXL345_REG_TAP_AXIS,
  745. axis_ctrl);
  746. else
  747. ret = regmap_clear_bits(st->regmap, ADXL345_REG_TAP_AXIS,
  748. axis_ctrl);
  749. if (ret)
  750. return ret;
  751. return _adxl345_set_tap_int(st, ADXL345_SINGLE_TAP, en);
  752. }
  753. static int adxl345_set_doubletap_en(struct adxl345_state *st, bool en)
  754. {
  755. int ret;
  756. /*
  757. * Generally suppress detection of spikes during the latency period as
  758. * double taps here, this is fully optional for double tap detection
  759. */
  760. ret = regmap_assign_bits(st->regmap, ADXL345_REG_TAP_AXIS,
  761. ADXL345_REG_TAP_SUPPRESS, en);
  762. if (ret)
  763. return ret;
  764. return _adxl345_set_tap_int(st, ADXL345_DOUBLE_TAP, en);
  765. }
  766. static int _adxl345_set_tap_time(struct adxl345_state *st,
  767. enum adxl345_tap_time_type type, u32 val_us)
  768. {
  769. unsigned int regval;
  770. switch (type) {
  771. case ADXL345_TAP_TIME_WINDOW:
  772. st->tap_window_us = val_us;
  773. break;
  774. case ADXL345_TAP_TIME_LATENT:
  775. st->tap_latent_us = val_us;
  776. break;
  777. case ADXL345_TAP_TIME_DUR:
  778. st->tap_duration_us = val_us;
  779. break;
  780. }
  781. /*
  782. * The scale factor is 1250us / LSB for tap_window_us and tap_latent_us.
  783. * For tap_duration_us the scale factor is 625us / LSB.
  784. */
  785. if (type == ADXL345_TAP_TIME_DUR)
  786. regval = DIV_ROUND_CLOSEST(val_us, 625);
  787. else
  788. regval = DIV_ROUND_CLOSEST(val_us, 1250);
  789. return regmap_write(st->regmap, adxl345_tap_time_reg[type], regval);
  790. }
  791. static int adxl345_set_tap_duration(struct adxl345_state *st, u32 val_int,
  792. u32 val_fract_us)
  793. {
  794. /*
  795. * Max value is 255 * 625 us = 0.159375 seconds
  796. *
  797. * Note: the scaling is similar to the scaling in the ADXL380
  798. */
  799. if (val_int || val_fract_us > 159375)
  800. return -EINVAL;
  801. return _adxl345_set_tap_time(st, ADXL345_TAP_TIME_DUR, val_fract_us);
  802. }
  803. static int adxl345_set_tap_window(struct adxl345_state *st, u32 val_int,
  804. u32 val_fract_us)
  805. {
  806. /*
  807. * Max value is 255 * 1250 us = 0.318750 seconds
  808. *
  809. * Note: the scaling is similar to the scaling in the ADXL380
  810. */
  811. if (val_int || val_fract_us > 318750)
  812. return -EINVAL;
  813. return _adxl345_set_tap_time(st, ADXL345_TAP_TIME_WINDOW, val_fract_us);
  814. }
  815. static int adxl345_set_tap_latent(struct adxl345_state *st, u32 val_int,
  816. u32 val_fract_us)
  817. {
  818. /*
  819. * Max value is 255 * 1250 us = 0.318750 seconds
  820. *
  821. * Note: the scaling is similar to the scaling in the ADXL380
  822. */
  823. if (val_int || val_fract_us > 318750)
  824. return -EINVAL;
  825. return _adxl345_set_tap_time(st, ADXL345_TAP_TIME_LATENT, val_fract_us);
  826. }
  827. static int adxl345_find_odr(struct adxl345_state *st, int val,
  828. int val2, enum adxl345_odr *odr)
  829. {
  830. int i;
  831. for (i = 0; i < ARRAY_SIZE(adxl345_odr_tbl); i++) {
  832. if (val == adxl345_odr_tbl[i][0] &&
  833. val2 == adxl345_odr_tbl[i][1]) {
  834. *odr = i;
  835. return 0;
  836. }
  837. }
  838. return -EINVAL;
  839. }
  840. static int adxl345_set_odr(struct adxl345_state *st, enum adxl345_odr odr)
  841. {
  842. int ret;
  843. ret = regmap_update_bits(st->regmap, ADXL345_REG_BW_RATE,
  844. ADXL345_BW_RATE_MSK,
  845. FIELD_PREP(ADXL345_BW_RATE_MSK, odr));
  846. if (ret)
  847. return ret;
  848. /* update inactivity time by ODR */
  849. return adxl345_set_inact_time(st, 0, 0);
  850. }
  851. static int adxl345_find_range(struct adxl345_state *st, int val, int val2,
  852. enum adxl345_range *range)
  853. {
  854. int i;
  855. for (i = 0; i < ARRAY_SIZE(adxl345_fullres_range_tbl); i++) {
  856. if (val == adxl345_fullres_range_tbl[i][0] &&
  857. val2 == adxl345_fullres_range_tbl[i][1]) {
  858. *range = i;
  859. return 0;
  860. }
  861. }
  862. return -EINVAL;
  863. }
  864. static int adxl345_set_range(struct adxl345_state *st, enum adxl345_range range)
  865. {
  866. unsigned int act_threshold, inact_threshold;
  867. unsigned int range_old;
  868. unsigned int regval;
  869. int ret;
  870. ret = regmap_read(st->regmap, ADXL345_REG_DATA_FORMAT, &regval);
  871. if (ret)
  872. return ret;
  873. range_old = FIELD_GET(ADXL345_DATA_FORMAT_RANGE, regval);
  874. ret = regmap_read(st->regmap,
  875. adxl345_act_thresh_reg[ADXL345_ACTIVITY],
  876. &act_threshold);
  877. if (ret)
  878. return ret;
  879. ret = regmap_update_bits(st->regmap, ADXL345_REG_DATA_FORMAT,
  880. ADXL345_DATA_FORMAT_RANGE,
  881. FIELD_PREP(ADXL345_DATA_FORMAT_RANGE, range));
  882. if (ret)
  883. return ret;
  884. act_threshold = act_threshold * adxl345_range_factor_tbl[range_old]
  885. / adxl345_range_factor_tbl[range];
  886. act_threshold = min(U8_MAX, max(1, act_threshold));
  887. inact_threshold = st->inact_threshold;
  888. inact_threshold = inact_threshold * adxl345_range_factor_tbl[range_old]
  889. / adxl345_range_factor_tbl[range];
  890. inact_threshold = min(U8_MAX, max(1, inact_threshold));
  891. ret = regmap_write(st->regmap, adxl345_act_thresh_reg[ADXL345_ACTIVITY],
  892. act_threshold);
  893. if (ret)
  894. return ret;
  895. return adxl345_set_inact_threshold(st, inact_threshold);
  896. }
  897. static int adxl345_read_avail(struct iio_dev *indio_dev,
  898. struct iio_chan_spec const *chan,
  899. const int **vals, int *type,
  900. int *length, long mask)
  901. {
  902. switch (mask) {
  903. case IIO_CHAN_INFO_SCALE:
  904. *vals = (int *)adxl345_fullres_range_tbl;
  905. *type = IIO_VAL_INT_PLUS_MICRO;
  906. *length = ARRAY_SIZE(adxl345_fullres_range_tbl) * 2;
  907. return IIO_AVAIL_LIST;
  908. case IIO_CHAN_INFO_SAMP_FREQ:
  909. *vals = (int *)adxl345_odr_tbl;
  910. *type = IIO_VAL_INT_PLUS_MICRO;
  911. *length = ARRAY_SIZE(adxl345_odr_tbl) * 2;
  912. return IIO_AVAIL_LIST;
  913. }
  914. return -EINVAL;
  915. }
  916. static int adxl345_read_raw(struct iio_dev *indio_dev,
  917. struct iio_chan_spec const *chan,
  918. int *val, int *val2, long mask)
  919. {
  920. struct adxl345_state *st = iio_priv(indio_dev);
  921. __le16 accel;
  922. unsigned int regval;
  923. enum adxl345_odr odr;
  924. enum adxl345_range range;
  925. int ret;
  926. switch (mask) {
  927. case IIO_CHAN_INFO_RAW:
  928. /*
  929. * Data is stored in adjacent registers:
  930. * ADXL345_REG_DATA(X0/Y0/Z0) contain the least significant byte
  931. * and ADXL345_REG_DATA(X0/Y0/Z0) + 1 the most significant byte
  932. */
  933. ret = regmap_bulk_read(st->regmap,
  934. ADXL345_REG_DATA_AXIS(chan->address),
  935. &accel, sizeof(accel));
  936. if (ret)
  937. return ret;
  938. *val = sign_extend32(le16_to_cpu(accel), 12);
  939. return IIO_VAL_INT;
  940. case IIO_CHAN_INFO_SCALE:
  941. ret = regmap_read(st->regmap, ADXL345_REG_DATA_FORMAT, &regval);
  942. if (ret)
  943. return ret;
  944. range = FIELD_GET(ADXL345_DATA_FORMAT_RANGE, regval);
  945. *val = adxl345_fullres_range_tbl[range][0];
  946. *val2 = adxl345_fullres_range_tbl[range][1];
  947. return IIO_VAL_INT_PLUS_MICRO;
  948. case IIO_CHAN_INFO_CALIBBIAS:
  949. ret = regmap_read(st->regmap,
  950. ADXL345_REG_OFS_AXIS(chan->address), &regval);
  951. if (ret)
  952. return ret;
  953. /*
  954. * 8-bit resolution at +/- 2g, that is 4x accel data scale
  955. * factor
  956. */
  957. *val = sign_extend32(regval, 7) * 4;
  958. return IIO_VAL_INT;
  959. case IIO_CHAN_INFO_SAMP_FREQ:
  960. ret = regmap_read(st->regmap, ADXL345_REG_BW_RATE, &regval);
  961. if (ret)
  962. return ret;
  963. odr = FIELD_GET(ADXL345_BW_RATE_MSK, regval);
  964. *val = adxl345_odr_tbl[odr][0];
  965. *val2 = adxl345_odr_tbl[odr][1];
  966. return IIO_VAL_INT_PLUS_MICRO;
  967. }
  968. return -EINVAL;
  969. }
  970. static int adxl345_write_raw(struct iio_dev *indio_dev,
  971. struct iio_chan_spec const *chan,
  972. int val, int val2, long mask)
  973. {
  974. struct adxl345_state *st = iio_priv(indio_dev);
  975. enum adxl345_range range;
  976. enum adxl345_odr odr;
  977. int ret;
  978. ret = adxl345_set_measure_en(st, false);
  979. if (ret)
  980. return ret;
  981. switch (mask) {
  982. case IIO_CHAN_INFO_CALIBBIAS:
  983. /*
  984. * 8-bit resolution at +/- 2g, that is 4x accel data scale
  985. * factor
  986. */
  987. ret = regmap_write(st->regmap,
  988. ADXL345_REG_OFS_AXIS(chan->address),
  989. val / 4);
  990. if (ret)
  991. return ret;
  992. break;
  993. case IIO_CHAN_INFO_SAMP_FREQ:
  994. ret = adxl345_find_odr(st, val, val2, &odr);
  995. if (ret)
  996. return ret;
  997. ret = adxl345_set_odr(st, odr);
  998. if (ret)
  999. return ret;
  1000. break;
  1001. case IIO_CHAN_INFO_SCALE:
  1002. ret = adxl345_find_range(st, val, val2, &range);
  1003. if (ret)
  1004. return ret;
  1005. ret = adxl345_set_range(st, range);
  1006. if (ret)
  1007. return ret;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. return adxl345_set_measure_en(st, true);
  1013. }
  1014. static int adxl345_read_mag_config(struct adxl345_state *st,
  1015. enum iio_event_direction dir,
  1016. enum adxl345_activity_type type_act,
  1017. enum adxl345_activity_type type_inact)
  1018. {
  1019. switch (dir) {
  1020. case IIO_EV_DIR_RISING:
  1021. return !!adxl345_is_act_inact_en(st, type_act);
  1022. case IIO_EV_DIR_FALLING:
  1023. return !!adxl345_is_act_inact_en(st, type_inact);
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. }
  1028. static int adxl345_write_mag_config(struct adxl345_state *st,
  1029. enum iio_event_direction dir,
  1030. enum adxl345_activity_type type_act,
  1031. enum adxl345_activity_type type_inact,
  1032. bool state)
  1033. {
  1034. switch (dir) {
  1035. case IIO_EV_DIR_RISING:
  1036. return adxl345_set_act_inact_en(st, type_act, state);
  1037. case IIO_EV_DIR_FALLING:
  1038. return adxl345_set_act_inact_en(st, type_inact, state);
  1039. default:
  1040. return -EINVAL;
  1041. }
  1042. }
  1043. static int adxl345_read_event_config(struct iio_dev *indio_dev,
  1044. const struct iio_chan_spec *chan,
  1045. enum iio_event_type type,
  1046. enum iio_event_direction dir)
  1047. {
  1048. struct adxl345_state *st = iio_priv(indio_dev);
  1049. bool int_en;
  1050. int ret;
  1051. switch (type) {
  1052. case IIO_EV_TYPE_MAG:
  1053. return adxl345_read_mag_config(st, dir,
  1054. ADXL345_ACTIVITY,
  1055. ADXL345_INACTIVITY);
  1056. case IIO_EV_TYPE_MAG_ADAPTIVE:
  1057. return adxl345_read_mag_config(st, dir,
  1058. ADXL345_ACTIVITY_AC,
  1059. ADXL345_INACTIVITY_AC);
  1060. case IIO_EV_TYPE_GESTURE:
  1061. switch (dir) {
  1062. case IIO_EV_DIR_SINGLETAP:
  1063. ret = adxl345_is_tap_en(st, chan->channel2,
  1064. ADXL345_SINGLE_TAP, &int_en);
  1065. if (ret)
  1066. return ret;
  1067. return int_en;
  1068. case IIO_EV_DIR_DOUBLETAP:
  1069. ret = adxl345_is_tap_en(st, chan->channel2,
  1070. ADXL345_DOUBLE_TAP, &int_en);
  1071. if (ret)
  1072. return ret;
  1073. return int_en;
  1074. default:
  1075. return -EINVAL;
  1076. }
  1077. default:
  1078. return -EINVAL;
  1079. }
  1080. }
  1081. static int adxl345_write_event_config(struct iio_dev *indio_dev,
  1082. const struct iio_chan_spec *chan,
  1083. enum iio_event_type type,
  1084. enum iio_event_direction dir,
  1085. bool state)
  1086. {
  1087. struct adxl345_state *st = iio_priv(indio_dev);
  1088. switch (type) {
  1089. case IIO_EV_TYPE_MAG:
  1090. return adxl345_write_mag_config(st, dir,
  1091. ADXL345_ACTIVITY,
  1092. ADXL345_INACTIVITY,
  1093. state);
  1094. case IIO_EV_TYPE_MAG_ADAPTIVE:
  1095. return adxl345_write_mag_config(st, dir,
  1096. ADXL345_ACTIVITY_AC,
  1097. ADXL345_INACTIVITY_AC,
  1098. state);
  1099. case IIO_EV_TYPE_GESTURE:
  1100. switch (dir) {
  1101. case IIO_EV_DIR_SINGLETAP:
  1102. return adxl345_set_singletap_en(st, chan->channel2, state);
  1103. case IIO_EV_DIR_DOUBLETAP:
  1104. return adxl345_set_doubletap_en(st, state);
  1105. default:
  1106. return -EINVAL;
  1107. }
  1108. default:
  1109. return -EINVAL;
  1110. }
  1111. }
  1112. static int adxl345_read_mag_value(struct adxl345_state *st,
  1113. enum iio_event_direction dir,
  1114. enum iio_event_info info,
  1115. enum adxl345_activity_type type_act,
  1116. enum adxl345_activity_type type_inact,
  1117. int *val, int *val2)
  1118. {
  1119. unsigned int threshold;
  1120. int ret;
  1121. switch (info) {
  1122. case IIO_EV_INFO_VALUE:
  1123. switch (dir) {
  1124. case IIO_EV_DIR_RISING:
  1125. ret = regmap_read(st->regmap,
  1126. adxl345_act_thresh_reg[type_act],
  1127. &threshold);
  1128. if (ret)
  1129. return ret;
  1130. *val = 62500 * threshold;
  1131. *val2 = MICRO;
  1132. return IIO_VAL_FRACTIONAL;
  1133. case IIO_EV_DIR_FALLING:
  1134. *val = 62500 * st->inact_threshold;
  1135. *val2 = MICRO;
  1136. return IIO_VAL_FRACTIONAL;
  1137. default:
  1138. return -EINVAL;
  1139. }
  1140. case IIO_EV_INFO_PERIOD:
  1141. *val = st->inact_time_ms;
  1142. *val2 = MILLI;
  1143. return IIO_VAL_FRACTIONAL;
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. }
  1148. static int adxl345_write_mag_value(struct adxl345_state *st,
  1149. enum iio_event_direction dir,
  1150. enum iio_event_info info,
  1151. enum adxl345_activity_type type_act,
  1152. enum adxl345_activity_type type_inact,
  1153. int val, int val2)
  1154. {
  1155. switch (info) {
  1156. case IIO_EV_INFO_VALUE:
  1157. /* Scaling factor 62.5mg/LSB, i.e. ~16g corresponds to 0xff */
  1158. val = DIV_ROUND_CLOSEST(val * MICRO + val2, 62500);
  1159. switch (dir) {
  1160. case IIO_EV_DIR_RISING:
  1161. return regmap_write(st->regmap,
  1162. adxl345_act_thresh_reg[type_act],
  1163. val);
  1164. case IIO_EV_DIR_FALLING:
  1165. return adxl345_set_inact_threshold(st, val);
  1166. default:
  1167. return -EINVAL;
  1168. }
  1169. case IIO_EV_INFO_PERIOD:
  1170. return adxl345_set_inact_time(st, val, val2);
  1171. default:
  1172. return -EINVAL;
  1173. }
  1174. }
  1175. static int adxl345_read_event_value(struct iio_dev *indio_dev,
  1176. const struct iio_chan_spec *chan,
  1177. enum iio_event_type type,
  1178. enum iio_event_direction dir,
  1179. enum iio_event_info info,
  1180. int *val, int *val2)
  1181. {
  1182. struct adxl345_state *st = iio_priv(indio_dev);
  1183. unsigned int tap_threshold;
  1184. int ret;
  1185. switch (type) {
  1186. case IIO_EV_TYPE_MAG:
  1187. return adxl345_read_mag_value(st, dir, info,
  1188. ADXL345_ACTIVITY,
  1189. ADXL345_INACTIVITY,
  1190. val, val2);
  1191. case IIO_EV_TYPE_MAG_ADAPTIVE:
  1192. return adxl345_read_mag_value(st, dir, info,
  1193. ADXL345_ACTIVITY_AC,
  1194. ADXL345_INACTIVITY_AC,
  1195. val, val2);
  1196. case IIO_EV_TYPE_GESTURE:
  1197. switch (info) {
  1198. case IIO_EV_INFO_VALUE:
  1199. /*
  1200. * The scale factor would be 62.5mg/LSB (i.e. 0xFF = 16g) but
  1201. * not applied here. In context of this general purpose sensor,
  1202. * what imports is rather signal intensity than the absolute
  1203. * measured g value.
  1204. */
  1205. ret = regmap_read(st->regmap, ADXL345_REG_THRESH_TAP,
  1206. &tap_threshold);
  1207. if (ret)
  1208. return ret;
  1209. *val = sign_extend32(tap_threshold, 7);
  1210. return IIO_VAL_INT;
  1211. case IIO_EV_INFO_TIMEOUT:
  1212. *val = st->tap_duration_us;
  1213. *val2 = MICRO;
  1214. return IIO_VAL_FRACTIONAL;
  1215. case IIO_EV_INFO_RESET_TIMEOUT:
  1216. *val = st->tap_window_us;
  1217. *val2 = MICRO;
  1218. return IIO_VAL_FRACTIONAL;
  1219. case IIO_EV_INFO_TAP2_MIN_DELAY:
  1220. *val = st->tap_latent_us;
  1221. *val2 = MICRO;
  1222. return IIO_VAL_FRACTIONAL;
  1223. default:
  1224. return -EINVAL;
  1225. }
  1226. default:
  1227. return -EINVAL;
  1228. }
  1229. }
  1230. static int adxl345_write_event_value(struct iio_dev *indio_dev,
  1231. const struct iio_chan_spec *chan,
  1232. enum iio_event_type type,
  1233. enum iio_event_direction dir,
  1234. enum iio_event_info info,
  1235. int val, int val2)
  1236. {
  1237. struct adxl345_state *st = iio_priv(indio_dev);
  1238. int ret;
  1239. ret = adxl345_set_measure_en(st, false);
  1240. if (ret)
  1241. return ret;
  1242. switch (type) {
  1243. case IIO_EV_TYPE_MAG:
  1244. ret = adxl345_write_mag_value(st, dir, info,
  1245. ADXL345_ACTIVITY,
  1246. ADXL345_INACTIVITY,
  1247. val, val2);
  1248. if (ret)
  1249. return ret;
  1250. break;
  1251. case IIO_EV_TYPE_MAG_ADAPTIVE:
  1252. ret = adxl345_write_mag_value(st, dir, info,
  1253. ADXL345_ACTIVITY_AC,
  1254. ADXL345_INACTIVITY_AC,
  1255. val, val2);
  1256. if (ret)
  1257. return ret;
  1258. break;
  1259. case IIO_EV_TYPE_GESTURE:
  1260. switch (info) {
  1261. case IIO_EV_INFO_VALUE:
  1262. ret = regmap_write(st->regmap, ADXL345_REG_THRESH_TAP,
  1263. min(val, 0xFF));
  1264. if (ret)
  1265. return ret;
  1266. break;
  1267. case IIO_EV_INFO_TIMEOUT:
  1268. ret = adxl345_set_tap_duration(st, val, val2);
  1269. if (ret)
  1270. return ret;
  1271. break;
  1272. case IIO_EV_INFO_RESET_TIMEOUT:
  1273. ret = adxl345_set_tap_window(st, val, val2);
  1274. if (ret)
  1275. return ret;
  1276. break;
  1277. case IIO_EV_INFO_TAP2_MIN_DELAY:
  1278. ret = adxl345_set_tap_latent(st, val, val2);
  1279. if (ret)
  1280. return ret;
  1281. break;
  1282. default:
  1283. return -EINVAL;
  1284. }
  1285. break;
  1286. default:
  1287. return -EINVAL;
  1288. }
  1289. return adxl345_set_measure_en(st, true);
  1290. }
  1291. static int adxl345_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  1292. unsigned int writeval, unsigned int *readval)
  1293. {
  1294. struct adxl345_state *st = iio_priv(indio_dev);
  1295. if (readval)
  1296. return regmap_read(st->regmap, reg, readval);
  1297. return regmap_write(st->regmap, reg, writeval);
  1298. }
  1299. static int adxl345_set_watermark(struct iio_dev *indio_dev, unsigned int value)
  1300. {
  1301. struct adxl345_state *st = iio_priv(indio_dev);
  1302. const unsigned int fifo_mask = 0x1F, watermark_mask = 0x02;
  1303. int ret;
  1304. value = min(value, ADXL345_FIFO_SIZE - 1);
  1305. ret = regmap_update_bits(st->regmap, ADXL345_REG_FIFO_CTL, fifo_mask, value);
  1306. if (ret)
  1307. return ret;
  1308. st->watermark = value;
  1309. return regmap_update_bits(st->regmap, ADXL345_REG_INT_ENABLE,
  1310. watermark_mask, ADXL345_INT_WATERMARK);
  1311. }
  1312. static int adxl345_write_raw_get_fmt(struct iio_dev *indio_dev,
  1313. struct iio_chan_spec const *chan,
  1314. long mask)
  1315. {
  1316. switch (mask) {
  1317. case IIO_CHAN_INFO_CALIBBIAS:
  1318. return IIO_VAL_INT;
  1319. case IIO_CHAN_INFO_SCALE:
  1320. return IIO_VAL_INT_PLUS_MICRO;
  1321. case IIO_CHAN_INFO_SAMP_FREQ:
  1322. return IIO_VAL_INT_PLUS_MICRO;
  1323. default:
  1324. return -EINVAL;
  1325. }
  1326. }
  1327. static void adxl345_powerdown(void *ptr)
  1328. {
  1329. struct adxl345_state *st = ptr;
  1330. adxl345_set_measure_en(st, false);
  1331. }
  1332. static int adxl345_set_fifo(struct adxl345_state *st)
  1333. {
  1334. unsigned int intio;
  1335. int ret;
  1336. /* FIFO should only be configured while in standby mode */
  1337. ret = adxl345_set_measure_en(st, false);
  1338. if (ret)
  1339. return ret;
  1340. ret = regmap_read(st->regmap, ADXL345_REG_INT_MAP, &intio);
  1341. if (ret)
  1342. return ret;
  1343. ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL,
  1344. FIELD_PREP(ADXL345_FIFO_CTL_SAMPLES_MSK,
  1345. st->watermark) |
  1346. FIELD_PREP(ADXL345_FIFO_CTL_TRIGGER_MSK, intio) |
  1347. FIELD_PREP(ADXL345_FIFO_CTL_MODE_MSK,
  1348. st->fifo_mode));
  1349. if (ret)
  1350. return ret;
  1351. return adxl345_set_measure_en(st, true);
  1352. }
  1353. /**
  1354. * adxl345_get_samples() - Read number of FIFO entries.
  1355. * @st: The initialized state instance of this driver.
  1356. *
  1357. * The sensor does not support treating any axis individually, or exclude them
  1358. * from measuring.
  1359. *
  1360. * Return: negative error, or value.
  1361. */
  1362. static int adxl345_get_samples(struct adxl345_state *st)
  1363. {
  1364. unsigned int regval = 0;
  1365. int ret;
  1366. ret = regmap_read(st->regmap, ADXL345_REG_FIFO_STATUS, &regval);
  1367. if (ret)
  1368. return ret;
  1369. return FIELD_GET(ADXL345_REG_FIFO_STATUS_MSK, regval);
  1370. }
  1371. /**
  1372. * adxl345_fifo_transfer() - Read samples number of elements.
  1373. * @st: The instance of the state object of this sensor.
  1374. * @samples: The number of lines in the FIFO referred to as fifo_entry.
  1375. *
  1376. * It is recommended that a multiple-byte read of all registers be performed to
  1377. * prevent a change in data between reads of sequential registers. That is to
  1378. * read out the data registers X0, X1, Y0, Y1, Z0, Z1, i.e. 6 bytes at once.
  1379. *
  1380. * Return: 0 or error value.
  1381. */
  1382. static int adxl345_fifo_transfer(struct adxl345_state *st, int samples)
  1383. {
  1384. int i, ret = 0;
  1385. for (i = 0; i < samples; i++) {
  1386. ret = regmap_bulk_read(st->regmap, ADXL345_REG_XYZ_BASE,
  1387. st->fifo_buf + (i * ADXL345_DIRS),
  1388. sizeof(st->fifo_buf[0]) * ADXL345_DIRS);
  1389. if (ret)
  1390. return ret;
  1391. /*
  1392. * To ensure that the FIFO has completely popped, there must be at least 5
  1393. * us between the end of reading the data registers, signified by the
  1394. * transition to register 0x38 from 0x37 or the CS pin going high, and the
  1395. * start of new reads of the FIFO or reading the FIFO_STATUS register. For
  1396. * SPI operation at 1.5 MHz or lower, the register addressing portion of the
  1397. * transmission is sufficient delay to ensure the FIFO has completely
  1398. * popped. It is necessary for SPI operation greater than 1.5 MHz to
  1399. * de-assert the CS pin to ensure a total of 5 us, which is at most 3.4 us
  1400. * at 5 MHz operation.
  1401. */
  1402. if (st->fifo_delay && samples > 1)
  1403. udelay(3);
  1404. }
  1405. return ret;
  1406. }
  1407. /**
  1408. * adxl345_fifo_reset() - Empty the FIFO in error condition.
  1409. * @st: The instance to the state object of the sensor.
  1410. *
  1411. * Read all elements of the FIFO. Reading the interrupt source register
  1412. * resets the sensor.
  1413. */
  1414. static void adxl345_fifo_reset(struct adxl345_state *st)
  1415. {
  1416. int regval;
  1417. int samples;
  1418. adxl345_set_measure_en(st, false);
  1419. samples = adxl345_get_samples(st);
  1420. if (samples > 0)
  1421. adxl345_fifo_transfer(st, samples);
  1422. regmap_read(st->regmap, ADXL345_REG_INT_SOURCE, &regval);
  1423. adxl345_set_measure_en(st, true);
  1424. }
  1425. static int adxl345_buffer_postenable(struct iio_dev *indio_dev)
  1426. {
  1427. struct adxl345_state *st = iio_priv(indio_dev);
  1428. st->fifo_mode = ADXL345_FIFO_STREAM;
  1429. return adxl345_set_fifo(st);
  1430. }
  1431. static int adxl345_buffer_predisable(struct iio_dev *indio_dev)
  1432. {
  1433. struct adxl345_state *st = iio_priv(indio_dev);
  1434. int ret;
  1435. st->fifo_mode = ADXL345_FIFO_BYPASS;
  1436. ret = adxl345_set_fifo(st);
  1437. if (ret)
  1438. return ret;
  1439. return regmap_write(st->regmap, ADXL345_REG_INT_ENABLE, 0x00);
  1440. }
  1441. static const struct iio_buffer_setup_ops adxl345_buffer_ops = {
  1442. .postenable = adxl345_buffer_postenable,
  1443. .predisable = adxl345_buffer_predisable,
  1444. };
  1445. static int adxl345_fifo_push(struct iio_dev *indio_dev,
  1446. int samples)
  1447. {
  1448. struct adxl345_state *st = iio_priv(indio_dev);
  1449. int i, ret;
  1450. if (samples <= 0)
  1451. return -EINVAL;
  1452. ret = adxl345_fifo_transfer(st, samples);
  1453. if (ret)
  1454. return ret;
  1455. for (i = 0; i < ADXL345_DIRS * samples; i += ADXL345_DIRS)
  1456. iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
  1457. return 0;
  1458. }
  1459. static int adxl345_push_event(struct iio_dev *indio_dev, int int_stat,
  1460. enum iio_modifier act_dir,
  1461. enum iio_modifier tap_dir)
  1462. {
  1463. s64 ts = iio_get_time_ns(indio_dev);
  1464. struct adxl345_state *st = iio_priv(indio_dev);
  1465. unsigned int regval;
  1466. int samples;
  1467. int ret = -ENOENT;
  1468. if (FIELD_GET(ADXL345_INT_SINGLE_TAP, int_stat)) {
  1469. ret = iio_push_event(indio_dev,
  1470. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, tap_dir,
  1471. IIO_EV_TYPE_GESTURE,
  1472. IIO_EV_DIR_SINGLETAP),
  1473. ts);
  1474. if (ret)
  1475. return ret;
  1476. }
  1477. if (FIELD_GET(ADXL345_INT_DOUBLE_TAP, int_stat)) {
  1478. ret = iio_push_event(indio_dev,
  1479. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, tap_dir,
  1480. IIO_EV_TYPE_GESTURE,
  1481. IIO_EV_DIR_DOUBLETAP),
  1482. ts);
  1483. if (ret)
  1484. return ret;
  1485. }
  1486. if (FIELD_GET(ADXL345_INT_ACTIVITY, int_stat)) {
  1487. ret = regmap_read(st->regmap, ADXL345_REG_ACT_INACT_CTRL, &regval);
  1488. if (ret)
  1489. return ret;
  1490. if (FIELD_GET(ADXL345_REG_ACT_ACDC, regval)) {
  1491. /* AC coupled */
  1492. ret = iio_push_event(indio_dev,
  1493. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, act_dir,
  1494. IIO_EV_TYPE_MAG_ADAPTIVE,
  1495. IIO_EV_DIR_RISING),
  1496. ts);
  1497. } else {
  1498. /* DC coupled, relying on THRESH */
  1499. ret = iio_push_event(indio_dev,
  1500. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, act_dir,
  1501. IIO_EV_TYPE_MAG,
  1502. IIO_EV_DIR_RISING),
  1503. ts);
  1504. }
  1505. if (ret)
  1506. return ret;
  1507. }
  1508. if (FIELD_GET(ADXL345_INT_INACTIVITY, int_stat)) {
  1509. ret = regmap_read(st->regmap, ADXL345_REG_ACT_INACT_CTRL, &regval);
  1510. if (ret)
  1511. return ret;
  1512. if (FIELD_GET(ADXL345_REG_INACT_ACDC, regval)) {
  1513. /* AC coupled */
  1514. ret = iio_push_event(indio_dev,
  1515. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
  1516. IIO_MOD_X_AND_Y_AND_Z,
  1517. IIO_EV_TYPE_MAG_ADAPTIVE,
  1518. IIO_EV_DIR_FALLING),
  1519. ts);
  1520. } else {
  1521. /* DC coupled, relying on THRESH */
  1522. ret = iio_push_event(indio_dev,
  1523. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
  1524. IIO_MOD_X_AND_Y_AND_Z,
  1525. IIO_EV_TYPE_MAG,
  1526. IIO_EV_DIR_FALLING),
  1527. ts);
  1528. }
  1529. if (ret)
  1530. return ret;
  1531. }
  1532. if (FIELD_GET(ADXL345_INT_FREE_FALL, int_stat)) {
  1533. ret = iio_push_event(indio_dev,
  1534. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
  1535. IIO_MOD_X_AND_Y_AND_Z,
  1536. IIO_EV_TYPE_MAG,
  1537. IIO_EV_DIR_FALLING),
  1538. ts);
  1539. if (ret)
  1540. return ret;
  1541. }
  1542. if (FIELD_GET(ADXL345_INT_WATERMARK, int_stat)) {
  1543. samples = adxl345_get_samples(st);
  1544. if (samples < 0)
  1545. return -EINVAL;
  1546. if (adxl345_fifo_push(indio_dev, samples) < 0)
  1547. return -EINVAL;
  1548. ret = 0;
  1549. }
  1550. return ret;
  1551. }
  1552. /**
  1553. * adxl345_irq_handler() - Handle irqs of the ADXL345.
  1554. * @irq: The irq being handled.
  1555. * @p: The struct iio_device pointer for the device.
  1556. *
  1557. * Return: The interrupt was handled.
  1558. */
  1559. static irqreturn_t adxl345_irq_handler(int irq, void *p)
  1560. {
  1561. struct iio_dev *indio_dev = p;
  1562. struct adxl345_state *st = iio_priv(indio_dev);
  1563. unsigned int regval;
  1564. enum iio_modifier tap_dir = IIO_NO_MOD;
  1565. enum iio_modifier act_dir = IIO_NO_MOD;
  1566. u32 axis_ctrl;
  1567. int int_stat;
  1568. int ret;
  1569. ret = regmap_read(st->regmap, ADXL345_REG_TAP_AXIS, &axis_ctrl);
  1570. if (ret)
  1571. return IRQ_NONE;
  1572. if (FIELD_GET(ADXL345_REG_TAP_AXIS_MSK, axis_ctrl) ||
  1573. FIELD_GET(ADXL345_ACT_XYZ_EN, axis_ctrl)) {
  1574. ret = regmap_read(st->regmap, ADXL345_REG_ACT_TAP_STATUS, &regval);
  1575. if (ret)
  1576. return IRQ_NONE;
  1577. if (FIELD_GET(ADXL345_TAP_Z_EN, regval))
  1578. tap_dir = IIO_MOD_Z;
  1579. else if (FIELD_GET(ADXL345_TAP_Y_EN, regval))
  1580. tap_dir = IIO_MOD_Y;
  1581. else if (FIELD_GET(ADXL345_TAP_X_EN, regval))
  1582. tap_dir = IIO_MOD_X;
  1583. if (FIELD_GET(ADXL345_ACT_Z_EN, regval))
  1584. act_dir = IIO_MOD_Z;
  1585. else if (FIELD_GET(ADXL345_ACT_Y_EN, regval))
  1586. act_dir = IIO_MOD_Y;
  1587. else if (FIELD_GET(ADXL345_ACT_X_EN, regval))
  1588. act_dir = IIO_MOD_X;
  1589. }
  1590. if (regmap_read(st->regmap, ADXL345_REG_INT_SOURCE, &int_stat))
  1591. return IRQ_NONE;
  1592. if (adxl345_push_event(indio_dev, int_stat, act_dir, tap_dir))
  1593. goto err;
  1594. if (FIELD_GET(ADXL345_INT_OVERRUN, int_stat))
  1595. goto err;
  1596. return IRQ_HANDLED;
  1597. err:
  1598. adxl345_fifo_reset(st);
  1599. return IRQ_HANDLED;
  1600. }
  1601. static const struct iio_info adxl345_info = {
  1602. .read_raw = adxl345_read_raw,
  1603. .write_raw = adxl345_write_raw,
  1604. .read_avail = adxl345_read_avail,
  1605. .write_raw_get_fmt = adxl345_write_raw_get_fmt,
  1606. .read_event_config = adxl345_read_event_config,
  1607. .write_event_config = adxl345_write_event_config,
  1608. .read_event_value = adxl345_read_event_value,
  1609. .write_event_value = adxl345_write_event_value,
  1610. .debugfs_reg_access = &adxl345_reg_access,
  1611. .hwfifo_set_watermark = adxl345_set_watermark,
  1612. };
  1613. static int adxl345_get_int_line(struct device *dev, int *irq)
  1614. {
  1615. *irq = fwnode_irq_get_byname(dev_fwnode(dev), "INT1");
  1616. if (*irq > 0)
  1617. return ADXL345_INT1;
  1618. *irq = fwnode_irq_get_byname(dev_fwnode(dev), "INT2");
  1619. if (*irq > 0)
  1620. return ADXL345_INT2;
  1621. return ADXL345_INT_NONE;
  1622. }
  1623. /**
  1624. * adxl345_core_probe() - Probe and setup for the accelerometer.
  1625. * @dev: Driver model representation of the device
  1626. * @regmap: Regmap instance for the device
  1627. * @fifo_delay_default: Using FIFO with SPI needs delay
  1628. * @setup: Setup routine to be executed right before the standard device
  1629. * setup
  1630. *
  1631. * For SPI operation greater than 1.6 MHz, it is necessary to deassert the CS
  1632. * pin to ensure a total delay of 5 us; otherwise, the delay is not sufficient.
  1633. * The total delay necessary for 5 MHz operation is at most 3.4 us. This is not
  1634. * a concern when using I2C mode because the communication rate is low enough
  1635. * to ensure a sufficient delay between FIFO reads.
  1636. * Ref: "Retrieving Data from FIFO", p. 21 of 36, Data Sheet ADXL345 Rev. G
  1637. *
  1638. * Return: 0 on success, negative errno on error
  1639. */
  1640. int adxl345_core_probe(struct device *dev, struct regmap *regmap,
  1641. bool fifo_delay_default,
  1642. int (*setup)(struct device*, struct regmap*))
  1643. {
  1644. struct adxl345_state *st;
  1645. struct iio_dev *indio_dev;
  1646. u32 regval;
  1647. u8 intio = ADXL345_INT1;
  1648. unsigned int data_format_mask = (ADXL345_DATA_FORMAT_RANGE |
  1649. ADXL345_DATA_FORMAT_JUSTIFY |
  1650. ADXL345_DATA_FORMAT_FULL_RES |
  1651. ADXL345_DATA_FORMAT_SELF_TEST);
  1652. unsigned int tap_threshold;
  1653. int irq;
  1654. int ret;
  1655. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1656. if (!indio_dev)
  1657. return -ENOMEM;
  1658. st = iio_priv(indio_dev);
  1659. st->regmap = regmap;
  1660. st->info = device_get_match_data(dev);
  1661. if (!st->info)
  1662. return -ENODEV;
  1663. st->fifo_delay = fifo_delay_default;
  1664. /* Init with reasonable values */
  1665. tap_threshold = 48; /* 48 [0x30] -> ~3g */
  1666. st->tap_duration_us = 16; /* 16 [0x10] -> .010 */
  1667. st->tap_window_us = 64; /* 64 [0x40] -> .080 */
  1668. st->tap_latent_us = 16; /* 16 [0x10] -> .020 */
  1669. indio_dev->name = st->info->name;
  1670. indio_dev->info = &adxl345_info;
  1671. indio_dev->modes = INDIO_DIRECT_MODE;
  1672. indio_dev->channels = adxl345_channels;
  1673. indio_dev->num_channels = ARRAY_SIZE(adxl345_channels);
  1674. indio_dev->available_scan_masks = adxl345_scan_masks;
  1675. /*
  1676. * Using I2C at 100kHz would limit the maximum ODR to 200Hz, operation
  1677. * at an output rate above the recommended maximum may result in
  1678. * undesired behavior.
  1679. */
  1680. ret = adxl345_set_odr(st, ADXL345_ODR_200HZ);
  1681. if (ret)
  1682. return ret;
  1683. ret = adxl345_set_range(st, ADXL345_16G_RANGE);
  1684. if (ret)
  1685. return ret;
  1686. /* Reset interrupts at start up */
  1687. ret = regmap_write(st->regmap, ADXL345_REG_INT_ENABLE, 0x00);
  1688. if (ret)
  1689. return ret;
  1690. if (setup) {
  1691. /* Perform optional initial bus specific configuration */
  1692. ret = setup(dev, st->regmap);
  1693. if (ret)
  1694. return ret;
  1695. /* Enable full-resolution mode */
  1696. ret = regmap_update_bits(st->regmap, ADXL345_REG_DATA_FORMAT,
  1697. data_format_mask,
  1698. ADXL345_DATA_FORMAT_FULL_RES);
  1699. if (ret)
  1700. return dev_err_probe(dev, ret,
  1701. "Failed to set data range\n");
  1702. } else {
  1703. /* Enable full-resolution mode (init all data_format bits) */
  1704. ret = regmap_write(st->regmap, ADXL345_REG_DATA_FORMAT,
  1705. ADXL345_DATA_FORMAT_FULL_RES);
  1706. if (ret)
  1707. return dev_err_probe(dev, ret,
  1708. "Failed to set data range\n");
  1709. }
  1710. ret = regmap_read(st->regmap, ADXL345_REG_DEVID, &regval);
  1711. if (ret)
  1712. return dev_err_probe(dev, ret, "Error reading device ID\n");
  1713. if (regval != ADXL345_DEVID)
  1714. return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n",
  1715. regval, ADXL345_DEVID);
  1716. /* Enable measurement mode */
  1717. ret = adxl345_set_measure_en(st, true);
  1718. if (ret)
  1719. return dev_err_probe(dev, ret, "Failed to enable measurement mode\n");
  1720. ret = devm_add_action_or_reset(dev, adxl345_powerdown, st);
  1721. if (ret)
  1722. return ret;
  1723. intio = adxl345_get_int_line(dev, &irq);
  1724. if (intio != ADXL345_INT_NONE) {
  1725. /*
  1726. * In the INT map register, bits set to 0 route their
  1727. * corresponding interrupts to the INT1 pin, while bits set to 1
  1728. * route them to the INT2 pin. The intio should handle this
  1729. * mapping accordingly.
  1730. */
  1731. ret = regmap_assign_bits(st->regmap, ADXL345_REG_INT_MAP,
  1732. U8_MAX, intio);
  1733. if (ret)
  1734. return ret;
  1735. /*
  1736. * Initialized with sensible default values to streamline
  1737. * sensor operation. These defaults are partly derived from
  1738. * the previous input driver for the ADXL345 and partly
  1739. * based on the recommendations provided in the datasheet.
  1740. */
  1741. ret = regmap_write(st->regmap, ADXL345_REG_ACT_INACT_CTRL, 0);
  1742. if (ret)
  1743. return ret;
  1744. ret = regmap_write(st->regmap, ADXL345_REG_THRESH_ACT, 6);
  1745. if (ret)
  1746. return ret;
  1747. ret = adxl345_set_inact_threshold(st, 4);
  1748. if (ret)
  1749. return ret;
  1750. ret = regmap_write(st->regmap, ADXL345_REG_THRESH_TAP, tap_threshold);
  1751. if (ret)
  1752. return ret;
  1753. /* FIFO_STREAM mode is going to be activated later */
  1754. ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, &adxl345_buffer_ops);
  1755. if (ret)
  1756. return ret;
  1757. ret = devm_request_threaded_irq(dev, irq, NULL,
  1758. &adxl345_irq_handler,
  1759. IRQF_SHARED | IRQF_ONESHOT,
  1760. indio_dev->name, indio_dev);
  1761. if (ret)
  1762. return ret;
  1763. } else {
  1764. ret = regmap_write(st->regmap, ADXL345_REG_FIFO_CTL,
  1765. FIELD_PREP(ADXL345_FIFO_CTL_MODE_MSK,
  1766. ADXL345_FIFO_BYPASS));
  1767. if (ret)
  1768. return ret;
  1769. }
  1770. return devm_iio_device_register(dev, indio_dev);
  1771. }
  1772. EXPORT_SYMBOL_NS_GPL(adxl345_core_probe, "IIO_ADXL345");
  1773. MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
  1774. MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer core driver");
  1775. MODULE_LICENSE("GPL v2");