adxl367.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Analog Devices, Inc.
  4. * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/iio/buffer.h>
  9. #include <linux/iio/events.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/kfifo_buf.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/unaligned.h>
  19. #include "adxl367.h"
  20. #define ADXL367_REG_DEVID 0x00
  21. #define ADXL367_DEVID_AD 0xAD
  22. #define ADXL367_REG_STATUS 0x0B
  23. #define ADXL367_STATUS_INACT_MASK BIT(5)
  24. #define ADXL367_STATUS_ACT_MASK BIT(4)
  25. #define ADXL367_STATUS_FIFO_FULL_MASK BIT(2)
  26. #define ADXL367_FIFO_ENT_H_MASK GENMASK(1, 0)
  27. #define ADXL367_REG_X_DATA_H 0x0E
  28. #define ADXL367_REG_Y_DATA_H 0x10
  29. #define ADXL367_REG_Z_DATA_H 0x12
  30. #define ADXL367_REG_TEMP_DATA_H 0x14
  31. #define ADXL367_REG_EX_ADC_DATA_H 0x16
  32. #define ADXL367_DATA_MASK GENMASK(15, 2)
  33. #define ADXL367_TEMP_25C 165
  34. #define ADXL367_TEMP_PER_C 54
  35. #define ADXL367_VOLTAGE_OFFSET 8192
  36. #define ADXL367_VOLTAGE_MAX_MV 1000
  37. #define ADXL367_VOLTAGE_MAX_RAW GENMASK(13, 0)
  38. #define ADXL367_REG_RESET 0x1F
  39. #define ADXL367_RESET_CODE 0x52
  40. #define ADXL367_REG_THRESH_ACT_H 0x20
  41. #define ADXL367_REG_THRESH_INACT_H 0x23
  42. #define ADXL367_THRESH_MAX GENMASK(12, 0)
  43. #define ADXL367_THRESH_VAL_H_MASK GENMASK(12, 6)
  44. #define ADXL367_THRESH_H_MASK GENMASK(6, 0)
  45. #define ADXL367_THRESH_VAL_L_MASK GENMASK(5, 0)
  46. #define ADXL367_THRESH_L_MASK GENMASK(7, 2)
  47. #define ADXL367_REG_TIME_ACT 0x22
  48. #define ADXL367_REG_TIME_INACT_H 0x25
  49. #define ADXL367_TIME_ACT_MAX GENMASK(7, 0)
  50. #define ADXL367_TIME_INACT_MAX GENMASK(15, 0)
  51. #define ADXL367_TIME_INACT_VAL_H_MASK GENMASK(15, 8)
  52. #define ADXL367_TIME_INACT_H_MASK GENMASK(7, 0)
  53. #define ADXL367_TIME_INACT_VAL_L_MASK GENMASK(7, 0)
  54. #define ADXL367_TIME_INACT_L_MASK GENMASK(7, 0)
  55. #define ADXL367_REG_ACT_INACT_CTL 0x27
  56. #define ADXL367_ACT_EN_MASK GENMASK(1, 0)
  57. #define ADXL367_ACT_LINKLOOP_MASK GENMASK(5, 4)
  58. #define ADXL367_REG_FIFO_CTL 0x28
  59. #define ADXL367_FIFO_CTL_FORMAT_MASK GENMASK(6, 3)
  60. #define ADXL367_FIFO_CTL_MODE_MASK GENMASK(1, 0)
  61. #define ADXL367_REG_FIFO_SAMPLES 0x29
  62. #define ADXL367_FIFO_SIZE 512
  63. #define ADXL367_FIFO_MAX_WATERMARK 511
  64. #define ADXL367_SAMPLES_VAL_H_MASK BIT(8)
  65. #define ADXL367_SAMPLES_H_MASK BIT(2)
  66. #define ADXL367_SAMPLES_VAL_L_MASK GENMASK(7, 0)
  67. #define ADXL367_SAMPLES_L_MASK GENMASK(7, 0)
  68. #define ADXL367_REG_INT1_MAP 0x2A
  69. #define ADXL367_INT_INACT_MASK BIT(5)
  70. #define ADXL367_INT_ACT_MASK BIT(4)
  71. #define ADXL367_INT_FIFO_WATERMARK_MASK BIT(2)
  72. #define ADXL367_REG_FILTER_CTL 0x2C
  73. #define ADXL367_FILTER_CTL_RANGE_MASK GENMASK(7, 6)
  74. #define ADXL367_2G_RANGE_1G 4095
  75. #define ADXL367_2G_RANGE_100MG 409
  76. #define ADXL367_FILTER_CTL_ODR_MASK GENMASK(2, 0)
  77. #define ADXL367_REG_POWER_CTL 0x2D
  78. #define ADXL367_POWER_CTL_MODE_MASK GENMASK(1, 0)
  79. #define ADXL367_REG_ADC_CTL 0x3C
  80. #define ADXL367_REG_TEMP_CTL 0x3D
  81. #define ADXL367_ADC_EN_MASK BIT(0)
  82. enum adxl367_range {
  83. ADXL367_2G_RANGE,
  84. ADXL367_4G_RANGE,
  85. ADXL367_8G_RANGE,
  86. };
  87. enum adxl367_fifo_mode {
  88. ADXL367_FIFO_MODE_DISABLED = 0b00,
  89. ADXL367_FIFO_MODE_STREAM = 0b10,
  90. };
  91. enum adxl367_fifo_format {
  92. ADXL367_FIFO_FORMAT_XYZ,
  93. ADXL367_FIFO_FORMAT_X,
  94. ADXL367_FIFO_FORMAT_Y,
  95. ADXL367_FIFO_FORMAT_Z,
  96. ADXL367_FIFO_FORMAT_XYZT,
  97. ADXL367_FIFO_FORMAT_XT,
  98. ADXL367_FIFO_FORMAT_YT,
  99. ADXL367_FIFO_FORMAT_ZT,
  100. ADXL367_FIFO_FORMAT_XYZA,
  101. ADXL367_FIFO_FORMAT_XA,
  102. ADXL367_FIFO_FORMAT_YA,
  103. ADXL367_FIFO_FORMAT_ZA,
  104. };
  105. enum adxl367_op_mode {
  106. ADXL367_OP_STANDBY = 0b00,
  107. ADXL367_OP_MEASURE = 0b10,
  108. };
  109. enum adxl367_act_proc_mode {
  110. ADXL367_LOOPED = 0b11,
  111. };
  112. enum adxl367_act_en_mode {
  113. ADXL367_ACT_DISABLED = 0b00,
  114. ADCL367_ACT_REF_ENABLED = 0b11,
  115. };
  116. enum adxl367_activity_type {
  117. ADXL367_ACTIVITY,
  118. ADXL367_INACTIVITY,
  119. };
  120. enum adxl367_odr {
  121. ADXL367_ODR_12P5HZ,
  122. ADXL367_ODR_25HZ,
  123. ADXL367_ODR_50HZ,
  124. ADXL367_ODR_100HZ,
  125. ADXL367_ODR_200HZ,
  126. ADXL367_ODR_400HZ,
  127. };
  128. struct adxl367_state {
  129. const struct adxl367_ops *ops;
  130. void *context;
  131. struct device *dev;
  132. struct regmap *regmap;
  133. /*
  134. * Synchronize access to members of driver state, and ensure atomicity
  135. * of consecutive regmap operations.
  136. */
  137. struct mutex lock;
  138. enum adxl367_odr odr;
  139. enum adxl367_range range;
  140. unsigned int act_threshold;
  141. unsigned int act_time_ms;
  142. unsigned int inact_threshold;
  143. unsigned int inact_time_ms;
  144. unsigned int fifo_set_size;
  145. unsigned int fifo_watermark;
  146. __be16 fifo_buf[ADXL367_FIFO_SIZE] __aligned(IIO_DMA_MINALIGN);
  147. __be16 sample_buf;
  148. u8 act_threshold_buf[2];
  149. u8 inact_time_buf[2];
  150. u8 status_buf[3];
  151. };
  152. static const unsigned int adxl367_threshold_h_reg_tbl[] = {
  153. [ADXL367_ACTIVITY] = ADXL367_REG_THRESH_ACT_H,
  154. [ADXL367_INACTIVITY] = ADXL367_REG_THRESH_INACT_H,
  155. };
  156. static const unsigned int adxl367_act_en_shift_tbl[] = {
  157. [ADXL367_ACTIVITY] = 0,
  158. [ADXL367_INACTIVITY] = 2,
  159. };
  160. static const unsigned int adxl367_act_int_mask_tbl[] = {
  161. [ADXL367_ACTIVITY] = ADXL367_INT_ACT_MASK,
  162. [ADXL367_INACTIVITY] = ADXL367_INT_INACT_MASK,
  163. };
  164. static const int adxl367_samp_freq_tbl[][2] = {
  165. [ADXL367_ODR_12P5HZ] = {12, 500000},
  166. [ADXL367_ODR_25HZ] = {25, 0},
  167. [ADXL367_ODR_50HZ] = {50, 0},
  168. [ADXL367_ODR_100HZ] = {100, 0},
  169. [ADXL367_ODR_200HZ] = {200, 0},
  170. [ADXL367_ODR_400HZ] = {400, 0},
  171. };
  172. /* (g * 2) * 9.80665 * 1000000 / (2^14 - 1) */
  173. static const int adxl367_range_scale_tbl[][2] = {
  174. [ADXL367_2G_RANGE] = {0, 2394347},
  175. [ADXL367_4G_RANGE] = {0, 4788695},
  176. [ADXL367_8G_RANGE] = {0, 9577391},
  177. };
  178. static const int adxl367_range_scale_factor_tbl[] = {
  179. [ADXL367_2G_RANGE] = 1,
  180. [ADXL367_4G_RANGE] = 2,
  181. [ADXL367_8G_RANGE] = 4,
  182. };
  183. enum {
  184. ADXL367_X_CHANNEL_INDEX,
  185. ADXL367_Y_CHANNEL_INDEX,
  186. ADXL367_Z_CHANNEL_INDEX,
  187. ADXL367_TEMP_CHANNEL_INDEX,
  188. ADXL367_EX_ADC_CHANNEL_INDEX
  189. };
  190. #define ADXL367_X_CHANNEL_MASK BIT(ADXL367_X_CHANNEL_INDEX)
  191. #define ADXL367_Y_CHANNEL_MASK BIT(ADXL367_Y_CHANNEL_INDEX)
  192. #define ADXL367_Z_CHANNEL_MASK BIT(ADXL367_Z_CHANNEL_INDEX)
  193. #define ADXL367_TEMP_CHANNEL_MASK BIT(ADXL367_TEMP_CHANNEL_INDEX)
  194. #define ADXL367_EX_ADC_CHANNEL_MASK BIT(ADXL367_EX_ADC_CHANNEL_INDEX)
  195. static const enum adxl367_fifo_format adxl367_fifo_formats[] = {
  196. ADXL367_FIFO_FORMAT_X,
  197. ADXL367_FIFO_FORMAT_Y,
  198. ADXL367_FIFO_FORMAT_Z,
  199. ADXL367_FIFO_FORMAT_XT,
  200. ADXL367_FIFO_FORMAT_YT,
  201. ADXL367_FIFO_FORMAT_ZT,
  202. ADXL367_FIFO_FORMAT_XA,
  203. ADXL367_FIFO_FORMAT_YA,
  204. ADXL367_FIFO_FORMAT_ZA,
  205. ADXL367_FIFO_FORMAT_XYZ,
  206. ADXL367_FIFO_FORMAT_XYZT,
  207. ADXL367_FIFO_FORMAT_XYZA,
  208. };
  209. static const unsigned long adxl367_channel_masks[] = {
  210. ADXL367_X_CHANNEL_MASK,
  211. ADXL367_Y_CHANNEL_MASK,
  212. ADXL367_Z_CHANNEL_MASK,
  213. ADXL367_X_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
  214. ADXL367_Y_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
  215. ADXL367_Z_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
  216. ADXL367_X_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
  217. ADXL367_Y_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
  218. ADXL367_Z_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
  219. ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK,
  220. ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK |
  221. ADXL367_TEMP_CHANNEL_MASK,
  222. ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK |
  223. ADXL367_EX_ADC_CHANNEL_MASK,
  224. 0,
  225. };
  226. static int adxl367_set_measure_en(struct adxl367_state *st, bool en)
  227. {
  228. enum adxl367_op_mode op_mode = en ? ADXL367_OP_MEASURE
  229. : ADXL367_OP_STANDBY;
  230. int ret;
  231. ret = regmap_update_bits(st->regmap, ADXL367_REG_POWER_CTL,
  232. ADXL367_POWER_CTL_MODE_MASK,
  233. FIELD_PREP(ADXL367_POWER_CTL_MODE_MASK,
  234. op_mode));
  235. if (ret)
  236. return ret;
  237. /*
  238. * Wait for acceleration output to settle after entering
  239. * measure mode.
  240. */
  241. if (en)
  242. msleep(100);
  243. return 0;
  244. }
  245. static void adxl367_scale_act_thresholds(struct adxl367_state *st,
  246. enum adxl367_range old_range,
  247. enum adxl367_range new_range)
  248. {
  249. st->act_threshold = st->act_threshold
  250. * adxl367_range_scale_factor_tbl[old_range]
  251. / adxl367_range_scale_factor_tbl[new_range];
  252. st->inact_threshold = st->inact_threshold
  253. * adxl367_range_scale_factor_tbl[old_range]
  254. / adxl367_range_scale_factor_tbl[new_range];
  255. }
  256. static int _adxl367_set_act_threshold(struct adxl367_state *st,
  257. enum adxl367_activity_type act,
  258. unsigned int threshold)
  259. {
  260. u8 reg = adxl367_threshold_h_reg_tbl[act];
  261. int ret;
  262. if (threshold > ADXL367_THRESH_MAX)
  263. return -EINVAL;
  264. st->act_threshold_buf[0] = FIELD_PREP(ADXL367_THRESH_H_MASK,
  265. FIELD_GET(ADXL367_THRESH_VAL_H_MASK,
  266. threshold));
  267. st->act_threshold_buf[1] = FIELD_PREP(ADXL367_THRESH_L_MASK,
  268. FIELD_GET(ADXL367_THRESH_VAL_L_MASK,
  269. threshold));
  270. ret = regmap_bulk_write(st->regmap, reg, st->act_threshold_buf,
  271. sizeof(st->act_threshold_buf));
  272. if (ret)
  273. return ret;
  274. if (act == ADXL367_ACTIVITY)
  275. st->act_threshold = threshold;
  276. else
  277. st->inact_threshold = threshold;
  278. return 0;
  279. }
  280. static int adxl367_set_act_threshold(struct adxl367_state *st,
  281. enum adxl367_activity_type act,
  282. unsigned int threshold)
  283. {
  284. int ret;
  285. guard(mutex)(&st->lock);
  286. ret = adxl367_set_measure_en(st, false);
  287. if (ret)
  288. return ret;
  289. ret = _adxl367_set_act_threshold(st, act, threshold);
  290. if (ret)
  291. return ret;
  292. return adxl367_set_measure_en(st, true);
  293. }
  294. static int adxl367_set_act_proc_mode(struct adxl367_state *st,
  295. enum adxl367_act_proc_mode mode)
  296. {
  297. return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL,
  298. ADXL367_ACT_LINKLOOP_MASK,
  299. FIELD_PREP(ADXL367_ACT_LINKLOOP_MASK,
  300. mode));
  301. }
  302. static int adxl367_set_act_interrupt_en(struct adxl367_state *st,
  303. enum adxl367_activity_type act,
  304. bool en)
  305. {
  306. unsigned int mask = adxl367_act_int_mask_tbl[act];
  307. return regmap_update_bits(st->regmap, ADXL367_REG_INT1_MAP,
  308. mask, en ? mask : 0);
  309. }
  310. static int adxl367_get_act_interrupt_en(struct adxl367_state *st,
  311. enum adxl367_activity_type act,
  312. bool *en)
  313. {
  314. unsigned int mask = adxl367_act_int_mask_tbl[act];
  315. unsigned int val;
  316. int ret;
  317. ret = regmap_read(st->regmap, ADXL367_REG_INT1_MAP, &val);
  318. if (ret)
  319. return ret;
  320. *en = !!(val & mask);
  321. return 0;
  322. }
  323. static int adxl367_set_act_en(struct adxl367_state *st,
  324. enum adxl367_activity_type act,
  325. enum adxl367_act_en_mode en)
  326. {
  327. unsigned int ctl_shift = adxl367_act_en_shift_tbl[act];
  328. return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL,
  329. ADXL367_ACT_EN_MASK << ctl_shift,
  330. en << ctl_shift);
  331. }
  332. static int adxl367_set_fifo_watermark_interrupt_en(struct adxl367_state *st,
  333. bool en)
  334. {
  335. return regmap_update_bits(st->regmap, ADXL367_REG_INT1_MAP,
  336. ADXL367_INT_FIFO_WATERMARK_MASK,
  337. en ? ADXL367_INT_FIFO_WATERMARK_MASK : 0);
  338. }
  339. static int adxl367_get_fifo_mode(struct adxl367_state *st,
  340. enum adxl367_fifo_mode *fifo_mode)
  341. {
  342. unsigned int val;
  343. int ret;
  344. ret = regmap_read(st->regmap, ADXL367_REG_FIFO_CTL, &val);
  345. if (ret)
  346. return ret;
  347. *fifo_mode = FIELD_GET(ADXL367_FIFO_CTL_MODE_MASK, val);
  348. return 0;
  349. }
  350. static int adxl367_set_fifo_mode(struct adxl367_state *st,
  351. enum adxl367_fifo_mode fifo_mode)
  352. {
  353. return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
  354. ADXL367_FIFO_CTL_MODE_MASK,
  355. FIELD_PREP(ADXL367_FIFO_CTL_MODE_MASK,
  356. fifo_mode));
  357. }
  358. static int adxl367_set_fifo_format(struct adxl367_state *st,
  359. enum adxl367_fifo_format fifo_format)
  360. {
  361. return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
  362. ADXL367_FIFO_CTL_FORMAT_MASK,
  363. FIELD_PREP(ADXL367_FIFO_CTL_FORMAT_MASK,
  364. fifo_format));
  365. }
  366. static int adxl367_set_fifo_watermark(struct adxl367_state *st,
  367. unsigned int fifo_watermark)
  368. {
  369. unsigned int fifo_samples = fifo_watermark * st->fifo_set_size;
  370. unsigned int fifo_samples_h, fifo_samples_l;
  371. int ret;
  372. if (fifo_samples > ADXL367_FIFO_MAX_WATERMARK)
  373. fifo_samples = ADXL367_FIFO_MAX_WATERMARK;
  374. fifo_samples /= st->fifo_set_size;
  375. fifo_samples_h = FIELD_PREP(ADXL367_SAMPLES_H_MASK,
  376. FIELD_GET(ADXL367_SAMPLES_VAL_H_MASK,
  377. fifo_samples));
  378. fifo_samples_l = FIELD_PREP(ADXL367_SAMPLES_L_MASK,
  379. FIELD_GET(ADXL367_SAMPLES_VAL_L_MASK,
  380. fifo_samples));
  381. ret = regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
  382. ADXL367_SAMPLES_H_MASK, fifo_samples_h);
  383. if (ret)
  384. return ret;
  385. ret = regmap_update_bits(st->regmap, ADXL367_REG_FIFO_SAMPLES,
  386. ADXL367_SAMPLES_L_MASK, fifo_samples_l);
  387. if (ret)
  388. return ret;
  389. st->fifo_watermark = fifo_watermark;
  390. return 0;
  391. }
  392. static int adxl367_set_range(struct iio_dev *indio_dev,
  393. enum adxl367_range range)
  394. {
  395. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  396. struct adxl367_state *st = iio_priv(indio_dev);
  397. int ret;
  398. guard(mutex)(&st->lock);
  399. ret = adxl367_set_measure_en(st, false);
  400. if (ret)
  401. return ret;
  402. ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
  403. ADXL367_FILTER_CTL_RANGE_MASK,
  404. FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
  405. range));
  406. if (ret)
  407. return ret;
  408. adxl367_scale_act_thresholds(st, st->range, range);
  409. /* Activity thresholds depend on range */
  410. ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
  411. st->act_threshold);
  412. if (ret)
  413. return ret;
  414. ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
  415. st->inact_threshold);
  416. if (ret)
  417. return ret;
  418. ret = adxl367_set_measure_en(st, true);
  419. if (ret)
  420. return ret;
  421. st->range = range;
  422. return 0;
  423. }
  424. unreachable();
  425. }
  426. static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms)
  427. {
  428. int freq_hz = adxl367_samp_freq_tbl[st->odr][0];
  429. int freq_microhz = adxl367_samp_freq_tbl[st->odr][1];
  430. /* Scale to decihertz to prevent precision loss in 12.5Hz case. */
  431. int freq_dhz = freq_hz * 10 + freq_microhz / 100000;
  432. return DIV_ROUND_CLOSEST(ms * freq_dhz, 10000);
  433. }
  434. static int _adxl367_set_act_time_ms(struct adxl367_state *st, unsigned int ms)
  435. {
  436. unsigned int val = adxl367_time_ms_to_samples(st, ms);
  437. int ret;
  438. if (val > ADXL367_TIME_ACT_MAX)
  439. val = ADXL367_TIME_ACT_MAX;
  440. ret = regmap_write(st->regmap, ADXL367_REG_TIME_ACT, val);
  441. if (ret)
  442. return ret;
  443. st->act_time_ms = ms;
  444. return 0;
  445. }
  446. static int _adxl367_set_inact_time_ms(struct adxl367_state *st, unsigned int ms)
  447. {
  448. unsigned int val = adxl367_time_ms_to_samples(st, ms);
  449. int ret;
  450. if (val > ADXL367_TIME_INACT_MAX)
  451. val = ADXL367_TIME_INACT_MAX;
  452. st->inact_time_buf[0] = FIELD_PREP(ADXL367_TIME_INACT_H_MASK,
  453. FIELD_GET(ADXL367_TIME_INACT_VAL_H_MASK,
  454. val));
  455. st->inact_time_buf[1] = FIELD_PREP(ADXL367_TIME_INACT_L_MASK,
  456. FIELD_GET(ADXL367_TIME_INACT_VAL_L_MASK,
  457. val));
  458. ret = regmap_bulk_write(st->regmap, ADXL367_REG_TIME_INACT_H,
  459. st->inact_time_buf, sizeof(st->inact_time_buf));
  460. if (ret)
  461. return ret;
  462. st->inact_time_ms = ms;
  463. return 0;
  464. }
  465. static int adxl367_set_act_time_ms(struct adxl367_state *st,
  466. enum adxl367_activity_type act,
  467. unsigned int ms)
  468. {
  469. int ret;
  470. guard(mutex)(&st->lock);
  471. ret = adxl367_set_measure_en(st, false);
  472. if (ret)
  473. return ret;
  474. if (act == ADXL367_ACTIVITY)
  475. ret = _adxl367_set_act_time_ms(st, ms);
  476. else
  477. ret = _adxl367_set_inact_time_ms(st, ms);
  478. if (ret)
  479. return ret;
  480. return adxl367_set_measure_en(st, true);
  481. }
  482. static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
  483. {
  484. int ret;
  485. ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
  486. ADXL367_FILTER_CTL_ODR_MASK,
  487. FIELD_PREP(ADXL367_FILTER_CTL_ODR_MASK,
  488. odr));
  489. if (ret)
  490. return ret;
  491. /* Activity timers depend on ODR */
  492. ret = _adxl367_set_act_time_ms(st, st->act_time_ms);
  493. if (ret)
  494. return ret;
  495. ret = _adxl367_set_inact_time_ms(st, st->inact_time_ms);
  496. if (ret)
  497. return ret;
  498. st->odr = odr;
  499. return 0;
  500. }
  501. static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr)
  502. {
  503. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  504. struct adxl367_state *st = iio_priv(indio_dev);
  505. int ret;
  506. guard(mutex)(&st->lock);
  507. ret = adxl367_set_measure_en(st, false);
  508. if (ret)
  509. return ret;
  510. ret = _adxl367_set_odr(st, odr);
  511. if (ret)
  512. return ret;
  513. return adxl367_set_measure_en(st, true);
  514. }
  515. unreachable();
  516. }
  517. static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg,
  518. bool en)
  519. {
  520. return regmap_update_bits(st->regmap, reg, ADXL367_ADC_EN_MASK,
  521. en ? ADXL367_ADC_EN_MASK : 0);
  522. }
  523. static int adxl367_set_temp_adc_reg_en(struct adxl367_state *st,
  524. unsigned int reg, bool en)
  525. {
  526. int ret;
  527. switch (reg) {
  528. case ADXL367_REG_TEMP_DATA_H:
  529. ret = adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
  530. break;
  531. case ADXL367_REG_EX_ADC_DATA_H:
  532. ret = adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
  533. break;
  534. default:
  535. return 0;
  536. }
  537. if (ret)
  538. return ret;
  539. if (en)
  540. msleep(100);
  541. return 0;
  542. }
  543. static int adxl367_set_temp_adc_mask_en(struct adxl367_state *st,
  544. const unsigned long *active_scan_mask,
  545. bool en)
  546. {
  547. if (*active_scan_mask & ADXL367_TEMP_CHANNEL_MASK)
  548. return adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
  549. else if (*active_scan_mask & ADXL367_EX_ADC_CHANNEL_MASK)
  550. return adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
  551. return 0;
  552. }
  553. static int adxl367_find_odr(struct adxl367_state *st, int val, int val2,
  554. enum adxl367_odr *odr)
  555. {
  556. size_t size = ARRAY_SIZE(adxl367_samp_freq_tbl);
  557. int i;
  558. for (i = 0; i < size; i++)
  559. if (val == adxl367_samp_freq_tbl[i][0] &&
  560. val2 == adxl367_samp_freq_tbl[i][1])
  561. break;
  562. if (i == size)
  563. return -EINVAL;
  564. *odr = i;
  565. return 0;
  566. }
  567. static int adxl367_find_range(struct adxl367_state *st, int val, int val2,
  568. enum adxl367_range *range)
  569. {
  570. size_t size = ARRAY_SIZE(adxl367_range_scale_tbl);
  571. int i;
  572. for (i = 0; i < size; i++)
  573. if (val == adxl367_range_scale_tbl[i][0] &&
  574. val2 == adxl367_range_scale_tbl[i][1])
  575. break;
  576. if (i == size)
  577. return -EINVAL;
  578. *range = i;
  579. return 0;
  580. }
  581. static int adxl367_read_sample(struct iio_dev *indio_dev,
  582. struct iio_chan_spec const *chan,
  583. int *val)
  584. {
  585. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  586. struct adxl367_state *st = iio_priv(indio_dev);
  587. u16 sample;
  588. int ret;
  589. guard(mutex)(&st->lock);
  590. ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
  591. if (ret)
  592. return ret;
  593. ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
  594. sizeof(st->sample_buf));
  595. if (ret)
  596. return ret;
  597. sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
  598. *val = sign_extend32(sample, chan->scan_type.realbits - 1);
  599. ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
  600. if (ret)
  601. return ret;
  602. return IIO_VAL_INT;
  603. }
  604. unreachable();
  605. }
  606. static int adxl367_get_status(struct adxl367_state *st, u8 *status,
  607. u16 *fifo_entries)
  608. {
  609. int ret;
  610. /* Read STATUS, FIFO_ENT_L and FIFO_ENT_H */
  611. ret = regmap_bulk_read(st->regmap, ADXL367_REG_STATUS,
  612. st->status_buf, sizeof(st->status_buf));
  613. if (ret)
  614. return ret;
  615. st->status_buf[2] &= ADXL367_FIFO_ENT_H_MASK;
  616. *status = st->status_buf[0];
  617. *fifo_entries = get_unaligned_le16(&st->status_buf[1]);
  618. return 0;
  619. }
  620. static bool adxl367_push_event(struct iio_dev *indio_dev, u8 status)
  621. {
  622. unsigned int ev_dir;
  623. if (FIELD_GET(ADXL367_STATUS_ACT_MASK, status))
  624. ev_dir = IIO_EV_DIR_RISING;
  625. else if (FIELD_GET(ADXL367_STATUS_INACT_MASK, status))
  626. ev_dir = IIO_EV_DIR_FALLING;
  627. else
  628. return false;
  629. iio_push_event(indio_dev,
  630. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  631. IIO_EV_TYPE_THRESH, ev_dir),
  632. iio_get_time_ns(indio_dev));
  633. return true;
  634. }
  635. static bool adxl367_push_fifo_data(struct iio_dev *indio_dev, u8 status,
  636. u16 fifo_entries)
  637. {
  638. struct adxl367_state *st = iio_priv(indio_dev);
  639. int ret;
  640. int i;
  641. if (!FIELD_GET(ADXL367_STATUS_FIFO_FULL_MASK, status))
  642. return false;
  643. fifo_entries -= fifo_entries % st->fifo_set_size;
  644. ret = st->ops->read_fifo(st->context, st->fifo_buf, fifo_entries);
  645. if (ret) {
  646. dev_err(st->dev, "Failed to read FIFO: %d\n", ret);
  647. return true;
  648. }
  649. for (i = 0; i < fifo_entries; i += st->fifo_set_size)
  650. iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
  651. return true;
  652. }
  653. static irqreturn_t adxl367_irq_handler(int irq, void *private)
  654. {
  655. struct iio_dev *indio_dev = private;
  656. struct adxl367_state *st = iio_priv(indio_dev);
  657. u16 fifo_entries;
  658. bool handled;
  659. u8 status;
  660. int ret;
  661. ret = adxl367_get_status(st, &status, &fifo_entries);
  662. if (ret)
  663. return IRQ_NONE;
  664. handled = adxl367_push_event(indio_dev, status);
  665. handled |= adxl367_push_fifo_data(indio_dev, status, fifo_entries);
  666. return handled ? IRQ_HANDLED : IRQ_NONE;
  667. }
  668. static int adxl367_reg_access(struct iio_dev *indio_dev,
  669. unsigned int reg,
  670. unsigned int writeval,
  671. unsigned int *readval)
  672. {
  673. struct adxl367_state *st = iio_priv(indio_dev);
  674. if (readval)
  675. return regmap_read(st->regmap, reg, readval);
  676. else
  677. return regmap_write(st->regmap, reg, writeval);
  678. }
  679. static int adxl367_read_raw(struct iio_dev *indio_dev,
  680. struct iio_chan_spec const *chan,
  681. int *val, int *val2, long info)
  682. {
  683. struct adxl367_state *st = iio_priv(indio_dev);
  684. switch (info) {
  685. case IIO_CHAN_INFO_RAW:
  686. return adxl367_read_sample(indio_dev, chan, val);
  687. case IIO_CHAN_INFO_SCALE:
  688. switch (chan->type) {
  689. case IIO_ACCEL: {
  690. guard(mutex)(&st->lock);
  691. *val = adxl367_range_scale_tbl[st->range][0];
  692. *val2 = adxl367_range_scale_tbl[st->range][1];
  693. return IIO_VAL_INT_PLUS_NANO;
  694. }
  695. case IIO_TEMP:
  696. *val = 1000;
  697. *val2 = ADXL367_TEMP_PER_C;
  698. return IIO_VAL_FRACTIONAL;
  699. case IIO_VOLTAGE:
  700. *val = ADXL367_VOLTAGE_MAX_MV;
  701. *val2 = ADXL367_VOLTAGE_MAX_RAW;
  702. return IIO_VAL_FRACTIONAL;
  703. default:
  704. return -EINVAL;
  705. }
  706. case IIO_CHAN_INFO_OFFSET:
  707. switch (chan->type) {
  708. case IIO_TEMP:
  709. *val = 25 * ADXL367_TEMP_PER_C - ADXL367_TEMP_25C;
  710. return IIO_VAL_INT;
  711. case IIO_VOLTAGE:
  712. *val = ADXL367_VOLTAGE_OFFSET;
  713. return IIO_VAL_INT;
  714. default:
  715. return -EINVAL;
  716. }
  717. case IIO_CHAN_INFO_SAMP_FREQ: {
  718. guard(mutex)(&st->lock);
  719. *val = adxl367_samp_freq_tbl[st->odr][0];
  720. *val2 = adxl367_samp_freq_tbl[st->odr][1];
  721. return IIO_VAL_INT_PLUS_MICRO;
  722. }
  723. default:
  724. return -EINVAL;
  725. }
  726. }
  727. static int adxl367_write_raw(struct iio_dev *indio_dev,
  728. struct iio_chan_spec const *chan,
  729. int val, int val2, long info)
  730. {
  731. struct adxl367_state *st = iio_priv(indio_dev);
  732. int ret;
  733. switch (info) {
  734. case IIO_CHAN_INFO_SAMP_FREQ: {
  735. enum adxl367_odr odr;
  736. ret = adxl367_find_odr(st, val, val2, &odr);
  737. if (ret)
  738. return ret;
  739. return adxl367_set_odr(indio_dev, odr);
  740. }
  741. case IIO_CHAN_INFO_SCALE: {
  742. enum adxl367_range range;
  743. ret = adxl367_find_range(st, val, val2, &range);
  744. if (ret)
  745. return ret;
  746. return adxl367_set_range(indio_dev, range);
  747. }
  748. default:
  749. return -EINVAL;
  750. }
  751. }
  752. static int adxl367_write_raw_get_fmt(struct iio_dev *indio_dev,
  753. struct iio_chan_spec const *chan,
  754. long info)
  755. {
  756. switch (info) {
  757. case IIO_CHAN_INFO_SCALE:
  758. if (chan->type != IIO_ACCEL)
  759. return -EINVAL;
  760. return IIO_VAL_INT_PLUS_NANO;
  761. default:
  762. return IIO_VAL_INT_PLUS_MICRO;
  763. }
  764. }
  765. static int adxl367_read_avail(struct iio_dev *indio_dev,
  766. struct iio_chan_spec const *chan,
  767. const int **vals, int *type, int *length,
  768. long info)
  769. {
  770. switch (info) {
  771. case IIO_CHAN_INFO_SCALE:
  772. if (chan->type != IIO_ACCEL)
  773. return -EINVAL;
  774. *vals = (int *)adxl367_range_scale_tbl;
  775. *type = IIO_VAL_INT_PLUS_NANO;
  776. *length = ARRAY_SIZE(adxl367_range_scale_tbl) * 2;
  777. return IIO_AVAIL_LIST;
  778. case IIO_CHAN_INFO_SAMP_FREQ:
  779. *vals = (int *)adxl367_samp_freq_tbl;
  780. *type = IIO_VAL_INT_PLUS_MICRO;
  781. *length = ARRAY_SIZE(adxl367_samp_freq_tbl) * 2;
  782. return IIO_AVAIL_LIST;
  783. default:
  784. return -EINVAL;
  785. }
  786. }
  787. static int adxl367_read_event_value(struct iio_dev *indio_dev,
  788. const struct iio_chan_spec *chan,
  789. enum iio_event_type type,
  790. enum iio_event_direction dir,
  791. enum iio_event_info info,
  792. int *val, int *val2)
  793. {
  794. struct adxl367_state *st = iio_priv(indio_dev);
  795. guard(mutex)(&st->lock);
  796. switch (info) {
  797. case IIO_EV_INFO_VALUE: {
  798. switch (dir) {
  799. case IIO_EV_DIR_RISING:
  800. *val = st->act_threshold;
  801. return IIO_VAL_INT;
  802. case IIO_EV_DIR_FALLING:
  803. *val = st->inact_threshold;
  804. return IIO_VAL_INT;
  805. default:
  806. return -EINVAL;
  807. }
  808. }
  809. case IIO_EV_INFO_PERIOD:
  810. switch (dir) {
  811. case IIO_EV_DIR_RISING:
  812. *val = st->act_time_ms;
  813. *val2 = 1000;
  814. return IIO_VAL_FRACTIONAL;
  815. case IIO_EV_DIR_FALLING:
  816. *val = st->inact_time_ms;
  817. *val2 = 1000;
  818. return IIO_VAL_FRACTIONAL;
  819. default:
  820. return -EINVAL;
  821. }
  822. default:
  823. return -EINVAL;
  824. }
  825. }
  826. static int adxl367_write_event_value(struct iio_dev *indio_dev,
  827. const struct iio_chan_spec *chan,
  828. enum iio_event_type type,
  829. enum iio_event_direction dir,
  830. enum iio_event_info info,
  831. int val, int val2)
  832. {
  833. struct adxl367_state *st = iio_priv(indio_dev);
  834. switch (info) {
  835. case IIO_EV_INFO_VALUE:
  836. if (val < 0)
  837. return -EINVAL;
  838. switch (dir) {
  839. case IIO_EV_DIR_RISING:
  840. return adxl367_set_act_threshold(st, ADXL367_ACTIVITY, val);
  841. case IIO_EV_DIR_FALLING:
  842. return adxl367_set_act_threshold(st, ADXL367_INACTIVITY, val);
  843. default:
  844. return -EINVAL;
  845. }
  846. case IIO_EV_INFO_PERIOD:
  847. if (val < 0)
  848. return -EINVAL;
  849. val = val * 1000 + DIV_ROUND_UP(val2, 1000);
  850. switch (dir) {
  851. case IIO_EV_DIR_RISING:
  852. return adxl367_set_act_time_ms(st, ADXL367_ACTIVITY, val);
  853. case IIO_EV_DIR_FALLING:
  854. return adxl367_set_act_time_ms(st, ADXL367_INACTIVITY, val);
  855. default:
  856. return -EINVAL;
  857. }
  858. default:
  859. return -EINVAL;
  860. }
  861. }
  862. static int adxl367_read_event_config(struct iio_dev *indio_dev,
  863. const struct iio_chan_spec *chan,
  864. enum iio_event_type type,
  865. enum iio_event_direction dir)
  866. {
  867. struct adxl367_state *st = iio_priv(indio_dev);
  868. bool en;
  869. int ret;
  870. switch (dir) {
  871. case IIO_EV_DIR_RISING:
  872. ret = adxl367_get_act_interrupt_en(st, ADXL367_ACTIVITY, &en);
  873. return ret ?: en;
  874. case IIO_EV_DIR_FALLING:
  875. ret = adxl367_get_act_interrupt_en(st, ADXL367_INACTIVITY, &en);
  876. return ret ?: en;
  877. default:
  878. return -EINVAL;
  879. }
  880. }
  881. static int adxl367_write_event_config(struct iio_dev *indio_dev,
  882. const struct iio_chan_spec *chan,
  883. enum iio_event_type type,
  884. enum iio_event_direction dir,
  885. int state)
  886. {
  887. enum adxl367_activity_type act;
  888. switch (dir) {
  889. case IIO_EV_DIR_RISING:
  890. act = ADXL367_ACTIVITY;
  891. break;
  892. case IIO_EV_DIR_FALLING:
  893. act = ADXL367_INACTIVITY;
  894. break;
  895. default:
  896. return -EINVAL;
  897. }
  898. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  899. struct adxl367_state *st = iio_priv(indio_dev);
  900. int ret;
  901. guard(mutex)(&st->lock);
  902. ret = adxl367_set_measure_en(st, false);
  903. if (ret)
  904. return ret;
  905. ret = adxl367_set_act_interrupt_en(st, act, state);
  906. if (ret)
  907. return ret;
  908. ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED
  909. : ADXL367_ACT_DISABLED);
  910. if (ret)
  911. return ret;
  912. return adxl367_set_measure_en(st, true);
  913. }
  914. unreachable();
  915. }
  916. static ssize_t adxl367_get_fifo_enabled(struct device *dev,
  917. struct device_attribute *attr,
  918. char *buf)
  919. {
  920. struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
  921. enum adxl367_fifo_mode fifo_mode;
  922. int ret;
  923. ret = adxl367_get_fifo_mode(st, &fifo_mode);
  924. if (ret)
  925. return ret;
  926. return sysfs_emit(buf, "%d\n", fifo_mode != ADXL367_FIFO_MODE_DISABLED);
  927. }
  928. static ssize_t adxl367_get_fifo_watermark(struct device *dev,
  929. struct device_attribute *attr,
  930. char *buf)
  931. {
  932. struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
  933. unsigned int fifo_watermark;
  934. guard(mutex)(&st->lock);
  935. fifo_watermark = st->fifo_watermark;
  936. return sysfs_emit(buf, "%d\n", fifo_watermark);
  937. }
  938. IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1");
  939. IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max,
  940. __stringify(ADXL367_FIFO_MAX_WATERMARK));
  941. static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
  942. adxl367_get_fifo_watermark, NULL, 0);
  943. static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  944. adxl367_get_fifo_enabled, NULL, 0);
  945. static const struct iio_dev_attr *adxl367_fifo_attributes[] = {
  946. &iio_dev_attr_hwfifo_watermark_min,
  947. &iio_dev_attr_hwfifo_watermark_max,
  948. &iio_dev_attr_hwfifo_watermark,
  949. &iio_dev_attr_hwfifo_enabled,
  950. NULL,
  951. };
  952. static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val)
  953. {
  954. struct adxl367_state *st = iio_priv(indio_dev);
  955. int ret;
  956. if (val > ADXL367_FIFO_MAX_WATERMARK)
  957. return -EINVAL;
  958. guard(mutex)(&st->lock);
  959. ret = adxl367_set_measure_en(st, false);
  960. if (ret)
  961. return ret;
  962. ret = adxl367_set_fifo_watermark(st, val);
  963. if (ret)
  964. return ret;
  965. return adxl367_set_measure_en(st, true);
  966. }
  967. static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask,
  968. enum adxl367_fifo_format *fifo_format)
  969. {
  970. size_t size = ARRAY_SIZE(adxl367_fifo_formats);
  971. int i;
  972. for (i = 0; i < size; i++)
  973. if (*scan_mask == adxl367_channel_masks[i])
  974. break;
  975. if (i == size)
  976. return false;
  977. *fifo_format = adxl367_fifo_formats[i];
  978. return true;
  979. }
  980. static int adxl367_update_scan_mode(struct iio_dev *indio_dev,
  981. const unsigned long *active_scan_mask)
  982. {
  983. struct adxl367_state *st = iio_priv(indio_dev);
  984. enum adxl367_fifo_format fifo_format;
  985. int ret;
  986. if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format))
  987. return -EINVAL;
  988. guard(mutex)(&st->lock);
  989. ret = adxl367_set_measure_en(st, false);
  990. if (ret)
  991. return ret;
  992. ret = adxl367_set_fifo_format(st, fifo_format);
  993. if (ret)
  994. return ret;
  995. ret = adxl367_set_measure_en(st, true);
  996. if (ret)
  997. return ret;
  998. st->fifo_set_size = bitmap_weight(active_scan_mask,
  999. iio_get_masklength(indio_dev));
  1000. return 0;
  1001. }
  1002. static int adxl367_buffer_postenable(struct iio_dev *indio_dev)
  1003. {
  1004. struct adxl367_state *st = iio_priv(indio_dev);
  1005. int ret;
  1006. guard(mutex)(&st->lock);
  1007. ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
  1008. true);
  1009. if (ret)
  1010. return ret;
  1011. ret = adxl367_set_measure_en(st, false);
  1012. if (ret)
  1013. return ret;
  1014. ret = adxl367_set_fifo_watermark_interrupt_en(st, true);
  1015. if (ret)
  1016. return ret;
  1017. ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM);
  1018. if (ret)
  1019. return ret;
  1020. return adxl367_set_measure_en(st, true);
  1021. }
  1022. static int adxl367_buffer_predisable(struct iio_dev *indio_dev)
  1023. {
  1024. struct adxl367_state *st = iio_priv(indio_dev);
  1025. int ret;
  1026. guard(mutex)(&st->lock);
  1027. ret = adxl367_set_measure_en(st, false);
  1028. if (ret)
  1029. return ret;
  1030. ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED);
  1031. if (ret)
  1032. return ret;
  1033. ret = adxl367_set_fifo_watermark_interrupt_en(st, false);
  1034. if (ret)
  1035. return ret;
  1036. ret = adxl367_set_measure_en(st, true);
  1037. if (ret)
  1038. return ret;
  1039. return adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
  1040. false);
  1041. }
  1042. static const struct iio_buffer_setup_ops adxl367_buffer_ops = {
  1043. .postenable = adxl367_buffer_postenable,
  1044. .predisable = adxl367_buffer_predisable,
  1045. };
  1046. static const struct iio_info adxl367_info = {
  1047. .read_raw = adxl367_read_raw,
  1048. .write_raw = adxl367_write_raw,
  1049. .write_raw_get_fmt = adxl367_write_raw_get_fmt,
  1050. .read_avail = adxl367_read_avail,
  1051. .read_event_config = adxl367_read_event_config,
  1052. .write_event_config = adxl367_write_event_config,
  1053. .read_event_value = adxl367_read_event_value,
  1054. .write_event_value = adxl367_write_event_value,
  1055. .debugfs_reg_access = adxl367_reg_access,
  1056. .hwfifo_set_watermark = adxl367_set_watermark,
  1057. .update_scan_mode = adxl367_update_scan_mode,
  1058. };
  1059. static const struct iio_event_spec adxl367_events[] = {
  1060. {
  1061. .type = IIO_EV_TYPE_MAG_REFERENCED,
  1062. .dir = IIO_EV_DIR_RISING,
  1063. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1064. BIT(IIO_EV_INFO_PERIOD) |
  1065. BIT(IIO_EV_INFO_VALUE),
  1066. },
  1067. {
  1068. .type = IIO_EV_TYPE_MAG_REFERENCED,
  1069. .dir = IIO_EV_DIR_FALLING,
  1070. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1071. BIT(IIO_EV_INFO_PERIOD) |
  1072. BIT(IIO_EV_INFO_VALUE),
  1073. },
  1074. };
  1075. #define ADXL367_ACCEL_CHANNEL(index, reg, axis) { \
  1076. .type = IIO_ACCEL, \
  1077. .address = (reg), \
  1078. .modified = 1, \
  1079. .channel2 = IIO_MOD_##axis, \
  1080. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  1081. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  1082. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
  1083. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1084. .info_mask_shared_by_all_available = \
  1085. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1086. .event_spec = adxl367_events, \
  1087. .num_event_specs = ARRAY_SIZE(adxl367_events), \
  1088. .scan_index = (index), \
  1089. .scan_type = { \
  1090. .sign = 's', \
  1091. .realbits = 14, \
  1092. .storagebits = 16, \
  1093. .endianness = IIO_BE, \
  1094. }, \
  1095. }
  1096. #define ADXL367_CHANNEL(index, reg, _type) { \
  1097. .type = (_type), \
  1098. .address = (reg), \
  1099. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  1100. BIT(IIO_CHAN_INFO_OFFSET) | \
  1101. BIT(IIO_CHAN_INFO_SCALE), \
  1102. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1103. .scan_index = (index), \
  1104. .scan_type = { \
  1105. .sign = 's', \
  1106. .realbits = 14, \
  1107. .storagebits = 16, \
  1108. .endianness = IIO_BE, \
  1109. }, \
  1110. }
  1111. static const struct iio_chan_spec adxl367_channels[] = {
  1112. ADXL367_ACCEL_CHANNEL(ADXL367_X_CHANNEL_INDEX, ADXL367_REG_X_DATA_H, X),
  1113. ADXL367_ACCEL_CHANNEL(ADXL367_Y_CHANNEL_INDEX, ADXL367_REG_Y_DATA_H, Y),
  1114. ADXL367_ACCEL_CHANNEL(ADXL367_Z_CHANNEL_INDEX, ADXL367_REG_Z_DATA_H, Z),
  1115. ADXL367_CHANNEL(ADXL367_TEMP_CHANNEL_INDEX, ADXL367_REG_TEMP_DATA_H,
  1116. IIO_TEMP),
  1117. ADXL367_CHANNEL(ADXL367_EX_ADC_CHANNEL_INDEX, ADXL367_REG_EX_ADC_DATA_H,
  1118. IIO_VOLTAGE),
  1119. };
  1120. static int adxl367_verify_devid(struct adxl367_state *st)
  1121. {
  1122. unsigned int val;
  1123. int ret;
  1124. ret = regmap_read(st->regmap, ADXL367_REG_DEVID, &val);
  1125. if (ret)
  1126. return dev_err_probe(st->dev, ret, "Failed to read dev id\n");
  1127. if (val != ADXL367_DEVID_AD)
  1128. return dev_err_probe(st->dev, -ENODEV,
  1129. "Invalid dev id 0x%02X, expected 0x%02X\n",
  1130. val, ADXL367_DEVID_AD);
  1131. return 0;
  1132. }
  1133. static int adxl367_setup(struct adxl367_state *st)
  1134. {
  1135. int ret;
  1136. ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
  1137. ADXL367_2G_RANGE_1G);
  1138. if (ret)
  1139. return ret;
  1140. ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
  1141. ADXL367_2G_RANGE_100MG);
  1142. if (ret)
  1143. return ret;
  1144. ret = adxl367_set_act_proc_mode(st, ADXL367_LOOPED);
  1145. if (ret)
  1146. return ret;
  1147. ret = _adxl367_set_odr(st, ADXL367_ODR_400HZ);
  1148. if (ret)
  1149. return ret;
  1150. ret = _adxl367_set_act_time_ms(st, 10);
  1151. if (ret)
  1152. return ret;
  1153. ret = _adxl367_set_inact_time_ms(st, 10000);
  1154. if (ret)
  1155. return ret;
  1156. return adxl367_set_measure_en(st, true);
  1157. }
  1158. int adxl367_probe(struct device *dev, const struct adxl367_ops *ops,
  1159. void *context, struct regmap *regmap, int irq)
  1160. {
  1161. static const char * const regulator_names[] = { "vdd", "vddio" };
  1162. struct iio_dev *indio_dev;
  1163. struct adxl367_state *st;
  1164. int ret;
  1165. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1166. if (!indio_dev)
  1167. return -ENOMEM;
  1168. st = iio_priv(indio_dev);
  1169. st->dev = dev;
  1170. st->regmap = regmap;
  1171. st->context = context;
  1172. st->ops = ops;
  1173. mutex_init(&st->lock);
  1174. indio_dev->channels = adxl367_channels;
  1175. indio_dev->num_channels = ARRAY_SIZE(adxl367_channels);
  1176. indio_dev->available_scan_masks = adxl367_channel_masks;
  1177. indio_dev->name = "adxl367";
  1178. indio_dev->info = &adxl367_info;
  1179. indio_dev->modes = INDIO_DIRECT_MODE;
  1180. ret = devm_regulator_bulk_get_enable(st->dev,
  1181. ARRAY_SIZE(regulator_names),
  1182. regulator_names);
  1183. if (ret)
  1184. return dev_err_probe(st->dev, ret,
  1185. "Failed to get regulators\n");
  1186. ret = regmap_write(st->regmap, ADXL367_REG_RESET, ADXL367_RESET_CODE);
  1187. if (ret)
  1188. return ret;
  1189. fsleep(15000);
  1190. ret = adxl367_verify_devid(st);
  1191. if (ret)
  1192. return ret;
  1193. ret = adxl367_setup(st);
  1194. if (ret)
  1195. return ret;
  1196. ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev,
  1197. &adxl367_buffer_ops,
  1198. adxl367_fifo_attributes);
  1199. if (ret)
  1200. return ret;
  1201. ret = devm_request_threaded_irq(st->dev, irq, NULL,
  1202. adxl367_irq_handler, IRQF_ONESHOT,
  1203. indio_dev->name, indio_dev);
  1204. if (ret)
  1205. return dev_err_probe(st->dev, ret, "Failed to request irq\n");
  1206. return devm_iio_device_register(dev, indio_dev);
  1207. }
  1208. EXPORT_SYMBOL_NS_GPL(adxl367_probe, IIO_ADXL367);
  1209. MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
  1210. MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer driver");
  1211. MODULE_LICENSE("GPL");