adxl367.c 36 KB

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