adxl355_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADXL355 3-Axis Digital Accelerometer IIO core driver
  4. *
  5. * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
  6. *
  7. * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/iio/buffer.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/trigger.h>
  14. #include <linux/iio/triggered_buffer.h>
  15. #include <linux/iio/trigger_consumer.h>
  16. #include <linux/limits.h>
  17. #include <linux/math64.h>
  18. #include <linux/module.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/property.h>
  21. #include <linux/regmap.h>
  22. #include <linux/units.h>
  23. #include <linux/unaligned.h>
  24. #include "adxl355.h"
  25. /* ADXL355 Register Definitions */
  26. #define ADXL355_DEVID_AD_REG 0x00
  27. #define ADXL355_DEVID_MST_REG 0x01
  28. #define ADXL355_PARTID_REG 0x02
  29. #define ADXL355_STATUS_REG 0x04
  30. #define ADXL355_FIFO_ENTRIES_REG 0x05
  31. #define ADXL355_TEMP2_REG 0x06
  32. #define ADXL355_XDATA3_REG 0x08
  33. #define ADXL355_YDATA3_REG 0x0B
  34. #define ADXL355_ZDATA3_REG 0x0E
  35. #define ADXL355_FIFO_DATA_REG 0x11
  36. #define ADXL355_OFFSET_X_H_REG 0x1E
  37. #define ADXL355_OFFSET_Y_H_REG 0x20
  38. #define ADXL355_OFFSET_Z_H_REG 0x22
  39. #define ADXL355_ACT_EN_REG 0x24
  40. #define ADXL355_ACT_THRESH_H_REG 0x25
  41. #define ADXL355_ACT_THRESH_L_REG 0x26
  42. #define ADXL355_ACT_COUNT_REG 0x27
  43. #define ADXL355_FILTER_REG 0x28
  44. #define ADXL355_FILTER_ODR_MSK GENMASK(3, 0)
  45. #define ADXL355_FILTER_HPF_MSK GENMASK(6, 4)
  46. #define ADXL355_FIFO_SAMPLES_REG 0x29
  47. #define ADXL355_INT_MAP_REG 0x2A
  48. #define ADXL355_SYNC_REG 0x2B
  49. #define ADXL355_RANGE_REG 0x2C
  50. #define ADXL355_POWER_CTL_REG 0x2D
  51. #define ADXL355_POWER_CTL_MODE_MSK GENMASK(1, 0)
  52. #define ADXL355_POWER_CTL_DRDY_MSK BIT(2)
  53. #define ADXL355_SELF_TEST_REG 0x2E
  54. #define ADXL355_RESET_REG 0x2F
  55. #define ADXL355_DEVID_AD_VAL 0xAD
  56. #define ADXL355_DEVID_MST_VAL 0x1D
  57. #define ADXL355_PARTID_VAL 0xED
  58. #define ADXL359_PARTID_VAL 0xE9
  59. #define ADXL355_RESET_CODE 0x52
  60. static const struct regmap_range adxl355_read_reg_range[] = {
  61. regmap_reg_range(ADXL355_DEVID_AD_REG, ADXL355_FIFO_DATA_REG),
  62. regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_SELF_TEST_REG),
  63. };
  64. const struct regmap_access_table adxl355_readable_regs_tbl = {
  65. .yes_ranges = adxl355_read_reg_range,
  66. .n_yes_ranges = ARRAY_SIZE(adxl355_read_reg_range),
  67. };
  68. EXPORT_SYMBOL_NS_GPL(adxl355_readable_regs_tbl, IIO_ADXL355);
  69. static const struct regmap_range adxl355_write_reg_range[] = {
  70. regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_RESET_REG),
  71. };
  72. const struct regmap_access_table adxl355_writeable_regs_tbl = {
  73. .yes_ranges = adxl355_write_reg_range,
  74. .n_yes_ranges = ARRAY_SIZE(adxl355_write_reg_range),
  75. };
  76. EXPORT_SYMBOL_NS_GPL(adxl355_writeable_regs_tbl, IIO_ADXL355);
  77. const struct adxl355_chip_info adxl35x_chip_info[] = {
  78. [ADXL355] = {
  79. .name = "adxl355",
  80. .part_id = ADXL355_PARTID_VAL,
  81. /*
  82. * At +/- 2g with 20-bit resolution, scale is given in datasheet
  83. * as 3.9ug/LSB = 0.0000039 * 9.80665 = 0.00003824593 m/s^2.
  84. */
  85. .accel_scale = {
  86. .integer = 0,
  87. .decimal = 38245,
  88. },
  89. /*
  90. * The datasheet defines an intercept of 1885 LSB at 25 degC
  91. * and a slope of -9.05 LSB/C. The following formula can be used
  92. * to find the temperature:
  93. * Temp = ((RAW - 1885)/(-9.05)) + 25 but this doesn't follow
  94. * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE.
  95. * Hence using some rearranging we get the scale as -110.497238
  96. * and offset as -2111.25.
  97. */
  98. .temp_offset = {
  99. .integer = -2111,
  100. .decimal = 250000,
  101. },
  102. },
  103. [ADXL359] = {
  104. .name = "adxl359",
  105. .part_id = ADXL359_PARTID_VAL,
  106. /*
  107. * At +/- 10g with 20-bit resolution, scale is given in datasheet
  108. * as 19.5ug/LSB = 0.0000195 * 9.80665 = 0.0.00019122967 m/s^2.
  109. */
  110. .accel_scale = {
  111. .integer = 0,
  112. .decimal = 191229,
  113. },
  114. /*
  115. * The datasheet defines an intercept of 1852 LSB at 25 degC
  116. * and a slope of -9.05 LSB/C. The following formula can be used
  117. * to find the temperature:
  118. * Temp = ((RAW - 1852)/(-9.05)) + 25 but this doesn't follow
  119. * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE.
  120. * Hence using some rearranging we get the scale as -110.497238
  121. * and offset as -2079.25.
  122. */
  123. .temp_offset = {
  124. .integer = -2079,
  125. .decimal = 250000,
  126. },
  127. },
  128. };
  129. EXPORT_SYMBOL_NS_GPL(adxl35x_chip_info, IIO_ADXL355);
  130. enum adxl355_op_mode {
  131. ADXL355_MEASUREMENT,
  132. ADXL355_STANDBY,
  133. ADXL355_TEMP_OFF,
  134. };
  135. enum adxl355_odr {
  136. ADXL355_ODR_4000HZ,
  137. ADXL355_ODR_2000HZ,
  138. ADXL355_ODR_1000HZ,
  139. ADXL355_ODR_500HZ,
  140. ADXL355_ODR_250HZ,
  141. ADXL355_ODR_125HZ,
  142. ADXL355_ODR_62_5HZ,
  143. ADXL355_ODR_31_25HZ,
  144. ADXL355_ODR_15_625HZ,
  145. ADXL355_ODR_7_813HZ,
  146. ADXL355_ODR_3_906HZ,
  147. };
  148. enum adxl355_hpf_3db {
  149. ADXL355_HPF_OFF,
  150. ADXL355_HPF_24_7,
  151. ADXL355_HPF_6_2084,
  152. ADXL355_HPF_1_5545,
  153. ADXL355_HPF_0_3862,
  154. ADXL355_HPF_0_0954,
  155. ADXL355_HPF_0_0238,
  156. };
  157. static const int adxl355_odr_table[][2] = {
  158. [0] = {4000, 0},
  159. [1] = {2000, 0},
  160. [2] = {1000, 0},
  161. [3] = {500, 0},
  162. [4] = {250, 0},
  163. [5] = {125, 0},
  164. [6] = {62, 500000},
  165. [7] = {31, 250000},
  166. [8] = {15, 625000},
  167. [9] = {7, 813000},
  168. [10] = {3, 906000},
  169. };
  170. static const int adxl355_hpf_3db_multipliers[] = {
  171. 0,
  172. 247000,
  173. 62084,
  174. 15545,
  175. 3862,
  176. 954,
  177. 238,
  178. };
  179. enum adxl355_chans {
  180. chan_x, chan_y, chan_z,
  181. };
  182. struct adxl355_chan_info {
  183. u8 data_reg;
  184. u8 offset_reg;
  185. };
  186. static const struct adxl355_chan_info adxl355_chans[] = {
  187. [chan_x] = {
  188. .data_reg = ADXL355_XDATA3_REG,
  189. .offset_reg = ADXL355_OFFSET_X_H_REG
  190. },
  191. [chan_y] = {
  192. .data_reg = ADXL355_YDATA3_REG,
  193. .offset_reg = ADXL355_OFFSET_Y_H_REG
  194. },
  195. [chan_z] = {
  196. .data_reg = ADXL355_ZDATA3_REG,
  197. .offset_reg = ADXL355_OFFSET_Z_H_REG
  198. },
  199. };
  200. struct adxl355_data {
  201. const struct adxl355_chip_info *chip_info;
  202. struct regmap *regmap;
  203. struct device *dev;
  204. struct mutex lock; /* lock to protect op_mode */
  205. enum adxl355_op_mode op_mode;
  206. enum adxl355_odr odr;
  207. enum adxl355_hpf_3db hpf_3db;
  208. int calibbias[3];
  209. int adxl355_hpf_3db_table[7][2];
  210. struct iio_trigger *dready_trig;
  211. union {
  212. u8 transf_buf[3];
  213. struct {
  214. u8 buf[14];
  215. s64 ts;
  216. } buffer;
  217. } __aligned(IIO_DMA_MINALIGN);
  218. };
  219. static int adxl355_set_op_mode(struct adxl355_data *data,
  220. enum adxl355_op_mode op_mode)
  221. {
  222. int ret;
  223. if (data->op_mode == op_mode)
  224. return 0;
  225. ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
  226. ADXL355_POWER_CTL_MODE_MSK, op_mode);
  227. if (ret)
  228. return ret;
  229. data->op_mode = op_mode;
  230. return ret;
  231. }
  232. static int adxl355_data_rdy_trigger_set_state(struct iio_trigger *trig,
  233. bool state)
  234. {
  235. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  236. struct adxl355_data *data = iio_priv(indio_dev);
  237. int ret;
  238. mutex_lock(&data->lock);
  239. ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
  240. ADXL355_POWER_CTL_DRDY_MSK,
  241. FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK,
  242. state ? 0 : 1));
  243. mutex_unlock(&data->lock);
  244. return ret;
  245. }
  246. static void adxl355_fill_3db_frequency_table(struct adxl355_data *data)
  247. {
  248. u32 multiplier;
  249. u64 div, rem;
  250. u64 odr;
  251. int i;
  252. odr = mul_u64_u32_shr(adxl355_odr_table[data->odr][0], MEGA, 0) +
  253. adxl355_odr_table[data->odr][1];
  254. for (i = 0; i < ARRAY_SIZE(adxl355_hpf_3db_multipliers); i++) {
  255. multiplier = adxl355_hpf_3db_multipliers[i];
  256. div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0),
  257. TERA * 100, &rem);
  258. data->adxl355_hpf_3db_table[i][0] = div;
  259. data->adxl355_hpf_3db_table[i][1] = div_u64(rem, MEGA * 100);
  260. }
  261. }
  262. static int adxl355_setup(struct adxl355_data *data)
  263. {
  264. unsigned int regval;
  265. int ret;
  266. ret = regmap_read(data->regmap, ADXL355_DEVID_AD_REG, &regval);
  267. if (ret)
  268. return ret;
  269. if (regval != ADXL355_DEVID_AD_VAL) {
  270. dev_err(data->dev, "Invalid ADI ID 0x%02x\n", regval);
  271. return -ENODEV;
  272. }
  273. ret = regmap_read(data->regmap, ADXL355_DEVID_MST_REG, &regval);
  274. if (ret)
  275. return ret;
  276. if (regval != ADXL355_DEVID_MST_VAL) {
  277. dev_err(data->dev, "Invalid MEMS ID 0x%02x\n", regval);
  278. return -ENODEV;
  279. }
  280. ret = regmap_read(data->regmap, ADXL355_PARTID_REG, &regval);
  281. if (ret)
  282. return ret;
  283. if (regval != ADXL355_PARTID_VAL)
  284. dev_warn(data->dev, "Invalid DEV ID 0x%02x\n", regval);
  285. /*
  286. * Perform a software reset to make sure the device is in a consistent
  287. * state after start-up.
  288. */
  289. ret = regmap_write(data->regmap, ADXL355_RESET_REG, ADXL355_RESET_CODE);
  290. if (ret)
  291. return ret;
  292. ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
  293. ADXL355_POWER_CTL_DRDY_MSK,
  294. FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK, 1));
  295. if (ret)
  296. return ret;
  297. adxl355_fill_3db_frequency_table(data);
  298. return adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  299. }
  300. static int adxl355_get_temp_data(struct adxl355_data *data, u8 addr)
  301. {
  302. return regmap_bulk_read(data->regmap, addr, data->transf_buf, 2);
  303. }
  304. static int adxl355_read_axis(struct adxl355_data *data, u8 addr)
  305. {
  306. int ret;
  307. ret = regmap_bulk_read(data->regmap, addr, data->transf_buf,
  308. ARRAY_SIZE(data->transf_buf));
  309. if (ret)
  310. return ret;
  311. return get_unaligned_be24(data->transf_buf);
  312. }
  313. static int adxl355_find_match(const int (*freq_tbl)[2], const int n,
  314. const int val, const int val2)
  315. {
  316. int i;
  317. for (i = 0; i < n; i++) {
  318. if (freq_tbl[i][0] == val && freq_tbl[i][1] == val2)
  319. return i;
  320. }
  321. return -EINVAL;
  322. }
  323. static int adxl355_set_odr(struct adxl355_data *data,
  324. enum adxl355_odr odr)
  325. {
  326. int ret;
  327. mutex_lock(&data->lock);
  328. if (data->odr == odr) {
  329. mutex_unlock(&data->lock);
  330. return 0;
  331. }
  332. ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
  333. if (ret)
  334. goto err_unlock;
  335. ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
  336. ADXL355_FILTER_ODR_MSK,
  337. FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
  338. if (ret)
  339. goto err_set_opmode;
  340. data->odr = odr;
  341. adxl355_fill_3db_frequency_table(data);
  342. ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  343. if (ret)
  344. goto err_set_opmode;
  345. mutex_unlock(&data->lock);
  346. return 0;
  347. err_set_opmode:
  348. adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  349. err_unlock:
  350. mutex_unlock(&data->lock);
  351. return ret;
  352. }
  353. static int adxl355_set_hpf_3db(struct adxl355_data *data,
  354. enum adxl355_hpf_3db hpf)
  355. {
  356. int ret;
  357. mutex_lock(&data->lock);
  358. if (data->hpf_3db == hpf) {
  359. mutex_unlock(&data->lock);
  360. return 0;
  361. }
  362. ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
  363. if (ret)
  364. goto err_unlock;
  365. ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
  366. ADXL355_FILTER_HPF_MSK,
  367. FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
  368. if (ret)
  369. goto err_set_opmode;
  370. data->hpf_3db = hpf;
  371. ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  372. if (ret)
  373. goto err_set_opmode;
  374. mutex_unlock(&data->lock);
  375. return 0;
  376. err_set_opmode:
  377. adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  378. err_unlock:
  379. mutex_unlock(&data->lock);
  380. return ret;
  381. }
  382. static int adxl355_set_calibbias(struct adxl355_data *data,
  383. enum adxl355_chans chan, int calibbias)
  384. {
  385. int ret;
  386. mutex_lock(&data->lock);
  387. ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
  388. if (ret)
  389. goto err_unlock;
  390. put_unaligned_be16(calibbias, data->transf_buf);
  391. ret = regmap_bulk_write(data->regmap,
  392. adxl355_chans[chan].offset_reg,
  393. data->transf_buf, 2);
  394. if (ret)
  395. goto err_set_opmode;
  396. data->calibbias[chan] = calibbias;
  397. ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  398. if (ret)
  399. goto err_set_opmode;
  400. mutex_unlock(&data->lock);
  401. return 0;
  402. err_set_opmode:
  403. adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
  404. err_unlock:
  405. mutex_unlock(&data->lock);
  406. return ret;
  407. }
  408. static int adxl355_read_raw(struct iio_dev *indio_dev,
  409. struct iio_chan_spec const *chan,
  410. int *val, int *val2, long mask)
  411. {
  412. struct adxl355_data *data = iio_priv(indio_dev);
  413. int ret;
  414. switch (mask) {
  415. case IIO_CHAN_INFO_RAW:
  416. switch (chan->type) {
  417. case IIO_TEMP:
  418. ret = adxl355_get_temp_data(data, chan->address);
  419. if (ret < 0)
  420. return ret;
  421. *val = get_unaligned_be16(data->transf_buf);
  422. return IIO_VAL_INT;
  423. case IIO_ACCEL:
  424. ret = adxl355_read_axis(data, adxl355_chans[
  425. chan->address].data_reg);
  426. if (ret < 0)
  427. return ret;
  428. *val = sign_extend32(ret >> chan->scan_type.shift,
  429. chan->scan_type.realbits - 1);
  430. return IIO_VAL_INT;
  431. default:
  432. return -EINVAL;
  433. }
  434. case IIO_CHAN_INFO_SCALE:
  435. switch (chan->type) {
  436. case IIO_TEMP:
  437. /*
  438. * Temperature scale is -110.497238.
  439. * See the detailed explanation in adxl35x_chip_info
  440. * definition above.
  441. */
  442. *val = -110;
  443. *val2 = 497238;
  444. return IIO_VAL_INT_PLUS_MICRO;
  445. case IIO_ACCEL:
  446. *val = data->chip_info->accel_scale.integer;
  447. *val2 = data->chip_info->accel_scale.decimal;
  448. return IIO_VAL_INT_PLUS_NANO;
  449. default:
  450. return -EINVAL;
  451. }
  452. case IIO_CHAN_INFO_OFFSET:
  453. *val = data->chip_info->temp_offset.integer;
  454. *val2 = data->chip_info->temp_offset.decimal;
  455. return IIO_VAL_INT_PLUS_MICRO;
  456. case IIO_CHAN_INFO_CALIBBIAS:
  457. *val = sign_extend32(data->calibbias[chan->address], 15);
  458. return IIO_VAL_INT;
  459. case IIO_CHAN_INFO_SAMP_FREQ:
  460. *val = adxl355_odr_table[data->odr][0];
  461. *val2 = adxl355_odr_table[data->odr][1];
  462. return IIO_VAL_INT_PLUS_MICRO;
  463. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  464. *val = data->adxl355_hpf_3db_table[data->hpf_3db][0];
  465. *val2 = data->adxl355_hpf_3db_table[data->hpf_3db][1];
  466. return IIO_VAL_INT_PLUS_MICRO;
  467. default:
  468. return -EINVAL;
  469. }
  470. }
  471. static int adxl355_write_raw(struct iio_dev *indio_dev,
  472. struct iio_chan_spec const *chan,
  473. int val, int val2, long mask)
  474. {
  475. struct adxl355_data *data = iio_priv(indio_dev);
  476. int odr_idx, hpf_idx, calibbias;
  477. switch (mask) {
  478. case IIO_CHAN_INFO_SAMP_FREQ:
  479. odr_idx = adxl355_find_match(adxl355_odr_table,
  480. ARRAY_SIZE(adxl355_odr_table),
  481. val, val2);
  482. if (odr_idx < 0)
  483. return odr_idx;
  484. return adxl355_set_odr(data, odr_idx);
  485. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  486. hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table,
  487. ARRAY_SIZE(data->adxl355_hpf_3db_table),
  488. val, val2);
  489. if (hpf_idx < 0)
  490. return hpf_idx;
  491. return adxl355_set_hpf_3db(data, hpf_idx);
  492. case IIO_CHAN_INFO_CALIBBIAS:
  493. calibbias = clamp_t(int, val, S16_MIN, S16_MAX);
  494. return adxl355_set_calibbias(data, chan->address, calibbias);
  495. default:
  496. return -EINVAL;
  497. }
  498. }
  499. static int adxl355_read_avail(struct iio_dev *indio_dev,
  500. struct iio_chan_spec const *chan,
  501. const int **vals, int *type, int *length,
  502. long mask)
  503. {
  504. struct adxl355_data *data = iio_priv(indio_dev);
  505. switch (mask) {
  506. case IIO_CHAN_INFO_SAMP_FREQ:
  507. *vals = (const int *)adxl355_odr_table;
  508. *type = IIO_VAL_INT_PLUS_MICRO;
  509. /* Values are stored in a 2D matrix */
  510. *length = ARRAY_SIZE(adxl355_odr_table) * 2;
  511. return IIO_AVAIL_LIST;
  512. case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
  513. *vals = (const int *)data->adxl355_hpf_3db_table;
  514. *type = IIO_VAL_INT_PLUS_MICRO;
  515. /* Values are stored in a 2D matrix */
  516. *length = ARRAY_SIZE(data->adxl355_hpf_3db_table) * 2;
  517. return IIO_AVAIL_LIST;
  518. default:
  519. return -EINVAL;
  520. }
  521. }
  522. static const unsigned long adxl355_avail_scan_masks[] = {
  523. GENMASK(3, 0),
  524. 0
  525. };
  526. static const struct iio_info adxl355_info = {
  527. .read_raw = adxl355_read_raw,
  528. .write_raw = adxl355_write_raw,
  529. .read_avail = &adxl355_read_avail,
  530. };
  531. static const struct iio_trigger_ops adxl355_trigger_ops = {
  532. .set_trigger_state = &adxl355_data_rdy_trigger_set_state,
  533. .validate_device = &iio_trigger_validate_own_device,
  534. };
  535. static irqreturn_t adxl355_trigger_handler(int irq, void *p)
  536. {
  537. struct iio_poll_func *pf = p;
  538. struct iio_dev *indio_dev = pf->indio_dev;
  539. struct adxl355_data *data = iio_priv(indio_dev);
  540. int ret;
  541. mutex_lock(&data->lock);
  542. /*
  543. * data->buffer is used both for triggered buffer support
  544. * and read/write_raw(), hence, it has to be zeroed here before usage.
  545. */
  546. data->buffer.buf[0] = 0;
  547. /*
  548. * The acceleration data is 24 bits and big endian. It has to be saved
  549. * in 32 bits, hence, it is saved in the 2nd byte of the 4 byte buffer.
  550. * The buf array is 14 bytes as it includes 3x4=12 bytes for
  551. * accelaration data of x, y, and z axis. It also includes 2 bytes for
  552. * temperature data.
  553. */
  554. ret = regmap_bulk_read(data->regmap, ADXL355_XDATA3_REG,
  555. &data->buffer.buf[1], 3);
  556. if (ret)
  557. goto out_unlock_notify;
  558. ret = regmap_bulk_read(data->regmap, ADXL355_YDATA3_REG,
  559. &data->buffer.buf[5], 3);
  560. if (ret)
  561. goto out_unlock_notify;
  562. ret = regmap_bulk_read(data->regmap, ADXL355_ZDATA3_REG,
  563. &data->buffer.buf[9], 3);
  564. if (ret)
  565. goto out_unlock_notify;
  566. ret = regmap_bulk_read(data->regmap, ADXL355_TEMP2_REG,
  567. &data->buffer.buf[12], 2);
  568. if (ret)
  569. goto out_unlock_notify;
  570. iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
  571. pf->timestamp);
  572. out_unlock_notify:
  573. mutex_unlock(&data->lock);
  574. iio_trigger_notify_done(indio_dev->trig);
  575. return IRQ_HANDLED;
  576. }
  577. #define ADXL355_ACCEL_CHANNEL(index, reg, axis) { \
  578. .type = IIO_ACCEL, \
  579. .address = reg, \
  580. .modified = 1, \
  581. .channel2 = IIO_MOD_##axis, \
  582. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  583. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  584. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  585. BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  586. BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
  587. .info_mask_shared_by_type_available = \
  588. BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  589. BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
  590. .scan_index = index, \
  591. .scan_type = { \
  592. .sign = 's', \
  593. .realbits = 20, \
  594. .storagebits = 32, \
  595. .shift = 4, \
  596. .endianness = IIO_BE, \
  597. } \
  598. }
  599. static const struct iio_chan_spec adxl355_channels[] = {
  600. ADXL355_ACCEL_CHANNEL(0, chan_x, X),
  601. ADXL355_ACCEL_CHANNEL(1, chan_y, Y),
  602. ADXL355_ACCEL_CHANNEL(2, chan_z, Z),
  603. {
  604. .type = IIO_TEMP,
  605. .address = ADXL355_TEMP2_REG,
  606. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  607. BIT(IIO_CHAN_INFO_SCALE) |
  608. BIT(IIO_CHAN_INFO_OFFSET),
  609. .scan_index = 3,
  610. .scan_type = {
  611. .sign = 's',
  612. .realbits = 12,
  613. .storagebits = 16,
  614. .endianness = IIO_BE,
  615. },
  616. },
  617. IIO_CHAN_SOFT_TIMESTAMP(4),
  618. };
  619. static int adxl355_probe_trigger(struct iio_dev *indio_dev, int irq)
  620. {
  621. struct adxl355_data *data = iio_priv(indio_dev);
  622. int ret;
  623. data->dready_trig = devm_iio_trigger_alloc(data->dev, "%s-dev%d",
  624. indio_dev->name,
  625. iio_device_id(indio_dev));
  626. if (!data->dready_trig)
  627. return -ENOMEM;
  628. data->dready_trig->ops = &adxl355_trigger_ops;
  629. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  630. ret = devm_request_irq(data->dev, irq,
  631. &iio_trigger_generic_data_rdy_poll,
  632. IRQF_ONESHOT, "adxl355_irq", data->dready_trig);
  633. if (ret)
  634. return dev_err_probe(data->dev, ret, "request irq %d failed\n",
  635. irq);
  636. ret = devm_iio_trigger_register(data->dev, data->dready_trig);
  637. if (ret) {
  638. dev_err(data->dev, "iio trigger register failed\n");
  639. return ret;
  640. }
  641. indio_dev->trig = iio_trigger_get(data->dready_trig);
  642. return 0;
  643. }
  644. int adxl355_core_probe(struct device *dev, struct regmap *regmap,
  645. const struct adxl355_chip_info *chip_info)
  646. {
  647. struct adxl355_data *data;
  648. struct iio_dev *indio_dev;
  649. int ret;
  650. int irq;
  651. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  652. if (!indio_dev)
  653. return -ENOMEM;
  654. data = iio_priv(indio_dev);
  655. data->regmap = regmap;
  656. data->dev = dev;
  657. data->op_mode = ADXL355_STANDBY;
  658. data->chip_info = chip_info;
  659. mutex_init(&data->lock);
  660. indio_dev->name = chip_info->name;
  661. indio_dev->info = &adxl355_info;
  662. indio_dev->modes = INDIO_DIRECT_MODE;
  663. indio_dev->channels = adxl355_channels;
  664. indio_dev->num_channels = ARRAY_SIZE(adxl355_channels);
  665. indio_dev->available_scan_masks = adxl355_avail_scan_masks;
  666. ret = adxl355_setup(data);
  667. if (ret) {
  668. dev_err(dev, "ADXL355 setup failed\n");
  669. return ret;
  670. }
  671. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  672. &iio_pollfunc_store_time,
  673. &adxl355_trigger_handler, NULL);
  674. if (ret) {
  675. dev_err(dev, "iio triggered buffer setup failed\n");
  676. return ret;
  677. }
  678. irq = fwnode_irq_get_byname(dev_fwnode(dev), "DRDY");
  679. if (irq > 0) {
  680. ret = adxl355_probe_trigger(indio_dev, irq);
  681. if (ret)
  682. return ret;
  683. }
  684. return devm_iio_device_register(dev, indio_dev);
  685. }
  686. EXPORT_SYMBOL_NS_GPL(adxl355_core_probe, IIO_ADXL355);
  687. MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
  688. MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer core driver");
  689. MODULE_LICENSE("GPL v2");