adxl367.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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. struct adxl367_state *st = iio_priv(indio_dev);
  396. int ret;
  397. guard(mutex)(&st->lock);
  398. ret = adxl367_set_measure_en(st, false);
  399. if (ret)
  400. return ret;
  401. ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
  402. ADXL367_FILTER_CTL_RANGE_MASK,
  403. FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
  404. range));
  405. if (ret)
  406. return ret;
  407. adxl367_scale_act_thresholds(st, st->range, range);
  408. /* Activity thresholds depend on range */
  409. ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
  410. st->act_threshold);
  411. if (ret)
  412. return ret;
  413. ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
  414. st->inact_threshold);
  415. if (ret)
  416. return ret;
  417. ret = adxl367_set_measure_en(st, true);
  418. if (ret)
  419. return ret;
  420. st->range = range;
  421. return 0;
  422. }
  423. static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms)
  424. {
  425. int freq_hz = adxl367_samp_freq_tbl[st->odr][0];
  426. int freq_microhz = adxl367_samp_freq_tbl[st->odr][1];
  427. /* Scale to decihertz to prevent precision loss in 12.5Hz case. */
  428. int freq_dhz = freq_hz * 10 + freq_microhz / 100000;
  429. return DIV_ROUND_CLOSEST(ms * freq_dhz, 10000);
  430. }
  431. static int _adxl367_set_act_time_ms(struct adxl367_state *st, unsigned int ms)
  432. {
  433. unsigned int val = adxl367_time_ms_to_samples(st, ms);
  434. int ret;
  435. if (val > ADXL367_TIME_ACT_MAX)
  436. val = ADXL367_TIME_ACT_MAX;
  437. ret = regmap_write(st->regmap, ADXL367_REG_TIME_ACT, val);
  438. if (ret)
  439. return ret;
  440. st->act_time_ms = ms;
  441. return 0;
  442. }
  443. static int _adxl367_set_inact_time_ms(struct adxl367_state *st, unsigned int ms)
  444. {
  445. unsigned int val = adxl367_time_ms_to_samples(st, ms);
  446. int ret;
  447. if (val > ADXL367_TIME_INACT_MAX)
  448. val = ADXL367_TIME_INACT_MAX;
  449. st->inact_time_buf[0] = FIELD_PREP(ADXL367_TIME_INACT_H_MASK,
  450. FIELD_GET(ADXL367_TIME_INACT_VAL_H_MASK,
  451. val));
  452. st->inact_time_buf[1] = FIELD_PREP(ADXL367_TIME_INACT_L_MASK,
  453. FIELD_GET(ADXL367_TIME_INACT_VAL_L_MASK,
  454. val));
  455. ret = regmap_bulk_write(st->regmap, ADXL367_REG_TIME_INACT_H,
  456. st->inact_time_buf, sizeof(st->inact_time_buf));
  457. if (ret)
  458. return ret;
  459. st->inact_time_ms = ms;
  460. return 0;
  461. }
  462. static int adxl367_set_act_time_ms(struct adxl367_state *st,
  463. enum adxl367_activity_type act,
  464. unsigned int ms)
  465. {
  466. int ret;
  467. guard(mutex)(&st->lock);
  468. ret = adxl367_set_measure_en(st, false);
  469. if (ret)
  470. return ret;
  471. if (act == ADXL367_ACTIVITY)
  472. ret = _adxl367_set_act_time_ms(st, ms);
  473. else
  474. ret = _adxl367_set_inact_time_ms(st, ms);
  475. if (ret)
  476. return ret;
  477. return adxl367_set_measure_en(st, true);
  478. }
  479. static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
  480. {
  481. int ret;
  482. ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
  483. ADXL367_FILTER_CTL_ODR_MASK,
  484. FIELD_PREP(ADXL367_FILTER_CTL_ODR_MASK,
  485. odr));
  486. if (ret)
  487. return ret;
  488. st->odr = odr;
  489. /* Activity timers depend on ODR */
  490. ret = _adxl367_set_act_time_ms(st, st->act_time_ms);
  491. if (ret)
  492. return ret;
  493. return _adxl367_set_inact_time_ms(st, st->inact_time_ms);
  494. }
  495. static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr)
  496. {
  497. struct adxl367_state *st = iio_priv(indio_dev);
  498. int ret;
  499. guard(mutex)(&st->lock);
  500. ret = adxl367_set_measure_en(st, false);
  501. if (ret)
  502. return ret;
  503. ret = _adxl367_set_odr(st, odr);
  504. if (ret)
  505. return ret;
  506. return adxl367_set_measure_en(st, true);
  507. }
  508. static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg,
  509. bool en)
  510. {
  511. return regmap_update_bits(st->regmap, reg, ADXL367_ADC_EN_MASK,
  512. en ? ADXL367_ADC_EN_MASK : 0);
  513. }
  514. static int adxl367_set_temp_adc_reg_en(struct adxl367_state *st,
  515. unsigned int reg, bool en)
  516. {
  517. int ret;
  518. switch (reg) {
  519. case ADXL367_REG_TEMP_DATA_H:
  520. ret = adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
  521. break;
  522. case ADXL367_REG_EX_ADC_DATA_H:
  523. ret = adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
  524. break;
  525. default:
  526. return 0;
  527. }
  528. if (ret)
  529. return ret;
  530. if (en)
  531. msleep(100);
  532. return 0;
  533. }
  534. static int adxl367_set_temp_adc_mask_en(struct adxl367_state *st,
  535. const unsigned long *active_scan_mask,
  536. bool en)
  537. {
  538. if (*active_scan_mask & ADXL367_TEMP_CHANNEL_MASK)
  539. return adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
  540. else if (*active_scan_mask & ADXL367_EX_ADC_CHANNEL_MASK)
  541. return adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
  542. return 0;
  543. }
  544. static int adxl367_find_odr(struct adxl367_state *st, int val, int val2,
  545. enum adxl367_odr *odr)
  546. {
  547. size_t size = ARRAY_SIZE(adxl367_samp_freq_tbl);
  548. int i;
  549. for (i = 0; i < size; i++)
  550. if (val == adxl367_samp_freq_tbl[i][0] &&
  551. val2 == adxl367_samp_freq_tbl[i][1])
  552. break;
  553. if (i == size)
  554. return -EINVAL;
  555. *odr = i;
  556. return 0;
  557. }
  558. static int adxl367_find_range(struct adxl367_state *st, int val, int val2,
  559. enum adxl367_range *range)
  560. {
  561. size_t size = ARRAY_SIZE(adxl367_range_scale_tbl);
  562. int i;
  563. for (i = 0; i < size; i++)
  564. if (val == adxl367_range_scale_tbl[i][0] &&
  565. val2 == adxl367_range_scale_tbl[i][1])
  566. break;
  567. if (i == size)
  568. return -EINVAL;
  569. *range = i;
  570. return 0;
  571. }
  572. static int adxl367_read_sample(struct iio_dev *indio_dev,
  573. struct iio_chan_spec const *chan,
  574. int *val)
  575. {
  576. struct adxl367_state *st = iio_priv(indio_dev);
  577. u16 sample;
  578. int ret;
  579. guard(mutex)(&st->lock);
  580. ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
  581. if (ret)
  582. return ret;
  583. ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
  584. sizeof(st->sample_buf));
  585. if (ret)
  586. return ret;
  587. sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
  588. *val = sign_extend32(sample, chan->scan_type.realbits - 1);
  589. ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
  590. if (ret)
  591. return ret;
  592. return IIO_VAL_INT;
  593. }
  594. static int adxl367_get_status(struct adxl367_state *st, u8 *status,
  595. u16 *fifo_entries)
  596. {
  597. int ret;
  598. /* Read STATUS, FIFO_ENT_L and FIFO_ENT_H */
  599. ret = regmap_bulk_read(st->regmap, ADXL367_REG_STATUS,
  600. st->status_buf, sizeof(st->status_buf));
  601. if (ret)
  602. return ret;
  603. st->status_buf[2] &= ADXL367_FIFO_ENT_H_MASK;
  604. *status = st->status_buf[0];
  605. *fifo_entries = get_unaligned_le16(&st->status_buf[1]);
  606. return 0;
  607. }
  608. static bool adxl367_push_event(struct iio_dev *indio_dev, u8 status)
  609. {
  610. unsigned int ev_dir;
  611. if (FIELD_GET(ADXL367_STATUS_ACT_MASK, status))
  612. ev_dir = IIO_EV_DIR_RISING;
  613. else if (FIELD_GET(ADXL367_STATUS_INACT_MASK, status))
  614. ev_dir = IIO_EV_DIR_FALLING;
  615. else
  616. return false;
  617. iio_push_event(indio_dev,
  618. IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
  619. IIO_EV_TYPE_THRESH, ev_dir),
  620. iio_get_time_ns(indio_dev));
  621. return true;
  622. }
  623. static bool adxl367_push_fifo_data(struct iio_dev *indio_dev, u8 status,
  624. u16 fifo_entries)
  625. {
  626. struct adxl367_state *st = iio_priv(indio_dev);
  627. int ret;
  628. int i;
  629. if (!FIELD_GET(ADXL367_STATUS_FIFO_FULL_MASK, status))
  630. return false;
  631. fifo_entries -= fifo_entries % st->fifo_set_size;
  632. ret = st->ops->read_fifo(st->context, st->fifo_buf, fifo_entries);
  633. if (ret) {
  634. dev_err(st->dev, "Failed to read FIFO: %d\n", ret);
  635. return true;
  636. }
  637. for (i = 0; i < fifo_entries; i += st->fifo_set_size)
  638. iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
  639. return true;
  640. }
  641. static irqreturn_t adxl367_irq_handler(int irq, void *private)
  642. {
  643. struct iio_dev *indio_dev = private;
  644. struct adxl367_state *st = iio_priv(indio_dev);
  645. u16 fifo_entries;
  646. bool handled;
  647. u8 status;
  648. int ret;
  649. ret = adxl367_get_status(st, &status, &fifo_entries);
  650. if (ret)
  651. return IRQ_NONE;
  652. handled = adxl367_push_event(indio_dev, status);
  653. handled |= adxl367_push_fifo_data(indio_dev, status, fifo_entries);
  654. return handled ? IRQ_HANDLED : IRQ_NONE;
  655. }
  656. static int adxl367_reg_access(struct iio_dev *indio_dev,
  657. unsigned int reg,
  658. unsigned int writeval,
  659. unsigned int *readval)
  660. {
  661. struct adxl367_state *st = iio_priv(indio_dev);
  662. if (readval)
  663. return regmap_read(st->regmap, reg, readval);
  664. else
  665. return regmap_write(st->regmap, reg, writeval);
  666. }
  667. static int adxl367_read_raw(struct iio_dev *indio_dev,
  668. struct iio_chan_spec const *chan,
  669. int *val, int *val2, long info)
  670. {
  671. struct adxl367_state *st = iio_priv(indio_dev);
  672. int ret;
  673. switch (info) {
  674. case IIO_CHAN_INFO_RAW:
  675. if (!iio_device_claim_direct(indio_dev))
  676. return -EBUSY;
  677. ret = adxl367_read_sample(indio_dev, chan, val);
  678. iio_device_release_direct(indio_dev);
  679. return ret;
  680. case IIO_CHAN_INFO_SCALE:
  681. switch (chan->type) {
  682. case IIO_ACCEL: {
  683. guard(mutex)(&st->lock);
  684. *val = adxl367_range_scale_tbl[st->range][0];
  685. *val2 = adxl367_range_scale_tbl[st->range][1];
  686. return IIO_VAL_INT_PLUS_NANO;
  687. }
  688. case IIO_TEMP:
  689. *val = 1000;
  690. *val2 = ADXL367_TEMP_PER_C;
  691. return IIO_VAL_FRACTIONAL;
  692. case IIO_VOLTAGE:
  693. *val = ADXL367_VOLTAGE_MAX_MV;
  694. *val2 = ADXL367_VOLTAGE_MAX_RAW;
  695. return IIO_VAL_FRACTIONAL;
  696. default:
  697. return -EINVAL;
  698. }
  699. case IIO_CHAN_INFO_OFFSET:
  700. switch (chan->type) {
  701. case IIO_TEMP:
  702. *val = 25 * ADXL367_TEMP_PER_C - ADXL367_TEMP_25C;
  703. return IIO_VAL_INT;
  704. case IIO_VOLTAGE:
  705. *val = ADXL367_VOLTAGE_OFFSET;
  706. return IIO_VAL_INT;
  707. default:
  708. return -EINVAL;
  709. }
  710. case IIO_CHAN_INFO_SAMP_FREQ: {
  711. guard(mutex)(&st->lock);
  712. *val = adxl367_samp_freq_tbl[st->odr][0];
  713. *val2 = adxl367_samp_freq_tbl[st->odr][1];
  714. return IIO_VAL_INT_PLUS_MICRO;
  715. }
  716. default:
  717. return -EINVAL;
  718. }
  719. }
  720. static int adxl367_write_raw(struct iio_dev *indio_dev,
  721. struct iio_chan_spec const *chan,
  722. int val, int val2, long info)
  723. {
  724. struct adxl367_state *st = iio_priv(indio_dev);
  725. int ret;
  726. switch (info) {
  727. case IIO_CHAN_INFO_SAMP_FREQ: {
  728. enum adxl367_odr odr;
  729. ret = adxl367_find_odr(st, val, val2, &odr);
  730. if (ret)
  731. return ret;
  732. if (!iio_device_claim_direct(indio_dev))
  733. return -EBUSY;
  734. ret = adxl367_set_odr(indio_dev, odr);
  735. iio_device_release_direct(indio_dev);
  736. return ret;
  737. }
  738. case IIO_CHAN_INFO_SCALE: {
  739. enum adxl367_range range;
  740. ret = adxl367_find_range(st, val, val2, &range);
  741. if (ret)
  742. return ret;
  743. if (!iio_device_claim_direct(indio_dev))
  744. return -EBUSY;
  745. ret = adxl367_set_range(indio_dev, range);
  746. iio_device_release_direct(indio_dev);
  747. return ret;
  748. }
  749. default:
  750. return -EINVAL;
  751. }
  752. }
  753. static int adxl367_write_raw_get_fmt(struct iio_dev *indio_dev,
  754. struct iio_chan_spec const *chan,
  755. long info)
  756. {
  757. switch (info) {
  758. case IIO_CHAN_INFO_SCALE:
  759. if (chan->type != IIO_ACCEL)
  760. return -EINVAL;
  761. return IIO_VAL_INT_PLUS_NANO;
  762. default:
  763. return IIO_VAL_INT_PLUS_MICRO;
  764. }
  765. }
  766. static int adxl367_read_avail(struct iio_dev *indio_dev,
  767. struct iio_chan_spec const *chan,
  768. const int **vals, int *type, int *length,
  769. long info)
  770. {
  771. switch (info) {
  772. case IIO_CHAN_INFO_SCALE:
  773. if (chan->type != IIO_ACCEL)
  774. return -EINVAL;
  775. *vals = (int *)adxl367_range_scale_tbl;
  776. *type = IIO_VAL_INT_PLUS_NANO;
  777. *length = ARRAY_SIZE(adxl367_range_scale_tbl) * 2;
  778. return IIO_AVAIL_LIST;
  779. case IIO_CHAN_INFO_SAMP_FREQ:
  780. *vals = (int *)adxl367_samp_freq_tbl;
  781. *type = IIO_VAL_INT_PLUS_MICRO;
  782. *length = ARRAY_SIZE(adxl367_samp_freq_tbl) * 2;
  783. return IIO_AVAIL_LIST;
  784. default:
  785. return -EINVAL;
  786. }
  787. }
  788. static int adxl367_read_event_value(struct iio_dev *indio_dev,
  789. const struct iio_chan_spec *chan,
  790. enum iio_event_type type,
  791. enum iio_event_direction dir,
  792. enum iio_event_info info,
  793. int *val, int *val2)
  794. {
  795. struct adxl367_state *st = iio_priv(indio_dev);
  796. guard(mutex)(&st->lock);
  797. switch (info) {
  798. case IIO_EV_INFO_VALUE: {
  799. switch (dir) {
  800. case IIO_EV_DIR_RISING:
  801. *val = st->act_threshold;
  802. return IIO_VAL_INT;
  803. case IIO_EV_DIR_FALLING:
  804. *val = st->inact_threshold;
  805. return IIO_VAL_INT;
  806. default:
  807. return -EINVAL;
  808. }
  809. }
  810. case IIO_EV_INFO_PERIOD:
  811. switch (dir) {
  812. case IIO_EV_DIR_RISING:
  813. *val = st->act_time_ms;
  814. *val2 = 1000;
  815. return IIO_VAL_FRACTIONAL;
  816. case IIO_EV_DIR_FALLING:
  817. *val = st->inact_time_ms;
  818. *val2 = 1000;
  819. return IIO_VAL_FRACTIONAL;
  820. default:
  821. return -EINVAL;
  822. }
  823. default:
  824. return -EINVAL;
  825. }
  826. }
  827. static int adxl367_write_event_value(struct iio_dev *indio_dev,
  828. const struct iio_chan_spec *chan,
  829. enum iio_event_type type,
  830. enum iio_event_direction dir,
  831. enum iio_event_info info,
  832. int val, int val2)
  833. {
  834. struct adxl367_state *st = iio_priv(indio_dev);
  835. switch (info) {
  836. case IIO_EV_INFO_VALUE:
  837. if (val < 0)
  838. return -EINVAL;
  839. switch (dir) {
  840. case IIO_EV_DIR_RISING:
  841. return adxl367_set_act_threshold(st, ADXL367_ACTIVITY, val);
  842. case IIO_EV_DIR_FALLING:
  843. return adxl367_set_act_threshold(st, ADXL367_INACTIVITY, val);
  844. default:
  845. return -EINVAL;
  846. }
  847. case IIO_EV_INFO_PERIOD:
  848. if (val < 0)
  849. return -EINVAL;
  850. val = val * 1000 + DIV_ROUND_UP(val2, 1000);
  851. switch (dir) {
  852. case IIO_EV_DIR_RISING:
  853. return adxl367_set_act_time_ms(st, ADXL367_ACTIVITY, val);
  854. case IIO_EV_DIR_FALLING:
  855. return adxl367_set_act_time_ms(st, ADXL367_INACTIVITY, val);
  856. default:
  857. return -EINVAL;
  858. }
  859. default:
  860. return -EINVAL;
  861. }
  862. }
  863. static int adxl367_read_event_config(struct iio_dev *indio_dev,
  864. const struct iio_chan_spec *chan,
  865. enum iio_event_type type,
  866. enum iio_event_direction dir)
  867. {
  868. struct adxl367_state *st = iio_priv(indio_dev);
  869. bool en;
  870. int ret;
  871. switch (dir) {
  872. case IIO_EV_DIR_RISING:
  873. ret = adxl367_get_act_interrupt_en(st, ADXL367_ACTIVITY, &en);
  874. return ret ?: en;
  875. case IIO_EV_DIR_FALLING:
  876. ret = adxl367_get_act_interrupt_en(st, ADXL367_INACTIVITY, &en);
  877. return ret ?: en;
  878. default:
  879. return -EINVAL;
  880. }
  881. }
  882. static int __adxl367_write_event_config(struct iio_dev *indio_dev,
  883. const struct iio_chan_spec *chan,
  884. enum iio_event_type type,
  885. enum iio_event_direction dir,
  886. bool state)
  887. {
  888. struct adxl367_state *st = iio_priv(indio_dev);
  889. enum adxl367_activity_type act;
  890. int ret;
  891. switch (dir) {
  892. case IIO_EV_DIR_RISING:
  893. act = ADXL367_ACTIVITY;
  894. break;
  895. case IIO_EV_DIR_FALLING:
  896. act = ADXL367_INACTIVITY;
  897. break;
  898. default:
  899. return -EINVAL;
  900. }
  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. static int adxl367_write_event_config(struct iio_dev *indio_dev,
  915. const struct iio_chan_spec *chan,
  916. enum iio_event_type type,
  917. enum iio_event_direction dir,
  918. bool state)
  919. {
  920. int ret;
  921. if (!iio_device_claim_direct(indio_dev))
  922. return -EBUSY;
  923. ret = __adxl367_write_event_config(indio_dev, chan, type, dir, state);
  924. iio_device_release_direct(indio_dev);
  925. return ret;
  926. }
  927. static ssize_t adxl367_get_fifo_enabled(struct device *dev,
  928. struct device_attribute *attr,
  929. char *buf)
  930. {
  931. struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
  932. enum adxl367_fifo_mode fifo_mode;
  933. int ret;
  934. ret = adxl367_get_fifo_mode(st, &fifo_mode);
  935. if (ret)
  936. return ret;
  937. return sysfs_emit(buf, "%d\n", fifo_mode != ADXL367_FIFO_MODE_DISABLED);
  938. }
  939. static ssize_t adxl367_get_fifo_watermark(struct device *dev,
  940. struct device_attribute *attr,
  941. char *buf)
  942. {
  943. struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
  944. unsigned int fifo_watermark;
  945. guard(mutex)(&st->lock);
  946. fifo_watermark = st->fifo_watermark;
  947. return sysfs_emit(buf, "%d\n", fifo_watermark);
  948. }
  949. IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1");
  950. IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max,
  951. __stringify(ADXL367_FIFO_MAX_WATERMARK));
  952. static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
  953. adxl367_get_fifo_watermark, NULL, 0);
  954. static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  955. adxl367_get_fifo_enabled, NULL, 0);
  956. static const struct iio_dev_attr *adxl367_fifo_attributes[] = {
  957. &iio_dev_attr_hwfifo_watermark_min,
  958. &iio_dev_attr_hwfifo_watermark_max,
  959. &iio_dev_attr_hwfifo_watermark,
  960. &iio_dev_attr_hwfifo_enabled,
  961. NULL,
  962. };
  963. static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val)
  964. {
  965. struct adxl367_state *st = iio_priv(indio_dev);
  966. int ret;
  967. if (val > ADXL367_FIFO_MAX_WATERMARK)
  968. return -EINVAL;
  969. guard(mutex)(&st->lock);
  970. ret = adxl367_set_measure_en(st, false);
  971. if (ret)
  972. return ret;
  973. ret = adxl367_set_fifo_watermark(st, val);
  974. if (ret)
  975. return ret;
  976. return adxl367_set_measure_en(st, true);
  977. }
  978. static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask,
  979. enum adxl367_fifo_format *fifo_format)
  980. {
  981. size_t size = ARRAY_SIZE(adxl367_fifo_formats);
  982. int i;
  983. for (i = 0; i < size; i++)
  984. if (*scan_mask == adxl367_channel_masks[i])
  985. break;
  986. if (i == size)
  987. return false;
  988. *fifo_format = adxl367_fifo_formats[i];
  989. return true;
  990. }
  991. static int adxl367_update_scan_mode(struct iio_dev *indio_dev,
  992. const unsigned long *active_scan_mask)
  993. {
  994. struct adxl367_state *st = iio_priv(indio_dev);
  995. enum adxl367_fifo_format fifo_format;
  996. int ret;
  997. if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format))
  998. return -EINVAL;
  999. guard(mutex)(&st->lock);
  1000. ret = adxl367_set_measure_en(st, false);
  1001. if (ret)
  1002. return ret;
  1003. ret = adxl367_set_fifo_format(st, fifo_format);
  1004. if (ret)
  1005. return ret;
  1006. ret = adxl367_set_measure_en(st, true);
  1007. if (ret)
  1008. return ret;
  1009. st->fifo_set_size = bitmap_weight(active_scan_mask,
  1010. iio_get_masklength(indio_dev));
  1011. return 0;
  1012. }
  1013. static int adxl367_buffer_postenable(struct iio_dev *indio_dev)
  1014. {
  1015. struct adxl367_state *st = iio_priv(indio_dev);
  1016. int ret;
  1017. guard(mutex)(&st->lock);
  1018. ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
  1019. true);
  1020. if (ret)
  1021. return ret;
  1022. ret = adxl367_set_measure_en(st, false);
  1023. if (ret)
  1024. return ret;
  1025. ret = adxl367_set_fifo_watermark_interrupt_en(st, true);
  1026. if (ret)
  1027. return ret;
  1028. ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM);
  1029. if (ret)
  1030. return ret;
  1031. return adxl367_set_measure_en(st, true);
  1032. }
  1033. static int adxl367_buffer_predisable(struct iio_dev *indio_dev)
  1034. {
  1035. struct adxl367_state *st = iio_priv(indio_dev);
  1036. int ret;
  1037. guard(mutex)(&st->lock);
  1038. ret = adxl367_set_measure_en(st, false);
  1039. if (ret)
  1040. return ret;
  1041. ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED);
  1042. if (ret)
  1043. return ret;
  1044. ret = adxl367_set_fifo_watermark_interrupt_en(st, false);
  1045. if (ret)
  1046. return ret;
  1047. ret = adxl367_set_measure_en(st, true);
  1048. if (ret)
  1049. return ret;
  1050. return adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
  1051. false);
  1052. }
  1053. static const struct iio_buffer_setup_ops adxl367_buffer_ops = {
  1054. .postenable = adxl367_buffer_postenable,
  1055. .predisable = adxl367_buffer_predisable,
  1056. };
  1057. static const struct iio_info adxl367_info = {
  1058. .read_raw = adxl367_read_raw,
  1059. .write_raw = adxl367_write_raw,
  1060. .write_raw_get_fmt = adxl367_write_raw_get_fmt,
  1061. .read_avail = adxl367_read_avail,
  1062. .read_event_config = adxl367_read_event_config,
  1063. .write_event_config = adxl367_write_event_config,
  1064. .read_event_value = adxl367_read_event_value,
  1065. .write_event_value = adxl367_write_event_value,
  1066. .debugfs_reg_access = adxl367_reg_access,
  1067. .hwfifo_set_watermark = adxl367_set_watermark,
  1068. .update_scan_mode = adxl367_update_scan_mode,
  1069. };
  1070. static const struct iio_event_spec adxl367_events[] = {
  1071. {
  1072. .type = IIO_EV_TYPE_MAG_REFERENCED,
  1073. .dir = IIO_EV_DIR_RISING,
  1074. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1075. BIT(IIO_EV_INFO_PERIOD) |
  1076. BIT(IIO_EV_INFO_VALUE),
  1077. },
  1078. {
  1079. .type = IIO_EV_TYPE_MAG_REFERENCED,
  1080. .dir = IIO_EV_DIR_FALLING,
  1081. .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
  1082. BIT(IIO_EV_INFO_PERIOD) |
  1083. BIT(IIO_EV_INFO_VALUE),
  1084. },
  1085. };
  1086. #define ADXL367_ACCEL_CHANNEL(index, reg, axis) { \
  1087. .type = IIO_ACCEL, \
  1088. .address = (reg), \
  1089. .modified = 1, \
  1090. .channel2 = IIO_MOD_##axis, \
  1091. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  1092. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  1093. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
  1094. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1095. .info_mask_shared_by_all_available = \
  1096. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1097. .event_spec = adxl367_events, \
  1098. .num_event_specs = ARRAY_SIZE(adxl367_events), \
  1099. .scan_index = (index), \
  1100. .scan_type = { \
  1101. .sign = 's', \
  1102. .realbits = 14, \
  1103. .storagebits = 16, \
  1104. .endianness = IIO_BE, \
  1105. }, \
  1106. }
  1107. #define ADXL367_CHANNEL(index, reg, _type) { \
  1108. .type = (_type), \
  1109. .address = (reg), \
  1110. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  1111. BIT(IIO_CHAN_INFO_OFFSET) | \
  1112. BIT(IIO_CHAN_INFO_SCALE), \
  1113. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  1114. .scan_index = (index), \
  1115. .scan_type = { \
  1116. .sign = 's', \
  1117. .realbits = 14, \
  1118. .storagebits = 16, \
  1119. .endianness = IIO_BE, \
  1120. }, \
  1121. }
  1122. static const struct iio_chan_spec adxl367_channels[] = {
  1123. ADXL367_ACCEL_CHANNEL(ADXL367_X_CHANNEL_INDEX, ADXL367_REG_X_DATA_H, X),
  1124. ADXL367_ACCEL_CHANNEL(ADXL367_Y_CHANNEL_INDEX, ADXL367_REG_Y_DATA_H, Y),
  1125. ADXL367_ACCEL_CHANNEL(ADXL367_Z_CHANNEL_INDEX, ADXL367_REG_Z_DATA_H, Z),
  1126. ADXL367_CHANNEL(ADXL367_TEMP_CHANNEL_INDEX, ADXL367_REG_TEMP_DATA_H,
  1127. IIO_TEMP),
  1128. ADXL367_CHANNEL(ADXL367_EX_ADC_CHANNEL_INDEX, ADXL367_REG_EX_ADC_DATA_H,
  1129. IIO_VOLTAGE),
  1130. };
  1131. static int adxl367_verify_devid(struct adxl367_state *st)
  1132. {
  1133. unsigned int val;
  1134. int ret;
  1135. ret = regmap_read(st->regmap, ADXL367_REG_DEVID, &val);
  1136. if (ret)
  1137. return dev_err_probe(st->dev, ret, "Failed to read dev id\n");
  1138. if (val != ADXL367_DEVID_AD)
  1139. return dev_err_probe(st->dev, -ENODEV,
  1140. "Invalid dev id 0x%02X, expected 0x%02X\n",
  1141. val, ADXL367_DEVID_AD);
  1142. return 0;
  1143. }
  1144. static int adxl367_setup(struct adxl367_state *st)
  1145. {
  1146. int ret;
  1147. ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
  1148. ADXL367_2G_RANGE_1G);
  1149. if (ret)
  1150. return ret;
  1151. ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
  1152. ADXL367_2G_RANGE_100MG);
  1153. if (ret)
  1154. return ret;
  1155. ret = adxl367_set_act_proc_mode(st, ADXL367_LOOPED);
  1156. if (ret)
  1157. return ret;
  1158. ret = _adxl367_set_odr(st, ADXL367_ODR_400HZ);
  1159. if (ret)
  1160. return ret;
  1161. ret = _adxl367_set_act_time_ms(st, 10);
  1162. if (ret)
  1163. return ret;
  1164. ret = _adxl367_set_inact_time_ms(st, 10000);
  1165. if (ret)
  1166. return ret;
  1167. return adxl367_set_measure_en(st, true);
  1168. }
  1169. int adxl367_probe(struct device *dev, const struct adxl367_ops *ops,
  1170. void *context, struct regmap *regmap, int irq)
  1171. {
  1172. static const char * const regulator_names[] = { "vdd", "vddio" };
  1173. struct iio_dev *indio_dev;
  1174. struct adxl367_state *st;
  1175. int ret;
  1176. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1177. if (!indio_dev)
  1178. return -ENOMEM;
  1179. st = iio_priv(indio_dev);
  1180. st->dev = dev;
  1181. st->regmap = regmap;
  1182. st->context = context;
  1183. st->ops = ops;
  1184. mutex_init(&st->lock);
  1185. indio_dev->channels = adxl367_channels;
  1186. indio_dev->num_channels = ARRAY_SIZE(adxl367_channels);
  1187. indio_dev->available_scan_masks = adxl367_channel_masks;
  1188. indio_dev->name = "adxl367";
  1189. indio_dev->info = &adxl367_info;
  1190. indio_dev->modes = INDIO_DIRECT_MODE;
  1191. ret = devm_regulator_bulk_get_enable(st->dev,
  1192. ARRAY_SIZE(regulator_names),
  1193. regulator_names);
  1194. if (ret)
  1195. return dev_err_probe(st->dev, ret,
  1196. "Failed to get regulators\n");
  1197. ret = regmap_write(st->regmap, ADXL367_REG_RESET, ADXL367_RESET_CODE);
  1198. if (ret)
  1199. return ret;
  1200. fsleep(15000);
  1201. ret = adxl367_verify_devid(st);
  1202. if (ret)
  1203. return ret;
  1204. ret = adxl367_setup(st);
  1205. if (ret)
  1206. return ret;
  1207. ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev,
  1208. &adxl367_buffer_ops,
  1209. adxl367_fifo_attributes);
  1210. if (ret)
  1211. return ret;
  1212. ret = devm_request_threaded_irq(st->dev, irq, NULL,
  1213. adxl367_irq_handler, IRQF_ONESHOT,
  1214. indio_dev->name, indio_dev);
  1215. if (ret)
  1216. return dev_err_probe(st->dev, ret, "Failed to request irq\n");
  1217. return devm_iio_device_register(dev, indio_dev);
  1218. }
  1219. EXPORT_SYMBOL_NS_GPL(adxl367_probe, "IIO_ADXL367");
  1220. MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
  1221. MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer driver");
  1222. MODULE_LICENSE("GPL");