adxl380.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ADXL380 3-Axis Digital Accelerometer core driver
  4. *
  5. * Copyright 2024 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/property.h>
  12. #include <linux/regmap.h>
  13. #include <linux/units.h>
  14. #include <linux/unaligned.h>
  15. #include <linux/iio/buffer.h>
  16. #include <linux/iio/events.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/kfifo_buf.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/regulator/consumer.h>
  21. #include "adxl380.h"
  22. #define ADXL380_ID_VAL 380
  23. #define ADXL382_ID_VAL 382
  24. #define ADXL380_DEVID_AD_REG 0x00
  25. #define ADLX380_PART_ID_REG 0x02
  26. #define ADXL380_X_DATA_H_REG 0x15
  27. #define ADXL380_Y_DATA_H_REG 0x17
  28. #define ADXL380_Z_DATA_H_REG 0x19
  29. #define ADXL380_T_DATA_H_REG 0x1B
  30. #define ADXL380_MISC_0_REG 0x20
  31. #define ADXL380_XL382_MSK BIT(7)
  32. #define ADXL380_MISC_1_REG 0x21
  33. #define ADXL380_X_DSM_OFFSET_REG 0x4D
  34. #define ADXL380_ACT_INACT_CTL_REG 0x37
  35. #define ADXL380_INACT_EN_MSK BIT(2)
  36. #define ADXL380_ACT_EN_MSK BIT(0)
  37. #define ADXL380_SNSR_AXIS_EN_REG 0x38
  38. #define ADXL380_ACT_INACT_AXIS_EN_MSK GENMASK(2, 0)
  39. #define ADXL380_THRESH_ACT_H_REG 0x39
  40. #define ADXL380_TIME_ACT_H_REG 0x3B
  41. #define ADXL380_THRESH_INACT_H_REG 0x3E
  42. #define ADXL380_TIME_INACT_H_REG 0x40
  43. #define ADXL380_THRESH_MAX GENMASK(12, 0)
  44. #define ADXL380_TIME_MAX GENMASK(24, 0)
  45. #define ADXL380_FIFO_CONFIG_0_REG 0x30
  46. #define ADXL380_FIFO_SAMPLES_8_MSK BIT(0)
  47. #define ADXL380_FIFO_MODE_MSK GENMASK(5, 4)
  48. #define ADXL380_FIFO_DISABLED 0
  49. #define ADXL380_FIFO_NORMAL 1
  50. #define ADXL380_FIFO_STREAMED 2
  51. #define ADXL380_FIFO_TRIGGERED 3
  52. #define ADXL380_FIFO_CONFIG_1_REG 0x31
  53. #define ADXL380_FIFO_STATUS_0_REG 0x1E
  54. #define ADXL380_TAP_THRESH_REG 0x43
  55. #define ADXL380_TAP_DUR_REG 0x44
  56. #define ADXL380_TAP_LATENT_REG 0x45
  57. #define ADXL380_TAP_WINDOW_REG 0x46
  58. #define ADXL380_TAP_TIME_MAX GENMASK(7, 0)
  59. #define ADXL380_TAP_CFG_REG 0x47
  60. #define ADXL380_TAP_AXIS_MSK GENMASK(1, 0)
  61. #define ADXL380_TRIG_CFG_REG 0x49
  62. #define ADXL380_TRIG_CFG_DEC_2X_MSK BIT(7)
  63. #define ADXL380_TRIG_CFG_SINC_RATE_MSK BIT(6)
  64. #define ADXL380_FILTER_REG 0x50
  65. #define ADXL380_FILTER_EQ_FILT_MSK BIT(6)
  66. #define ADXL380_FILTER_LPF_MODE_MSK GENMASK(5, 4)
  67. #define ADXL380_FILTER_HPF_PATH_MSK BIT(3)
  68. #define ADXL380_FILTER_HPF_CORNER_MSK GENMASK(2, 0)
  69. #define ADXL380_OP_MODE_REG 0x26
  70. #define ADXL380_OP_MODE_RANGE_MSK GENMASK(7, 6)
  71. #define ADXL380_OP_MODE_MSK GENMASK(3, 0)
  72. #define ADXL380_OP_MODE_STANDBY 0
  73. #define ADXL380_OP_MODE_HEART_SOUND 1
  74. #define ADXL380_OP_MODE_ULP 2
  75. #define ADXL380_OP_MODE_VLP 3
  76. #define ADXL380_OP_MODE_LP 4
  77. #define ADXL380_OP_MODE_LP_ULP 6
  78. #define ADXL380_OP_MODE_LP_VLP 7
  79. #define ADXL380_OP_MODE_RBW 8
  80. #define ADXL380_OP_MODE_RBW_ULP 10
  81. #define ADXL380_OP_MODE_RBW_VLP 11
  82. #define ADXL380_OP_MODE_HP 12
  83. #define ADXL380_OP_MODE_HP_ULP 14
  84. #define ADXL380_OP_MODE_HP_VLP 15
  85. #define ADXL380_OP_MODE_4G_RANGE 0
  86. #define ADXL382_OP_MODE_15G_RANGE 0
  87. #define ADXL380_OP_MODE_8G_RANGE 1
  88. #define ADXL382_OP_MODE_30G_RANGE 1
  89. #define ADXL380_OP_MODE_16G_RANGE 2
  90. #define ADXL382_OP_MODE_60G_RANGE 2
  91. #define ADXL380_DIG_EN_REG 0x27
  92. #define ADXL380_CHAN_EN_MSK(chan) BIT(4 + (chan))
  93. #define ADXL380_FIFO_EN_MSK BIT(3)
  94. #define ADXL380_INT0_MAP0_REG 0x2B
  95. #define ADXL380_INT1_MAP0_REG 0x2D
  96. #define ADXL380_INT_MAP0_INACT_INT0_MSK BIT(6)
  97. #define ADXL380_INT_MAP0_ACT_INT0_MSK BIT(5)
  98. #define ADXL380_INT_MAP0_FIFO_WM_INT0_MSK BIT(3)
  99. #define ADXL380_INT0_MAP1_REG 0x2C
  100. #define ADXL380_INT1_MAP1_REG 0x2E
  101. #define ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK BIT(1)
  102. #define ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK BIT(0)
  103. #define ADXL380_INT0_REG 0x5D
  104. #define ADXL380_INT0_POL_MSK BIT(7)
  105. #define ADXL380_RESET_REG 0x2A
  106. #define ADXL380_FIFO_DATA 0x1D
  107. #define ADXL380_DEVID_AD_VAL 0xAD
  108. #define ADXL380_RESET_CODE 0x52
  109. #define ADXL380_STATUS_0_REG 0x11
  110. #define ADXL380_STATUS_0_FIFO_FULL_MSK BIT(1)
  111. #define ADXL380_STATUS_0_FIFO_WM_MSK BIT(3)
  112. #define ADXL380_STATUS_1_INACT_MSK BIT(6)
  113. #define ADXL380_STATUS_1_ACT_MSK BIT(5)
  114. #define ADXL380_STATUS_1_DOUBLE_TAP_MSK BIT(1)
  115. #define ADXL380_STATUS_1_SINGLE_TAP_MSK BIT(0)
  116. #define ADXL380_FIFO_SAMPLES 315UL
  117. enum adxl380_channels {
  118. ADXL380_ACCEL_X,
  119. ADXL380_ACCEL_Y,
  120. ADXL380_ACCEL_Z,
  121. ADXL380_TEMP,
  122. ADXL380_CH_NUM
  123. };
  124. enum adxl380_axis {
  125. ADXL380_X_AXIS,
  126. ADXL380_Y_AXIS,
  127. ADXL380_Z_AXIS,
  128. };
  129. enum adxl380_activity_type {
  130. ADXL380_ACTIVITY,
  131. ADXL380_INACTIVITY,
  132. };
  133. enum adxl380_tap_type {
  134. ADXL380_SINGLE_TAP,
  135. ADXL380_DOUBLE_TAP,
  136. };
  137. enum adxl380_tap_time_type {
  138. ADXL380_TAP_TIME_LATENT,
  139. ADXL380_TAP_TIME_WINDOW,
  140. };
  141. static const int adxl380_range_scale_factor_tbl[] = { 1, 2, 4 };
  142. const struct adxl380_chip_info adxl380_chip_info = {
  143. .name = "adxl380",
  144. .chip_id = ADXL380_ID_VAL,
  145. .scale_tbl = {
  146. [ADXL380_OP_MODE_4G_RANGE] = { 0, 1307226 },
  147. [ADXL380_OP_MODE_8G_RANGE] = { 0, 2615434 },
  148. [ADXL380_OP_MODE_16G_RANGE] = { 0, 5229886 },
  149. },
  150. .samp_freq_tbl = { 8000, 16000, 32000 },
  151. /*
  152. * The datasheet defines an intercept of 470 LSB at 25 degC
  153. * and a sensitivity of 10.2 LSB/C.
  154. */
  155. .temp_offset = 25 * 102 / 10 - 470,
  156. };
  157. EXPORT_SYMBOL_NS_GPL(adxl380_chip_info, IIO_ADXL380);
  158. const struct adxl380_chip_info adxl382_chip_info = {
  159. .name = "adxl382",
  160. .chip_id = ADXL382_ID_VAL,
  161. .scale_tbl = {
  162. [ADXL382_OP_MODE_15G_RANGE] = { 0, 4903325 },
  163. [ADXL382_OP_MODE_30G_RANGE] = { 0, 9806650 },
  164. [ADXL382_OP_MODE_60G_RANGE] = { 0, 19613300 },
  165. },
  166. .samp_freq_tbl = { 16000, 32000, 64000 },
  167. /*
  168. * The datasheet defines an intercept of 570 LSB at 25 degC
  169. * and a sensitivity of 10.2 LSB/C.
  170. */
  171. .temp_offset = 25 * 102 / 10 - 570,
  172. };
  173. EXPORT_SYMBOL_NS_GPL(adxl382_chip_info, IIO_ADXL380);
  174. static const unsigned int adxl380_th_reg_high_addr[2] = {
  175. [ADXL380_ACTIVITY] = ADXL380_THRESH_ACT_H_REG,
  176. [ADXL380_INACTIVITY] = ADXL380_THRESH_INACT_H_REG,
  177. };
  178. static const unsigned int adxl380_time_reg_high_addr[2] = {
  179. [ADXL380_ACTIVITY] = ADXL380_TIME_ACT_H_REG,
  180. [ADXL380_INACTIVITY] = ADXL380_TIME_INACT_H_REG,
  181. };
  182. static const unsigned int adxl380_tap_time_reg[2] = {
  183. [ADXL380_TAP_TIME_LATENT] = ADXL380_TAP_LATENT_REG,
  184. [ADXL380_TAP_TIME_WINDOW] = ADXL380_TAP_WINDOW_REG,
  185. };
  186. struct adxl380_state {
  187. struct regmap *regmap;
  188. struct device *dev;
  189. const struct adxl380_chip_info *chip_info;
  190. /*
  191. * Synchronize access to members of driver state, and ensure atomicity
  192. * of consecutive regmap operations.
  193. */
  194. struct mutex lock;
  195. enum adxl380_axis tap_axis_en;
  196. u8 range;
  197. u8 odr;
  198. u8 fifo_set_size;
  199. u8 transf_buf[3];
  200. u16 watermark;
  201. u32 act_time_ms;
  202. u32 act_threshold;
  203. u32 inact_time_ms;
  204. u32 inact_threshold;
  205. u32 tap_latent_us;
  206. u32 tap_window_us;
  207. u32 tap_duration_us;
  208. u32 tap_threshold;
  209. int irq;
  210. int int_map[2];
  211. int lpf_tbl[4];
  212. int hpf_tbl[7][2];
  213. __be16 fifo_buf[ADXL380_FIFO_SAMPLES] __aligned(IIO_DMA_MINALIGN);
  214. };
  215. bool adxl380_readable_noinc_reg(struct device *dev, unsigned int reg)
  216. {
  217. return reg == ADXL380_FIFO_DATA;
  218. }
  219. EXPORT_SYMBOL_NS_GPL(adxl380_readable_noinc_reg, IIO_ADXL380);
  220. static int adxl380_set_measure_en(struct adxl380_state *st, bool en)
  221. {
  222. int ret;
  223. unsigned int act_inact_ctl;
  224. u8 op_mode = ADXL380_OP_MODE_STANDBY;
  225. if (en) {
  226. ret = regmap_read(st->regmap, ADXL380_ACT_INACT_CTL_REG, &act_inact_ctl);
  227. if (ret)
  228. return ret;
  229. /* Activity/ Inactivity detection available only in VLP/ULP mode */
  230. if (FIELD_GET(ADXL380_ACT_EN_MSK, act_inact_ctl) ||
  231. FIELD_GET(ADXL380_INACT_EN_MSK, act_inact_ctl))
  232. op_mode = ADXL380_OP_MODE_VLP;
  233. else
  234. op_mode = ADXL380_OP_MODE_HP;
  235. }
  236. return regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG,
  237. ADXL380_OP_MODE_MSK,
  238. FIELD_PREP(ADXL380_OP_MODE_MSK, op_mode));
  239. }
  240. static void adxl380_scale_act_inact_thresholds(struct adxl380_state *st,
  241. u8 old_range,
  242. u8 new_range)
  243. {
  244. st->act_threshold = mult_frac(st->act_threshold,
  245. adxl380_range_scale_factor_tbl[old_range],
  246. adxl380_range_scale_factor_tbl[new_range]);
  247. st->inact_threshold = mult_frac(st->inact_threshold,
  248. adxl380_range_scale_factor_tbl[old_range],
  249. adxl380_range_scale_factor_tbl[new_range]);
  250. }
  251. static int adxl380_write_act_inact_threshold(struct adxl380_state *st,
  252. enum adxl380_activity_type act,
  253. unsigned int th)
  254. {
  255. int ret;
  256. u8 reg = adxl380_th_reg_high_addr[act];
  257. if (th > ADXL380_THRESH_MAX)
  258. return -EINVAL;
  259. ret = regmap_write(st->regmap, reg + 1, th & GENMASK(7, 0));
  260. if (ret)
  261. return ret;
  262. ret = regmap_update_bits(st->regmap, reg, GENMASK(2, 0), th >> 8);
  263. if (ret)
  264. return ret;
  265. if (act == ADXL380_ACTIVITY)
  266. st->act_threshold = th;
  267. else
  268. st->inact_threshold = th;
  269. return 0;
  270. }
  271. static int adxl380_set_act_inact_threshold(struct iio_dev *indio_dev,
  272. enum adxl380_activity_type act,
  273. u16 th)
  274. {
  275. struct adxl380_state *st = iio_priv(indio_dev);
  276. int ret;
  277. guard(mutex)(&st->lock);
  278. ret = adxl380_set_measure_en(st, false);
  279. if (ret)
  280. return ret;
  281. ret = adxl380_write_act_inact_threshold(st, act, th);
  282. if (ret)
  283. return ret;
  284. return adxl380_set_measure_en(st, true);
  285. }
  286. static int adxl380_set_tap_threshold_value(struct iio_dev *indio_dev, u8 th)
  287. {
  288. int ret;
  289. struct adxl380_state *st = iio_priv(indio_dev);
  290. guard(mutex)(&st->lock);
  291. ret = adxl380_set_measure_en(st, false);
  292. if (ret)
  293. return ret;
  294. ret = regmap_write(st->regmap, ADXL380_TAP_THRESH_REG, th);
  295. if (ret)
  296. return ret;
  297. st->tap_threshold = th;
  298. return adxl380_set_measure_en(st, true);
  299. }
  300. static int _adxl380_write_tap_time_us(struct adxl380_state *st,
  301. enum adxl380_tap_time_type tap_time_type,
  302. u32 us)
  303. {
  304. u8 reg = adxl380_tap_time_reg[tap_time_type];
  305. unsigned int reg_val;
  306. int ret;
  307. /* scale factor for tap window is 1250us / LSB */
  308. reg_val = DIV_ROUND_CLOSEST(us, 1250);
  309. if (reg_val > ADXL380_TAP_TIME_MAX)
  310. reg_val = ADXL380_TAP_TIME_MAX;
  311. ret = regmap_write(st->regmap, reg, reg_val);
  312. if (ret)
  313. return ret;
  314. if (tap_time_type == ADXL380_TAP_TIME_WINDOW)
  315. st->tap_window_us = us;
  316. else
  317. st->tap_latent_us = us;
  318. return 0;
  319. }
  320. static int adxl380_write_tap_time_us(struct adxl380_state *st,
  321. enum adxl380_tap_time_type tap_time_type, u32 us)
  322. {
  323. int ret;
  324. guard(mutex)(&st->lock);
  325. ret = adxl380_set_measure_en(st, false);
  326. if (ret)
  327. return ret;
  328. ret = _adxl380_write_tap_time_us(st, tap_time_type, us);
  329. if (ret)
  330. return ret;
  331. return adxl380_set_measure_en(st, true);
  332. }
  333. static int adxl380_write_tap_dur_us(struct iio_dev *indio_dev, u32 us)
  334. {
  335. int ret;
  336. unsigned int reg_val;
  337. struct adxl380_state *st = iio_priv(indio_dev);
  338. /* 625us per code is the scale factor of TAP_DUR register */
  339. reg_val = DIV_ROUND_CLOSEST(us, 625);
  340. ret = adxl380_set_measure_en(st, false);
  341. if (ret)
  342. return ret;
  343. ret = regmap_write(st->regmap, ADXL380_TAP_DUR_REG, reg_val);
  344. if (ret)
  345. return ret;
  346. return adxl380_set_measure_en(st, true);
  347. }
  348. static int adxl380_read_chn(struct adxl380_state *st, u8 addr)
  349. {
  350. int ret;
  351. guard(mutex)(&st->lock);
  352. ret = regmap_bulk_read(st->regmap, addr, &st->transf_buf, 2);
  353. if (ret)
  354. return ret;
  355. return get_unaligned_be16(st->transf_buf);
  356. }
  357. static int adxl380_get_odr(struct adxl380_state *st, int *odr)
  358. {
  359. int ret;
  360. unsigned int trig_cfg, odr_idx;
  361. ret = regmap_read(st->regmap, ADXL380_TRIG_CFG_REG, &trig_cfg);
  362. if (ret)
  363. return ret;
  364. odr_idx = (FIELD_GET(ADXL380_TRIG_CFG_SINC_RATE_MSK, trig_cfg) << 1) |
  365. (FIELD_GET(ADXL380_TRIG_CFG_DEC_2X_MSK, trig_cfg) & 1);
  366. *odr = st->chip_info->samp_freq_tbl[odr_idx];
  367. return 0;
  368. }
  369. static const int adxl380_lpf_div[] = {
  370. 1, 4, 8, 16,
  371. };
  372. static int adxl380_fill_lpf_tbl(struct adxl380_state *st)
  373. {
  374. int ret, i;
  375. int odr;
  376. ret = adxl380_get_odr(st, &odr);
  377. if (ret)
  378. return ret;
  379. for (i = 0; i < ARRAY_SIZE(st->lpf_tbl); i++)
  380. st->lpf_tbl[i] = DIV_ROUND_CLOSEST(odr, adxl380_lpf_div[i]);
  381. return 0;
  382. }
  383. static const int adxl380_hpf_mul[] = {
  384. 0, 247000, 62084, 15545, 3862, 954, 238,
  385. };
  386. static int adxl380_fill_hpf_tbl(struct adxl380_state *st)
  387. {
  388. int i, ret, odr_hz;
  389. u32 multiplier;
  390. u64 div, rem, odr;
  391. ret = adxl380_get_odr(st, &odr_hz);
  392. if (ret)
  393. return ret;
  394. for (i = 0; i < ARRAY_SIZE(adxl380_hpf_mul); i++) {
  395. odr = mul_u64_u32_shr(odr_hz, MEGA, 0);
  396. multiplier = adxl380_hpf_mul[i];
  397. div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0),
  398. TERA * 100, &rem);
  399. st->hpf_tbl[i][0] = div;
  400. st->hpf_tbl[i][1] = div_u64(rem, MEGA * 100);
  401. }
  402. return 0;
  403. }
  404. static int adxl380_set_odr(struct adxl380_state *st, u8 odr)
  405. {
  406. int ret;
  407. guard(mutex)(&st->lock);
  408. ret = adxl380_set_measure_en(st, false);
  409. if (ret)
  410. return ret;
  411. ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG,
  412. ADXL380_TRIG_CFG_DEC_2X_MSK,
  413. FIELD_PREP(ADXL380_TRIG_CFG_DEC_2X_MSK, odr & 1));
  414. if (ret)
  415. return ret;
  416. ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG,
  417. ADXL380_TRIG_CFG_SINC_RATE_MSK,
  418. FIELD_PREP(ADXL380_TRIG_CFG_SINC_RATE_MSK, odr >> 1));
  419. if (ret)
  420. return ret;
  421. ret = adxl380_set_measure_en(st, true);
  422. if (ret)
  423. return ret;
  424. ret = adxl380_fill_lpf_tbl(st);
  425. if (ret)
  426. return ret;
  427. return adxl380_fill_hpf_tbl(st);
  428. }
  429. static int adxl380_find_match_1d_tbl(const int *array, unsigned int size,
  430. int val)
  431. {
  432. int i;
  433. for (i = 0; i < size; i++) {
  434. if (val == array[i])
  435. return i;
  436. }
  437. return size - 1;
  438. }
  439. static int adxl380_find_match_2d_tbl(const int (*freq_tbl)[2], int n, int val, int val2)
  440. {
  441. int i;
  442. for (i = 0; i < n; i++) {
  443. if (freq_tbl[i][0] == val && freq_tbl[i][1] == val2)
  444. return i;
  445. }
  446. return -EINVAL;
  447. }
  448. static int adxl380_get_lpf(struct adxl380_state *st, int *lpf)
  449. {
  450. int ret;
  451. unsigned int trig_cfg, lpf_idx;
  452. guard(mutex)(&st->lock);
  453. ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg);
  454. if (ret)
  455. return ret;
  456. lpf_idx = FIELD_GET(ADXL380_FILTER_LPF_MODE_MSK, trig_cfg);
  457. *lpf = st->lpf_tbl[lpf_idx];
  458. return 0;
  459. }
  460. static int adxl380_set_lpf(struct adxl380_state *st, u8 lpf)
  461. {
  462. int ret;
  463. u8 eq_bypass = 0;
  464. guard(mutex)(&st->lock);
  465. ret = adxl380_set_measure_en(st, false);
  466. if (ret)
  467. return ret;
  468. if (lpf)
  469. eq_bypass = 1;
  470. ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
  471. ADXL380_FILTER_EQ_FILT_MSK,
  472. FIELD_PREP(ADXL380_FILTER_EQ_FILT_MSK, eq_bypass));
  473. if (ret)
  474. return ret;
  475. ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
  476. ADXL380_FILTER_LPF_MODE_MSK,
  477. FIELD_PREP(ADXL380_FILTER_LPF_MODE_MSK, lpf));
  478. if (ret)
  479. return ret;
  480. return adxl380_set_measure_en(st, true);
  481. }
  482. static int adxl380_get_hpf(struct adxl380_state *st, int *hpf_int, int *hpf_frac)
  483. {
  484. int ret;
  485. unsigned int trig_cfg, hpf_idx;
  486. guard(mutex)(&st->lock);
  487. ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg);
  488. if (ret)
  489. return ret;
  490. hpf_idx = FIELD_GET(ADXL380_FILTER_HPF_CORNER_MSK, trig_cfg);
  491. *hpf_int = st->hpf_tbl[hpf_idx][0];
  492. *hpf_frac = st->hpf_tbl[hpf_idx][1];
  493. return 0;
  494. }
  495. static int adxl380_set_hpf(struct adxl380_state *st, u8 hpf)
  496. {
  497. int ret;
  498. u8 hpf_path = 0;
  499. guard(mutex)(&st->lock);
  500. ret = adxl380_set_measure_en(st, false);
  501. if (ret)
  502. return ret;
  503. if (hpf)
  504. hpf_path = 1;
  505. ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
  506. ADXL380_FILTER_HPF_PATH_MSK,
  507. FIELD_PREP(ADXL380_FILTER_HPF_PATH_MSK, hpf_path));
  508. if (ret)
  509. return ret;
  510. ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
  511. ADXL380_FILTER_HPF_CORNER_MSK,
  512. FIELD_PREP(ADXL380_FILTER_HPF_CORNER_MSK, hpf));
  513. if (ret)
  514. return ret;
  515. return adxl380_set_measure_en(st, true);
  516. }
  517. static int _adxl380_set_act_inact_time_ms(struct adxl380_state *st,
  518. enum adxl380_activity_type act,
  519. u32 ms)
  520. {
  521. u8 reg = adxl380_time_reg_high_addr[act];
  522. unsigned int reg_val;
  523. int ret;
  524. /* 500us per code is the scale factor of TIME_ACT / TIME_INACT registers */
  525. reg_val = min(DIV_ROUND_CLOSEST(ms * 1000, 500), ADXL380_TIME_MAX);
  526. put_unaligned_be24(reg_val, &st->transf_buf[0]);
  527. ret = regmap_bulk_write(st->regmap, reg, st->transf_buf, sizeof(st->transf_buf));
  528. if (ret)
  529. return ret;
  530. if (act == ADXL380_ACTIVITY)
  531. st->act_time_ms = ms;
  532. else
  533. st->inact_time_ms = ms;
  534. return 0;
  535. }
  536. static int adxl380_set_act_inact_time_ms(struct adxl380_state *st,
  537. enum adxl380_activity_type act,
  538. u32 ms)
  539. {
  540. int ret;
  541. guard(mutex)(&st->lock);
  542. ret = adxl380_set_measure_en(st, false);
  543. if (ret)
  544. return ret;
  545. ret = _adxl380_set_act_inact_time_ms(st, act, ms);
  546. if (ret)
  547. return ret;
  548. return adxl380_set_measure_en(st, true);
  549. }
  550. static int adxl380_set_range(struct adxl380_state *st, u8 range)
  551. {
  552. int ret;
  553. guard(mutex)(&st->lock);
  554. ret = adxl380_set_measure_en(st, false);
  555. if (ret)
  556. return ret;
  557. ret = regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG,
  558. ADXL380_OP_MODE_RANGE_MSK,
  559. FIELD_PREP(ADXL380_OP_MODE_RANGE_MSK, range));
  560. if (ret)
  561. return ret;
  562. adxl380_scale_act_inact_thresholds(st, st->range, range);
  563. /* Activity thresholds depend on range */
  564. ret = adxl380_write_act_inact_threshold(st, ADXL380_ACTIVITY,
  565. st->act_threshold);
  566. if (ret)
  567. return ret;
  568. ret = adxl380_write_act_inact_threshold(st, ADXL380_INACTIVITY,
  569. st->inact_threshold);
  570. if (ret)
  571. return ret;
  572. st->range = range;
  573. return adxl380_set_measure_en(st, true);
  574. }
  575. static int adxl380_write_act_inact_en(struct adxl380_state *st,
  576. enum adxl380_activity_type type,
  577. bool en)
  578. {
  579. if (type == ADXL380_ACTIVITY)
  580. return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG,
  581. ADXL380_ACT_EN_MSK,
  582. FIELD_PREP(ADXL380_ACT_EN_MSK, en));
  583. return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG,
  584. ADXL380_INACT_EN_MSK,
  585. FIELD_PREP(ADXL380_INACT_EN_MSK, en));
  586. }
  587. static int adxl380_read_act_inact_int(struct adxl380_state *st,
  588. enum adxl380_activity_type type,
  589. bool *en)
  590. {
  591. int ret;
  592. unsigned int reg_val;
  593. guard(mutex)(&st->lock);
  594. ret = regmap_read(st->regmap, st->int_map[0], &reg_val);
  595. if (ret)
  596. return ret;
  597. if (type == ADXL380_ACTIVITY)
  598. *en = FIELD_GET(ADXL380_INT_MAP0_ACT_INT0_MSK, reg_val);
  599. else
  600. *en = FIELD_GET(ADXL380_INT_MAP0_INACT_INT0_MSK, reg_val);
  601. return 0;
  602. }
  603. static int adxl380_write_act_inact_int(struct adxl380_state *st,
  604. enum adxl380_activity_type act,
  605. bool en)
  606. {
  607. if (act == ADXL380_ACTIVITY)
  608. return regmap_update_bits(st->regmap, st->int_map[0],
  609. ADXL380_INT_MAP0_ACT_INT0_MSK,
  610. FIELD_PREP(ADXL380_INT_MAP0_ACT_INT0_MSK, en));
  611. return regmap_update_bits(st->regmap, st->int_map[0],
  612. ADXL380_INT_MAP0_INACT_INT0_MSK,
  613. FIELD_PREP(ADXL380_INT_MAP0_INACT_INT0_MSK, en));
  614. }
  615. static int adxl380_act_inact_config(struct adxl380_state *st,
  616. enum adxl380_activity_type type,
  617. bool en)
  618. {
  619. int ret;
  620. guard(mutex)(&st->lock);
  621. ret = adxl380_set_measure_en(st, false);
  622. if (ret)
  623. return ret;
  624. ret = adxl380_write_act_inact_en(st, type, en);
  625. if (ret)
  626. return ret;
  627. ret = adxl380_write_act_inact_int(st, type, en);
  628. if (ret)
  629. return ret;
  630. return adxl380_set_measure_en(st, true);
  631. }
  632. static int adxl380_write_tap_axis(struct adxl380_state *st,
  633. enum adxl380_axis axis)
  634. {
  635. int ret;
  636. ret = regmap_update_bits(st->regmap, ADXL380_TAP_CFG_REG,
  637. ADXL380_TAP_AXIS_MSK,
  638. FIELD_PREP(ADXL380_TAP_AXIS_MSK, axis));
  639. if (ret)
  640. return ret;
  641. st->tap_axis_en = axis;
  642. return 0;
  643. }
  644. static int adxl380_read_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool *en)
  645. {
  646. int ret;
  647. unsigned int reg_val;
  648. ret = regmap_read(st->regmap, st->int_map[1], &reg_val);
  649. if (ret)
  650. return ret;
  651. if (type == ADXL380_SINGLE_TAP)
  652. *en = FIELD_GET(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, reg_val);
  653. else
  654. *en = FIELD_GET(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, reg_val);
  655. return 0;
  656. }
  657. static int adxl380_write_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool en)
  658. {
  659. if (type == ADXL380_SINGLE_TAP)
  660. return regmap_update_bits(st->regmap, st->int_map[1],
  661. ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK,
  662. FIELD_PREP(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, en));
  663. return regmap_update_bits(st->regmap, st->int_map[1],
  664. ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK,
  665. FIELD_PREP(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, en));
  666. }
  667. static int adxl380_tap_config(struct adxl380_state *st,
  668. enum adxl380_axis axis,
  669. enum adxl380_tap_type type,
  670. bool en)
  671. {
  672. int ret;
  673. guard(mutex)(&st->lock);
  674. ret = adxl380_set_measure_en(st, false);
  675. if (ret)
  676. return ret;
  677. ret = adxl380_write_tap_axis(st, axis);
  678. if (ret)
  679. return ret;
  680. ret = adxl380_write_tap_int(st, type, en);
  681. if (ret)
  682. return ret;
  683. return adxl380_set_measure_en(st, true);
  684. }
  685. static int adxl380_set_fifo_samples(struct adxl380_state *st)
  686. {
  687. int ret;
  688. u16 fifo_samples = st->watermark * st->fifo_set_size;
  689. ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
  690. ADXL380_FIFO_SAMPLES_8_MSK,
  691. FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
  692. (fifo_samples & BIT(8))));
  693. if (ret)
  694. return ret;
  695. return regmap_write(st->regmap, ADXL380_FIFO_CONFIG_1_REG,
  696. fifo_samples & 0xFF);
  697. }
  698. static int adxl380_get_status(struct adxl380_state *st, u8 *status0, u8 *status1)
  699. {
  700. int ret;
  701. /* STATUS0, STATUS1 are adjacent regs */
  702. ret = regmap_bulk_read(st->regmap, ADXL380_STATUS_0_REG,
  703. &st->transf_buf, 2);
  704. if (ret)
  705. return ret;
  706. *status0 = st->transf_buf[0];
  707. *status1 = st->transf_buf[1];
  708. return 0;
  709. }
  710. static int adxl380_get_fifo_entries(struct adxl380_state *st, u16 *fifo_entries)
  711. {
  712. int ret;
  713. ret = regmap_bulk_read(st->regmap, ADXL380_FIFO_STATUS_0_REG,
  714. &st->transf_buf, 2);
  715. if (ret)
  716. return ret;
  717. *fifo_entries = st->transf_buf[0] | ((BIT(0) & st->transf_buf[1]) << 8);
  718. return 0;
  719. }
  720. static void adxl380_push_event(struct iio_dev *indio_dev, s64 timestamp,
  721. u8 status1)
  722. {
  723. if (FIELD_GET(ADXL380_STATUS_1_ACT_MSK, status1))
  724. iio_push_event(indio_dev,
  725. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  726. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  727. timestamp);
  728. if (FIELD_GET(ADXL380_STATUS_1_INACT_MSK, status1))
  729. iio_push_event(indio_dev,
  730. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  731. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  732. timestamp);
  733. if (FIELD_GET(ADXL380_STATUS_1_SINGLE_TAP_MSK, status1))
  734. iio_push_event(indio_dev,
  735. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  736. IIO_EV_TYPE_GESTURE, IIO_EV_DIR_SINGLETAP),
  737. timestamp);
  738. if (FIELD_GET(ADXL380_STATUS_1_DOUBLE_TAP_MSK, status1))
  739. iio_push_event(indio_dev,
  740. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  741. IIO_EV_TYPE_GESTURE, IIO_EV_DIR_DOUBLETAP),
  742. timestamp);
  743. }
  744. static irqreturn_t adxl380_irq_handler(int irq, void *p)
  745. {
  746. struct iio_dev *indio_dev = p;
  747. struct adxl380_state *st = iio_priv(indio_dev);
  748. u8 status0, status1;
  749. u16 fifo_entries;
  750. int i;
  751. int ret;
  752. guard(mutex)(&st->lock);
  753. ret = adxl380_get_status(st, &status0, &status1);
  754. if (ret)
  755. return IRQ_HANDLED;
  756. adxl380_push_event(indio_dev, iio_get_time_ns(indio_dev), status1);
  757. if (!FIELD_GET(ADXL380_STATUS_0_FIFO_WM_MSK, status0))
  758. return IRQ_HANDLED;
  759. ret = adxl380_get_fifo_entries(st, &fifo_entries);
  760. if (ret)
  761. return IRQ_HANDLED;
  762. for (i = 0; i < fifo_entries; i += st->fifo_set_size) {
  763. ret = regmap_noinc_read(st->regmap, ADXL380_FIFO_DATA,
  764. &st->fifo_buf[i],
  765. 2 * st->fifo_set_size);
  766. if (ret)
  767. return IRQ_HANDLED;
  768. iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
  769. }
  770. return IRQ_HANDLED;
  771. }
  772. static int adxl380_write_calibbias_value(struct adxl380_state *st,
  773. unsigned long chan_addr,
  774. s8 calibbias)
  775. {
  776. int ret;
  777. guard(mutex)(&st->lock);
  778. ret = adxl380_set_measure_en(st, false);
  779. if (ret)
  780. return ret;
  781. ret = regmap_write(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, calibbias);
  782. if (ret)
  783. return ret;
  784. return adxl380_set_measure_en(st, true);
  785. }
  786. static int adxl380_read_calibbias_value(struct adxl380_state *st,
  787. unsigned long chan_addr,
  788. int *calibbias)
  789. {
  790. int ret;
  791. unsigned int reg_val;
  792. guard(mutex)(&st->lock);
  793. ret = regmap_read(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, &reg_val);
  794. if (ret)
  795. return ret;
  796. *calibbias = sign_extend32(reg_val, 7);
  797. return 0;
  798. }
  799. static ssize_t hwfifo_watermark_min_show(struct device *dev,
  800. struct device_attribute *attr,
  801. char *buf)
  802. {
  803. return sysfs_emit(buf, "1\n");
  804. }
  805. static ssize_t hwfifo_watermark_max_show(struct device *dev,
  806. struct device_attribute *attr,
  807. char *buf)
  808. {
  809. return sysfs_emit(buf, "%lu\n", ADXL380_FIFO_SAMPLES);
  810. }
  811. static ssize_t adxl380_get_fifo_watermark(struct device *dev,
  812. struct device_attribute *attr,
  813. char *buf)
  814. {
  815. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  816. struct adxl380_state *st = iio_priv(indio_dev);
  817. return sysfs_emit(buf, "%d\n", st->watermark);
  818. }
  819. static ssize_t adxl380_get_fifo_enabled(struct device *dev,
  820. struct device_attribute *attr,
  821. char *buf)
  822. {
  823. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  824. struct adxl380_state *st = iio_priv(indio_dev);
  825. int ret;
  826. unsigned int reg_val;
  827. ret = regmap_read(st->regmap, ADXL380_DIG_EN_REG, &reg_val);
  828. if (ret)
  829. return ret;
  830. return sysfs_emit(buf, "%lu\n",
  831. FIELD_GET(ADXL380_FIFO_EN_MSK, reg_val));
  832. }
  833. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
  834. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
  835. static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
  836. adxl380_get_fifo_watermark, NULL, 0);
  837. static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  838. adxl380_get_fifo_enabled, NULL, 0);
  839. static const struct iio_dev_attr *adxl380_fifo_attributes[] = {
  840. &iio_dev_attr_hwfifo_watermark_min,
  841. &iio_dev_attr_hwfifo_watermark_max,
  842. &iio_dev_attr_hwfifo_watermark,
  843. &iio_dev_attr_hwfifo_enabled,
  844. NULL
  845. };
  846. static int adxl380_buffer_postenable(struct iio_dev *indio_dev)
  847. {
  848. struct adxl380_state *st = iio_priv(indio_dev);
  849. int i;
  850. int ret;
  851. guard(mutex)(&st->lock);
  852. ret = adxl380_set_measure_en(st, false);
  853. if (ret)
  854. return ret;
  855. ret = regmap_update_bits(st->regmap,
  856. st->int_map[0],
  857. ADXL380_INT_MAP0_FIFO_WM_INT0_MSK,
  858. FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 1));
  859. if (ret)
  860. return ret;
  861. for_each_clear_bit(i, indio_dev->active_scan_mask, ADXL380_CH_NUM) {
  862. ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
  863. ADXL380_CHAN_EN_MSK(i),
  864. 0 << (4 + i));
  865. if (ret)
  866. return ret;
  867. }
  868. st->fifo_set_size = bitmap_weight(indio_dev->active_scan_mask,
  869. iio_get_masklength(indio_dev));
  870. if ((st->watermark * st->fifo_set_size) > ADXL380_FIFO_SAMPLES)
  871. st->watermark = (ADXL380_FIFO_SAMPLES / st->fifo_set_size);
  872. ret = adxl380_set_fifo_samples(st);
  873. if (ret)
  874. return ret;
  875. ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK,
  876. FIELD_PREP(ADXL380_FIFO_EN_MSK, 1));
  877. if (ret)
  878. return ret;
  879. return adxl380_set_measure_en(st, true);
  880. }
  881. static int adxl380_buffer_predisable(struct iio_dev *indio_dev)
  882. {
  883. struct adxl380_state *st = iio_priv(indio_dev);
  884. int ret, i;
  885. guard(mutex)(&st->lock);
  886. ret = adxl380_set_measure_en(st, false);
  887. if (ret)
  888. return ret;
  889. ret = regmap_update_bits(st->regmap,
  890. st->int_map[0],
  891. ADXL380_INT_MAP0_FIFO_WM_INT0_MSK,
  892. FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 0));
  893. if (ret)
  894. return ret;
  895. for (i = 0; i < indio_dev->num_channels; i++) {
  896. ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
  897. ADXL380_CHAN_EN_MSK(i),
  898. 1 << (4 + i));
  899. if (ret)
  900. return ret;
  901. }
  902. ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK,
  903. FIELD_PREP(ADXL380_FIFO_EN_MSK, 0));
  904. if (ret)
  905. return ret;
  906. return adxl380_set_measure_en(st, true);
  907. }
  908. static const struct iio_buffer_setup_ops adxl380_buffer_ops = {
  909. .postenable = adxl380_buffer_postenable,
  910. .predisable = adxl380_buffer_predisable,
  911. };
  912. static int adxl380_read_raw(struct iio_dev *indio_dev,
  913. struct iio_chan_spec const *chan,
  914. int *val, int *val2, long info)
  915. {
  916. struct adxl380_state *st = iio_priv(indio_dev);
  917. int ret;
  918. switch (info) {
  919. case IIO_CHAN_INFO_RAW:
  920. ret = iio_device_claim_direct_mode(indio_dev);
  921. if (ret)
  922. return ret;
  923. ret = adxl380_read_chn(st, chan->address);
  924. iio_device_release_direct_mode(indio_dev);
  925. if (ret < 0)
  926. return ret;
  927. *val = sign_extend32(ret >> chan->scan_type.shift,
  928. chan->scan_type.realbits - 1);
  929. return IIO_VAL_INT;
  930. case IIO_CHAN_INFO_SCALE:
  931. switch (chan->type) {
  932. case IIO_ACCEL:
  933. scoped_guard(mutex, &st->lock) {
  934. *val = st->chip_info->scale_tbl[st->range][0];
  935. *val2 = st->chip_info->scale_tbl[st->range][1];
  936. }
  937. return IIO_VAL_INT_PLUS_NANO;
  938. case IIO_TEMP:
  939. /* 10.2 LSB / Degree Celsius */
  940. *val = 10000;
  941. *val2 = 102;
  942. return IIO_VAL_FRACTIONAL;
  943. default:
  944. return -EINVAL;
  945. }
  946. case IIO_CHAN_INFO_OFFSET:
  947. switch (chan->type) {
  948. case IIO_TEMP:
  949. *val = st->chip_info->temp_offset;
  950. return IIO_VAL_INT;
  951. default:
  952. return -EINVAL;
  953. }
  954. case IIO_CHAN_INFO_CALIBBIAS:
  955. switch (chan->type) {
  956. case IIO_ACCEL:
  957. ret = adxl380_read_calibbias_value(st, chan->scan_index, val);
  958. if (ret)
  959. return ret;
  960. return IIO_VAL_INT;
  961. default:
  962. return -EINVAL;
  963. }
  964. case IIO_CHAN_INFO_SAMP_FREQ:
  965. ret = adxl380_get_odr(st, val);
  966. if (ret)
  967. return ret;
  968. return IIO_VAL_INT;
  969. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  970. ret = adxl380_get_lpf(st, val);
  971. if (ret)
  972. return ret;
  973. return IIO_VAL_INT;
  974. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  975. ret = adxl380_get_hpf(st, val, val2);
  976. if (ret)
  977. return ret;
  978. return IIO_VAL_INT_PLUS_MICRO;
  979. }
  980. return -EINVAL;
  981. }
  982. static int adxl380_read_avail(struct iio_dev *indio_dev,
  983. struct iio_chan_spec const *chan,
  984. const int **vals, int *type, int *length,
  985. long mask)
  986. {
  987. struct adxl380_state *st = iio_priv(indio_dev);
  988. if (chan->type != IIO_ACCEL)
  989. return -EINVAL;
  990. switch (mask) {
  991. case IIO_CHAN_INFO_SCALE:
  992. *vals = (const int *)st->chip_info->scale_tbl;
  993. *type = IIO_VAL_INT_PLUS_NANO;
  994. *length = ARRAY_SIZE(st->chip_info->scale_tbl) * 2;
  995. return IIO_AVAIL_LIST;
  996. case IIO_CHAN_INFO_SAMP_FREQ:
  997. *vals = (const int *)st->chip_info->samp_freq_tbl;
  998. *type = IIO_VAL_INT;
  999. *length = ARRAY_SIZE(st->chip_info->samp_freq_tbl);
  1000. return IIO_AVAIL_LIST;
  1001. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  1002. *vals = (const int *)st->lpf_tbl;
  1003. *type = IIO_VAL_INT;
  1004. *length = ARRAY_SIZE(st->lpf_tbl);
  1005. return IIO_AVAIL_LIST;
  1006. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  1007. *vals = (const int *)st->hpf_tbl;
  1008. *type = IIO_VAL_INT_PLUS_MICRO;
  1009. /* Values are stored in a 2D matrix */
  1010. *length = ARRAY_SIZE(st->hpf_tbl) * 2;
  1011. return IIO_AVAIL_LIST;
  1012. default:
  1013. return -EINVAL;
  1014. }
  1015. }
  1016. static int adxl380_write_raw(struct iio_dev *indio_dev,
  1017. struct iio_chan_spec const *chan,
  1018. int val, int val2, long info)
  1019. {
  1020. struct adxl380_state *st = iio_priv(indio_dev);
  1021. int odr_index, lpf_index, hpf_index, range_index;
  1022. switch (info) {
  1023. case IIO_CHAN_INFO_SAMP_FREQ:
  1024. odr_index = adxl380_find_match_1d_tbl(st->chip_info->samp_freq_tbl,
  1025. ARRAY_SIZE(st->chip_info->samp_freq_tbl),
  1026. val);
  1027. return adxl380_set_odr(st, odr_index);
  1028. case IIO_CHAN_INFO_CALIBBIAS:
  1029. return adxl380_write_calibbias_value(st, chan->scan_index, val);
  1030. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  1031. lpf_index = adxl380_find_match_1d_tbl(st->lpf_tbl,
  1032. ARRAY_SIZE(st->lpf_tbl),
  1033. val);
  1034. return adxl380_set_lpf(st, lpf_index);
  1035. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  1036. hpf_index = adxl380_find_match_2d_tbl(st->hpf_tbl,
  1037. ARRAY_SIZE(st->hpf_tbl),
  1038. val, val2);
  1039. if (hpf_index < 0)
  1040. return hpf_index;
  1041. return adxl380_set_hpf(st, hpf_index);
  1042. case IIO_CHAN_INFO_SCALE:
  1043. range_index = adxl380_find_match_2d_tbl(st->chip_info->scale_tbl,
  1044. ARRAY_SIZE(st->chip_info->scale_tbl),
  1045. val, val2);
  1046. if (range_index < 0)
  1047. return range_index;
  1048. return adxl380_set_range(st, range_index);
  1049. default:
  1050. return -EINVAL;
  1051. }
  1052. }
  1053. static int adxl380_write_raw_get_fmt(struct iio_dev *indio_dev,
  1054. struct iio_chan_spec const *chan,
  1055. long info)
  1056. {
  1057. switch (info) {
  1058. case IIO_CHAN_INFO_SCALE:
  1059. if (chan->type != IIO_ACCEL)
  1060. return -EINVAL;
  1061. return IIO_VAL_INT_PLUS_NANO;
  1062. default:
  1063. return IIO_VAL_INT_PLUS_MICRO;
  1064. }
  1065. }
  1066. static int adxl380_read_event_config(struct iio_dev *indio_dev,
  1067. const struct iio_chan_spec *chan,
  1068. enum iio_event_type type,
  1069. enum iio_event_direction dir)
  1070. {
  1071. struct adxl380_state *st = iio_priv(indio_dev);
  1072. int ret;
  1073. bool int_en;
  1074. bool tap_axis_en = false;
  1075. switch (chan->channel2) {
  1076. case IIO_MOD_X:
  1077. tap_axis_en = st->tap_axis_en == ADXL380_X_AXIS;
  1078. break;
  1079. case IIO_MOD_Y:
  1080. tap_axis_en = st->tap_axis_en == ADXL380_Y_AXIS;
  1081. break;
  1082. case IIO_MOD_Z:
  1083. tap_axis_en = st->tap_axis_en == ADXL380_Z_AXIS;
  1084. break;
  1085. default:
  1086. return -EINVAL;
  1087. }
  1088. switch (dir) {
  1089. case IIO_EV_DIR_RISING:
  1090. ret = adxl380_read_act_inact_int(st, ADXL380_ACTIVITY, &int_en);
  1091. if (ret)
  1092. return ret;
  1093. return int_en;
  1094. case IIO_EV_DIR_FALLING:
  1095. ret = adxl380_read_act_inact_int(st, ADXL380_INACTIVITY, &int_en);
  1096. if (ret)
  1097. return ret;
  1098. return int_en;
  1099. case IIO_EV_DIR_SINGLETAP:
  1100. ret = adxl380_read_tap_int(st, ADXL380_SINGLE_TAP, &int_en);
  1101. if (ret)
  1102. return ret;
  1103. return int_en && tap_axis_en;
  1104. case IIO_EV_DIR_DOUBLETAP:
  1105. ret = adxl380_read_tap_int(st, ADXL380_DOUBLE_TAP, &int_en);
  1106. if (ret)
  1107. return ret;
  1108. return int_en && tap_axis_en;
  1109. default:
  1110. return -EINVAL;
  1111. }
  1112. }
  1113. static int adxl380_write_event_config(struct iio_dev *indio_dev,
  1114. const struct iio_chan_spec *chan,
  1115. enum iio_event_type type,
  1116. enum iio_event_direction dir,
  1117. int state)
  1118. {
  1119. struct adxl380_state *st = iio_priv(indio_dev);
  1120. enum adxl380_axis axis;
  1121. switch (chan->channel2) {
  1122. case IIO_MOD_X:
  1123. axis = ADXL380_X_AXIS;
  1124. break;
  1125. case IIO_MOD_Y:
  1126. axis = ADXL380_Y_AXIS;
  1127. break;
  1128. case IIO_MOD_Z:
  1129. axis = ADXL380_Z_AXIS;
  1130. break;
  1131. default:
  1132. return -EINVAL;
  1133. }
  1134. switch (dir) {
  1135. case IIO_EV_DIR_RISING:
  1136. return adxl380_act_inact_config(st, ADXL380_ACTIVITY, state);
  1137. case IIO_EV_DIR_FALLING:
  1138. return adxl380_act_inact_config(st, ADXL380_INACTIVITY, state);
  1139. case IIO_EV_DIR_SINGLETAP:
  1140. return adxl380_tap_config(st, axis, ADXL380_SINGLE_TAP, state);
  1141. case IIO_EV_DIR_DOUBLETAP:
  1142. return adxl380_tap_config(st, axis, ADXL380_DOUBLE_TAP, state);
  1143. default:
  1144. return -EINVAL;
  1145. }
  1146. }
  1147. static int adxl380_read_event_value(struct iio_dev *indio_dev,
  1148. const struct iio_chan_spec *chan,
  1149. enum iio_event_type type,
  1150. enum iio_event_direction dir,
  1151. enum iio_event_info info,
  1152. int *val, int *val2)
  1153. {
  1154. struct adxl380_state *st = iio_priv(indio_dev);
  1155. guard(mutex)(&st->lock);
  1156. switch (type) {
  1157. case IIO_EV_TYPE_THRESH:
  1158. switch (info) {
  1159. case IIO_EV_INFO_VALUE: {
  1160. switch (dir) {
  1161. case IIO_EV_DIR_RISING:
  1162. *val = st->act_threshold;
  1163. return IIO_VAL_INT;
  1164. case IIO_EV_DIR_FALLING:
  1165. *val = st->inact_threshold;
  1166. return IIO_VAL_INT;
  1167. default:
  1168. return -EINVAL;
  1169. }
  1170. }
  1171. case IIO_EV_INFO_PERIOD:
  1172. switch (dir) {
  1173. case IIO_EV_DIR_RISING:
  1174. *val = st->act_time_ms;
  1175. *val2 = 1000;
  1176. return IIO_VAL_FRACTIONAL;
  1177. case IIO_EV_DIR_FALLING:
  1178. *val = st->inact_time_ms;
  1179. *val2 = 1000;
  1180. return IIO_VAL_FRACTIONAL;
  1181. default:
  1182. return -EINVAL;
  1183. }
  1184. default:
  1185. return -EINVAL;
  1186. }
  1187. case IIO_EV_TYPE_GESTURE:
  1188. switch (info) {
  1189. case IIO_EV_INFO_VALUE:
  1190. *val = st->tap_threshold;
  1191. return IIO_VAL_INT;
  1192. case IIO_EV_INFO_RESET_TIMEOUT:
  1193. *val = st->tap_window_us;
  1194. *val2 = 1000000;
  1195. return IIO_VAL_FRACTIONAL;
  1196. case IIO_EV_INFO_TAP2_MIN_DELAY:
  1197. *val = st->tap_latent_us;
  1198. *val2 = 1000000;
  1199. return IIO_VAL_FRACTIONAL;
  1200. default:
  1201. return -EINVAL;
  1202. }
  1203. default:
  1204. return -EINVAL;
  1205. }
  1206. }
  1207. static int adxl380_write_event_value(struct iio_dev *indio_dev,
  1208. const struct iio_chan_spec *chan,
  1209. enum iio_event_type type, enum iio_event_direction dir,
  1210. enum iio_event_info info, int val, int val2)
  1211. {
  1212. struct adxl380_state *st = iio_priv(indio_dev);
  1213. u32 val_ms, val_us;
  1214. if (chan->type != IIO_ACCEL)
  1215. return -EINVAL;
  1216. switch (type) {
  1217. case IIO_EV_TYPE_THRESH:
  1218. switch (info) {
  1219. case IIO_EV_INFO_VALUE:
  1220. switch (dir) {
  1221. case IIO_EV_DIR_RISING:
  1222. return adxl380_set_act_inact_threshold(indio_dev,
  1223. ADXL380_ACTIVITY, val);
  1224. case IIO_EV_DIR_FALLING:
  1225. return adxl380_set_act_inact_threshold(indio_dev,
  1226. ADXL380_INACTIVITY, val);
  1227. default:
  1228. return -EINVAL;
  1229. }
  1230. case IIO_EV_INFO_PERIOD:
  1231. val_ms = val * 1000 + DIV_ROUND_UP(val2, 1000);
  1232. switch (dir) {
  1233. case IIO_EV_DIR_RISING:
  1234. return adxl380_set_act_inact_time_ms(st,
  1235. ADXL380_ACTIVITY, val_ms);
  1236. case IIO_EV_DIR_FALLING:
  1237. return adxl380_set_act_inact_time_ms(st,
  1238. ADXL380_INACTIVITY, val_ms);
  1239. default:
  1240. return -EINVAL;
  1241. }
  1242. default:
  1243. return -EINVAL;
  1244. }
  1245. case IIO_EV_TYPE_GESTURE:
  1246. switch (info) {
  1247. case IIO_EV_INFO_VALUE:
  1248. return adxl380_set_tap_threshold_value(indio_dev, val);
  1249. case IIO_EV_INFO_RESET_TIMEOUT:
  1250. val_us = val * 1000000 + val2;
  1251. return adxl380_write_tap_time_us(st,
  1252. ADXL380_TAP_TIME_WINDOW,
  1253. val_us);
  1254. case IIO_EV_INFO_TAP2_MIN_DELAY:
  1255. val_us = val * 1000000 + val2;
  1256. return adxl380_write_tap_time_us(st,
  1257. ADXL380_TAP_TIME_LATENT,
  1258. val_us);
  1259. default:
  1260. return -EINVAL;
  1261. }
  1262. default:
  1263. return -EINVAL;
  1264. }
  1265. }
  1266. static ssize_t in_accel_gesture_tap_maxtomin_time_show(struct device *dev,
  1267. struct device_attribute *attr,
  1268. char *buf)
  1269. {
  1270. int vals[2];
  1271. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1272. struct adxl380_state *st = iio_priv(indio_dev);
  1273. guard(mutex)(&st->lock);
  1274. vals[0] = st->tap_duration_us;
  1275. vals[1] = MICRO;
  1276. return iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, vals);
  1277. }
  1278. static ssize_t in_accel_gesture_tap_maxtomin_time_store(struct device *dev,
  1279. struct device_attribute *attr,
  1280. const char *buf, size_t len)
  1281. {
  1282. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1283. struct adxl380_state *st = iio_priv(indio_dev);
  1284. int ret, val_int, val_fract_us;
  1285. guard(mutex)(&st->lock);
  1286. ret = iio_str_to_fixpoint(buf, 100000, &val_int, &val_fract_us);
  1287. if (ret)
  1288. return ret;
  1289. /* maximum value is 255 * 625 us = 0.159375 seconds */
  1290. if (val_int || val_fract_us > 159375 || val_fract_us < 0)
  1291. return -EINVAL;
  1292. ret = adxl380_write_tap_dur_us(indio_dev, val_fract_us);
  1293. if (ret)
  1294. return ret;
  1295. return len;
  1296. }
  1297. static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time, 0);
  1298. static struct attribute *adxl380_event_attributes[] = {
  1299. &iio_dev_attr_in_accel_gesture_tap_maxtomin_time.dev_attr.attr,
  1300. NULL
  1301. };
  1302. static const struct attribute_group adxl380_event_attribute_group = {
  1303. .attrs = adxl380_event_attributes,
  1304. };
  1305. static int adxl380_reg_access(struct iio_dev *indio_dev,
  1306. unsigned int reg,
  1307. unsigned int writeval,
  1308. unsigned int *readval)
  1309. {
  1310. struct adxl380_state *st = iio_priv(indio_dev);
  1311. if (readval)
  1312. return regmap_read(st->regmap, reg, readval);
  1313. return regmap_write(st->regmap, reg, writeval);
  1314. }
  1315. static int adxl380_set_watermark(struct iio_dev *indio_dev, unsigned int val)
  1316. {
  1317. struct adxl380_state *st = iio_priv(indio_dev);
  1318. st->watermark = min(val, ADXL380_FIFO_SAMPLES);
  1319. return 0;
  1320. }
  1321. static const struct iio_info adxl380_info = {
  1322. .read_raw = adxl380_read_raw,
  1323. .read_avail = &adxl380_read_avail,
  1324. .write_raw = adxl380_write_raw,
  1325. .write_raw_get_fmt = adxl380_write_raw_get_fmt,
  1326. .read_event_config = adxl380_read_event_config,
  1327. .write_event_config = adxl380_write_event_config,
  1328. .read_event_value = adxl380_read_event_value,
  1329. .write_event_value = adxl380_write_event_value,
  1330. .event_attrs = &adxl380_event_attribute_group,
  1331. .debugfs_reg_access = &adxl380_reg_access,
  1332. .hwfifo_set_watermark = adxl380_set_watermark,
  1333. };
  1334. static const struct iio_event_spec adxl380_events[] = {
  1335. {
  1336. .type = IIO_EV_TYPE_THRESH,
  1337. .dir = IIO_EV_DIR_RISING,
  1338. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1339. BIT(IIO_EV_INFO_VALUE) |
  1340. BIT(IIO_EV_INFO_PERIOD),
  1341. },
  1342. {
  1343. .type = IIO_EV_TYPE_THRESH,
  1344. .dir = IIO_EV_DIR_FALLING,
  1345. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1346. BIT(IIO_EV_INFO_VALUE) |
  1347. BIT(IIO_EV_INFO_PERIOD),
  1348. },
  1349. {
  1350. .type = IIO_EV_TYPE_GESTURE,
  1351. .dir = IIO_EV_DIR_SINGLETAP,
  1352. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  1353. .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
  1354. BIT(IIO_EV_INFO_RESET_TIMEOUT),
  1355. },
  1356. {
  1357. .type = IIO_EV_TYPE_GESTURE,
  1358. .dir = IIO_EV_DIR_DOUBLETAP,
  1359. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  1360. .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
  1361. BIT(IIO_EV_INFO_RESET_TIMEOUT) |
  1362. BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
  1363. },
  1364. };
  1365. #define ADXL380_ACCEL_CHANNEL(index, reg, axis) { \
  1366. .type = IIO_ACCEL, \
  1367. .address = reg, \
  1368. .modified = 1, \
  1369. .channel2 = IIO_MOD_##axis, \
  1370. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  1371. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  1372. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1373. .info_mask_shared_by_all_available = \
  1374. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1375. .info_mask_shared_by_type = \
  1376. BIT(IIO_CHAN_INFO_SCALE) | \
  1377. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  1378. BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
  1379. .info_mask_shared_by_type_available = \
  1380. BIT(IIO_CHAN_INFO_SCALE) | \
  1381. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  1382. BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
  1383. .scan_index = index, \
  1384. .scan_type = { \
  1385. .sign = 's', \
  1386. .realbits = 16, \
  1387. .storagebits = 16, \
  1388. .endianness = IIO_BE, \
  1389. }, \
  1390. .event_spec = adxl380_events, \
  1391. .num_event_specs = ARRAY_SIZE(adxl380_events) \
  1392. }
  1393. static const struct iio_chan_spec adxl380_channels[] = {
  1394. ADXL380_ACCEL_CHANNEL(0, ADXL380_X_DATA_H_REG, X),
  1395. ADXL380_ACCEL_CHANNEL(1, ADXL380_Y_DATA_H_REG, Y),
  1396. ADXL380_ACCEL_CHANNEL(2, ADXL380_Z_DATA_H_REG, Z),
  1397. {
  1398. .type = IIO_TEMP,
  1399. .address = ADXL380_T_DATA_H_REG,
  1400. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1401. BIT(IIO_CHAN_INFO_SCALE) |
  1402. BIT(IIO_CHAN_INFO_OFFSET),
  1403. .scan_index = 3,
  1404. .scan_type = {
  1405. .sign = 's',
  1406. .realbits = 12,
  1407. .storagebits = 16,
  1408. .shift = 4,
  1409. .endianness = IIO_BE,
  1410. },
  1411. },
  1412. };
  1413. static int adxl380_config_irq(struct iio_dev *indio_dev)
  1414. {
  1415. struct adxl380_state *st = iio_priv(indio_dev);
  1416. unsigned long irq_flag;
  1417. struct irq_data *desc;
  1418. u32 irq_type;
  1419. u8 polarity;
  1420. int ret;
  1421. st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT0");
  1422. if (st->irq > 0) {
  1423. st->int_map[0] = ADXL380_INT0_MAP0_REG;
  1424. st->int_map[1] = ADXL380_INT0_MAP1_REG;
  1425. } else {
  1426. st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT1");
  1427. if (st->irq > 0)
  1428. return dev_err_probe(st->dev, -ENODEV,
  1429. "no interrupt name specified");
  1430. st->int_map[0] = ADXL380_INT1_MAP0_REG;
  1431. st->int_map[1] = ADXL380_INT1_MAP1_REG;
  1432. }
  1433. desc = irq_get_irq_data(st->irq);
  1434. if (!desc)
  1435. return dev_err_probe(st->dev, -EINVAL, "Could not find IRQ %d\n", st->irq);
  1436. irq_type = irqd_get_trigger_type(desc);
  1437. if (irq_type == IRQ_TYPE_LEVEL_HIGH) {
  1438. polarity = 0;
  1439. irq_flag = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
  1440. } else if (irq_type == IRQ_TYPE_LEVEL_LOW) {
  1441. polarity = 1;
  1442. irq_flag = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
  1443. } else {
  1444. return dev_err_probe(st->dev, -EINVAL,
  1445. "Invalid interrupt 0x%x. Only level interrupts supported\n",
  1446. irq_type);
  1447. }
  1448. ret = regmap_update_bits(st->regmap, ADXL380_INT0_REG,
  1449. ADXL380_INT0_POL_MSK,
  1450. FIELD_PREP(ADXL380_INT0_POL_MSK, polarity));
  1451. if (ret)
  1452. return ret;
  1453. return devm_request_threaded_irq(st->dev, st->irq, NULL,
  1454. adxl380_irq_handler, irq_flag,
  1455. indio_dev->name, indio_dev);
  1456. }
  1457. static int adxl380_setup(struct iio_dev *indio_dev)
  1458. {
  1459. unsigned int reg_val;
  1460. u16 part_id, chip_id;
  1461. int ret, i;
  1462. struct adxl380_state *st = iio_priv(indio_dev);
  1463. ret = regmap_read(st->regmap, ADXL380_DEVID_AD_REG, &reg_val);
  1464. if (ret)
  1465. return ret;
  1466. if (reg_val != ADXL380_DEVID_AD_VAL)
  1467. dev_warn(st->dev, "Unknown chip id %x\n", reg_val);
  1468. ret = regmap_bulk_read(st->regmap, ADLX380_PART_ID_REG,
  1469. &st->transf_buf, 2);
  1470. if (ret)
  1471. return ret;
  1472. part_id = get_unaligned_be16(st->transf_buf);
  1473. part_id >>= 4;
  1474. if (part_id != ADXL380_ID_VAL)
  1475. dev_warn(st->dev, "Unknown part id %x\n", part_id);
  1476. ret = regmap_read(st->regmap, ADXL380_MISC_0_REG, &reg_val);
  1477. if (ret)
  1478. return ret;
  1479. /* Bit to differentiate between ADXL380/382. */
  1480. if (reg_val & ADXL380_XL382_MSK)
  1481. chip_id = ADXL382_ID_VAL;
  1482. else
  1483. chip_id = ADXL380_ID_VAL;
  1484. if (chip_id != st->chip_info->chip_id)
  1485. dev_warn(st->dev, "Unknown chip id %x\n", chip_id);
  1486. ret = regmap_write(st->regmap, ADXL380_RESET_REG, ADXL380_RESET_CODE);
  1487. if (ret)
  1488. return ret;
  1489. /*
  1490. * A latency of approximately 0.5 ms is required after soft reset.
  1491. * Stated in the register REG_RESET description.
  1492. */
  1493. fsleep(500);
  1494. for (i = 0; i < indio_dev->num_channels; i++) {
  1495. ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
  1496. ADXL380_CHAN_EN_MSK(i),
  1497. 1 << (4 + i));
  1498. if (ret)
  1499. return ret;
  1500. }
  1501. ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
  1502. ADXL380_FIFO_MODE_MSK,
  1503. FIELD_PREP(ADXL380_FIFO_MODE_MSK, ADXL380_FIFO_STREAMED));
  1504. if (ret)
  1505. return ret;
  1506. /* Select all 3 axis for act/inact detection. */
  1507. ret = regmap_update_bits(st->regmap, ADXL380_SNSR_AXIS_EN_REG,
  1508. ADXL380_ACT_INACT_AXIS_EN_MSK,
  1509. FIELD_PREP(ADXL380_ACT_INACT_AXIS_EN_MSK,
  1510. ADXL380_ACT_INACT_AXIS_EN_MSK));
  1511. if (ret)
  1512. return ret;
  1513. ret = adxl380_config_irq(indio_dev);
  1514. if (ret)
  1515. return ret;
  1516. ret = adxl380_fill_lpf_tbl(st);
  1517. if (ret)
  1518. return ret;
  1519. ret = adxl380_fill_hpf_tbl(st);
  1520. if (ret)
  1521. return ret;
  1522. return adxl380_set_measure_en(st, true);
  1523. }
  1524. int adxl380_probe(struct device *dev, struct regmap *regmap,
  1525. const struct adxl380_chip_info *chip_info)
  1526. {
  1527. struct iio_dev *indio_dev;
  1528. struct adxl380_state *st;
  1529. int ret;
  1530. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1531. if (!indio_dev)
  1532. return -ENOMEM;
  1533. st = iio_priv(indio_dev);
  1534. st->dev = dev;
  1535. st->regmap = regmap;
  1536. st->chip_info = chip_info;
  1537. mutex_init(&st->lock);
  1538. indio_dev->channels = adxl380_channels;
  1539. indio_dev->num_channels = ARRAY_SIZE(adxl380_channels);
  1540. indio_dev->name = chip_info->name;
  1541. indio_dev->info = &adxl380_info;
  1542. indio_dev->modes = INDIO_DIRECT_MODE;
  1543. ret = devm_regulator_get_enable(dev, "vddio");
  1544. if (ret)
  1545. return dev_err_probe(st->dev, ret,
  1546. "Failed to get vddio regulator\n");
  1547. ret = devm_regulator_get_enable(st->dev, "vsupply");
  1548. if (ret)
  1549. return dev_err_probe(st->dev, ret,
  1550. "Failed to get vsupply regulator\n");
  1551. ret = adxl380_setup(indio_dev);
  1552. if (ret)
  1553. return ret;
  1554. ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev,
  1555. &adxl380_buffer_ops,
  1556. adxl380_fifo_attributes);
  1557. if (ret)
  1558. return ret;
  1559. return devm_iio_device_register(dev, indio_dev);
  1560. }
  1561. EXPORT_SYMBOL_NS_GPL(adxl380_probe, IIO_ADXL380);
  1562. MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>");
  1563. MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
  1564. MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer driver");
  1565. MODULE_LICENSE("GPL");