adis16480.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADIS16480 and similar IMUs driver
  4. *
  5. * Copyright 2012 Analog Devices Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/math.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/lcm.h>
  18. #include <linux/property.h>
  19. #include <linux/swab.h>
  20. #include <linux/crc32.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/buffer.h>
  23. #include <linux/iio/imu/adis.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/debugfs.h>
  26. #define ADIS16480_PAGE_SIZE 0x80
  27. #define ADIS16480_REG(page, reg) ((page) * ADIS16480_PAGE_SIZE + (reg))
  28. #define ADIS16480_REG_PAGE_ID 0x00 /* Same address on each page */
  29. #define ADIS16480_REG_SEQ_CNT ADIS16480_REG(0x00, 0x06)
  30. #define ADIS16480_REG_SYS_E_FLA ADIS16480_REG(0x00, 0x08)
  31. #define ADIS16480_REG_DIAG_STS ADIS16480_REG(0x00, 0x0A)
  32. #define ADIS16480_REG_ALM_STS ADIS16480_REG(0x00, 0x0C)
  33. #define ADIS16480_REG_TEMP_OUT ADIS16480_REG(0x00, 0x0E)
  34. #define ADIS16480_REG_X_GYRO_OUT ADIS16480_REG(0x00, 0x10)
  35. #define ADIS16480_REG_Y_GYRO_OUT ADIS16480_REG(0x00, 0x14)
  36. #define ADIS16480_REG_Z_GYRO_OUT ADIS16480_REG(0x00, 0x18)
  37. #define ADIS16480_REG_X_ACCEL_OUT ADIS16480_REG(0x00, 0x1C)
  38. #define ADIS16480_REG_Y_ACCEL_OUT ADIS16480_REG(0x00, 0x20)
  39. #define ADIS16480_REG_Z_ACCEL_OUT ADIS16480_REG(0x00, 0x24)
  40. #define ADIS16480_REG_X_MAGN_OUT ADIS16480_REG(0x00, 0x28)
  41. #define ADIS16480_REG_Y_MAGN_OUT ADIS16480_REG(0x00, 0x2A)
  42. #define ADIS16480_REG_Z_MAGN_OUT ADIS16480_REG(0x00, 0x2C)
  43. #define ADIS16480_REG_BAROM_OUT ADIS16480_REG(0x00, 0x2E)
  44. #define ADIS16480_REG_X_DELTAANG_OUT ADIS16480_REG(0x00, 0x40)
  45. #define ADIS16480_REG_Y_DELTAANG_OUT ADIS16480_REG(0x00, 0x44)
  46. #define ADIS16480_REG_Z_DELTAANG_OUT ADIS16480_REG(0x00, 0x48)
  47. #define ADIS16480_REG_X_DELTAVEL_OUT ADIS16480_REG(0x00, 0x4C)
  48. #define ADIS16480_REG_Y_DELTAVEL_OUT ADIS16480_REG(0x00, 0x50)
  49. #define ADIS16480_REG_Z_DELTAVEL_OUT ADIS16480_REG(0x00, 0x54)
  50. #define ADIS16480_REG_PROD_ID ADIS16480_REG(0x00, 0x7E)
  51. #define ADIS16480_REG_X_GYRO_SCALE ADIS16480_REG(0x02, 0x04)
  52. #define ADIS16480_REG_Y_GYRO_SCALE ADIS16480_REG(0x02, 0x06)
  53. #define ADIS16480_REG_Z_GYRO_SCALE ADIS16480_REG(0x02, 0x08)
  54. #define ADIS16480_REG_X_ACCEL_SCALE ADIS16480_REG(0x02, 0x0A)
  55. #define ADIS16480_REG_Y_ACCEL_SCALE ADIS16480_REG(0x02, 0x0C)
  56. #define ADIS16480_REG_Z_ACCEL_SCALE ADIS16480_REG(0x02, 0x0E)
  57. #define ADIS16480_REG_X_GYRO_BIAS ADIS16480_REG(0x02, 0x10)
  58. #define ADIS16480_REG_Y_GYRO_BIAS ADIS16480_REG(0x02, 0x14)
  59. #define ADIS16480_REG_Z_GYRO_BIAS ADIS16480_REG(0x02, 0x18)
  60. #define ADIS16480_REG_X_ACCEL_BIAS ADIS16480_REG(0x02, 0x1C)
  61. #define ADIS16480_REG_Y_ACCEL_BIAS ADIS16480_REG(0x02, 0x20)
  62. #define ADIS16480_REG_Z_ACCEL_BIAS ADIS16480_REG(0x02, 0x24)
  63. #define ADIS16480_REG_X_HARD_IRON ADIS16480_REG(0x02, 0x28)
  64. #define ADIS16480_REG_Y_HARD_IRON ADIS16480_REG(0x02, 0x2A)
  65. #define ADIS16480_REG_Z_HARD_IRON ADIS16480_REG(0x02, 0x2C)
  66. #define ADIS16480_REG_BAROM_BIAS ADIS16480_REG(0x02, 0x40)
  67. #define ADIS16480_REG_FLASH_CNT ADIS16480_REG(0x02, 0x7C)
  68. #define ADIS16480_REG_GLOB_CMD ADIS16480_REG(0x03, 0x02)
  69. #define ADIS16480_REG_FNCTIO_CTRL ADIS16480_REG(0x03, 0x06)
  70. #define ADIS16480_REG_GPIO_CTRL ADIS16480_REG(0x03, 0x08)
  71. #define ADIS16480_REG_CONFIG ADIS16480_REG(0x03, 0x0A)
  72. #define ADIS16480_REG_DEC_RATE ADIS16480_REG(0x03, 0x0C)
  73. #define ADIS16480_REG_SLP_CNT ADIS16480_REG(0x03, 0x10)
  74. #define ADIS16480_REG_FILTER_BNK0 ADIS16480_REG(0x03, 0x16)
  75. #define ADIS16480_REG_FILTER_BNK1 ADIS16480_REG(0x03, 0x18)
  76. #define ADIS16480_REG_ALM_CNFG0 ADIS16480_REG(0x03, 0x20)
  77. #define ADIS16480_REG_ALM_CNFG1 ADIS16480_REG(0x03, 0x22)
  78. #define ADIS16480_REG_ALM_CNFG2 ADIS16480_REG(0x03, 0x24)
  79. #define ADIS16480_REG_XG_ALM_MAGN ADIS16480_REG(0x03, 0x28)
  80. #define ADIS16480_REG_YG_ALM_MAGN ADIS16480_REG(0x03, 0x2A)
  81. #define ADIS16480_REG_ZG_ALM_MAGN ADIS16480_REG(0x03, 0x2C)
  82. #define ADIS16480_REG_XA_ALM_MAGN ADIS16480_REG(0x03, 0x2E)
  83. #define ADIS16480_REG_YA_ALM_MAGN ADIS16480_REG(0x03, 0x30)
  84. #define ADIS16480_REG_ZA_ALM_MAGN ADIS16480_REG(0x03, 0x32)
  85. #define ADIS16480_REG_XM_ALM_MAGN ADIS16480_REG(0x03, 0x34)
  86. #define ADIS16480_REG_YM_ALM_MAGN ADIS16480_REG(0x03, 0x36)
  87. #define ADIS16480_REG_ZM_ALM_MAGN ADIS16480_REG(0x03, 0x38)
  88. #define ADIS16480_REG_BR_ALM_MAGN ADIS16480_REG(0x03, 0x3A)
  89. #define ADIS16480_REG_FIRM_REV ADIS16480_REG(0x03, 0x78)
  90. #define ADIS16480_REG_FIRM_DM ADIS16480_REG(0x03, 0x7A)
  91. #define ADIS16480_REG_FIRM_Y ADIS16480_REG(0x03, 0x7C)
  92. /*
  93. * External clock scaling in PPS mode.
  94. * Available only for ADIS1649x devices
  95. */
  96. #define ADIS16495_REG_SYNC_SCALE ADIS16480_REG(0x03, 0x10)
  97. #define ADIS16495_REG_BURST_CMD ADIS16480_REG(0x00, 0x7C)
  98. #define ADIS16495_GYRO_ACCEL_BURST_ID 0xA5A5
  99. #define ADIS16545_DELTA_ANG_VEL_BURST_ID 0xC3C3
  100. /* total number of segments in burst */
  101. #define ADIS16495_BURST_MAX_DATA 20
  102. #define ADIS16480_REG_SERIAL_NUM ADIS16480_REG(0x04, 0x20)
  103. /* Each filter coefficent bank spans two pages */
  104. #define ADIS16480_FIR_COEF(page) (x < 60 ? ADIS16480_REG(page, (x) + 8) : \
  105. ADIS16480_REG((page) + 1, (x) - 60 + 8))
  106. #define ADIS16480_FIR_COEF_A(x) ADIS16480_FIR_COEF(0x05, (x))
  107. #define ADIS16480_FIR_COEF_B(x) ADIS16480_FIR_COEF(0x07, (x))
  108. #define ADIS16480_FIR_COEF_C(x) ADIS16480_FIR_COEF(0x09, (x))
  109. #define ADIS16480_FIR_COEF_D(x) ADIS16480_FIR_COEF(0x0B, (x))
  110. /* ADIS16480_REG_FNCTIO_CTRL */
  111. #define ADIS16480_DRDY_SEL_MSK GENMASK(1, 0)
  112. #define ADIS16480_DRDY_SEL(x) FIELD_PREP(ADIS16480_DRDY_SEL_MSK, x)
  113. #define ADIS16480_DRDY_POL_MSK BIT(2)
  114. #define ADIS16480_DRDY_POL(x) FIELD_PREP(ADIS16480_DRDY_POL_MSK, x)
  115. #define ADIS16480_DRDY_EN_MSK BIT(3)
  116. #define ADIS16480_DRDY_EN(x) FIELD_PREP(ADIS16480_DRDY_EN_MSK, x)
  117. #define ADIS16480_SYNC_SEL_MSK GENMASK(5, 4)
  118. #define ADIS16480_SYNC_SEL(x) FIELD_PREP(ADIS16480_SYNC_SEL_MSK, x)
  119. #define ADIS16480_SYNC_EN_MSK BIT(7)
  120. #define ADIS16480_SYNC_EN(x) FIELD_PREP(ADIS16480_SYNC_EN_MSK, x)
  121. #define ADIS16480_SYNC_MODE_MSK BIT(8)
  122. #define ADIS16480_SYNC_MODE(x) FIELD_PREP(ADIS16480_SYNC_MODE_MSK, x)
  123. #define ADIS16545_BURST_DATA_SEL_0_CHN_MASK GENMASK(5, 0)
  124. #define ADIS16545_BURST_DATA_SEL_1_CHN_MASK GENMASK(16, 11)
  125. #define ADIS16545_BURST_DATA_SEL_MASK BIT(8)
  126. struct adis16480_chip_info {
  127. unsigned int num_channels;
  128. const struct iio_chan_spec *channels;
  129. unsigned int gyro_max_val;
  130. unsigned int gyro_max_scale;
  131. unsigned int accel_max_val;
  132. unsigned int accel_max_scale;
  133. unsigned int temp_scale;
  134. unsigned int deltang_max_val;
  135. unsigned int deltvel_max_val;
  136. unsigned int int_clk;
  137. unsigned int max_dec_rate;
  138. const unsigned int *filter_freqs;
  139. bool has_pps_clk_mode;
  140. bool has_sleep_cnt;
  141. bool has_burst_delta_data;
  142. const struct adis_data adis_data;
  143. };
  144. enum adis16480_int_pin {
  145. ADIS16480_PIN_DIO1,
  146. ADIS16480_PIN_DIO2,
  147. ADIS16480_PIN_DIO3,
  148. ADIS16480_PIN_DIO4
  149. };
  150. enum adis16480_clock_mode {
  151. ADIS16480_CLK_SYNC,
  152. ADIS16480_CLK_PPS,
  153. ADIS16480_CLK_INT
  154. };
  155. struct adis16480 {
  156. const struct adis16480_chip_info *chip_info;
  157. struct adis adis;
  158. struct clk *ext_clk;
  159. enum adis16480_clock_mode clk_mode;
  160. unsigned int clk_freq;
  161. u16 burst_id;
  162. /* Alignment needed for the timestamp */
  163. __be16 data[ADIS16495_BURST_MAX_DATA] __aligned(8);
  164. };
  165. static const char * const adis16480_int_pin_names[4] = {
  166. [ADIS16480_PIN_DIO1] = "DIO1",
  167. [ADIS16480_PIN_DIO2] = "DIO2",
  168. [ADIS16480_PIN_DIO3] = "DIO3",
  169. [ADIS16480_PIN_DIO4] = "DIO4",
  170. };
  171. static bool low_rate_allow;
  172. module_param(low_rate_allow, bool, 0444);
  173. MODULE_PARM_DESC(low_rate_allow,
  174. "Allow IMU rates below the minimum advisable when external clk is used in PPS mode (default: N)");
  175. static ssize_t adis16480_show_firmware_revision(struct file *file,
  176. char __user *userbuf, size_t count, loff_t *ppos)
  177. {
  178. struct adis16480 *adis16480 = file->private_data;
  179. char buf[7];
  180. size_t len;
  181. u16 rev;
  182. int ret;
  183. ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_REV, &rev);
  184. if (ret)
  185. return ret;
  186. len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
  187. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  188. }
  189. static const struct file_operations adis16480_firmware_revision_fops = {
  190. .open = simple_open,
  191. .read = adis16480_show_firmware_revision,
  192. .llseek = default_llseek,
  193. .owner = THIS_MODULE,
  194. };
  195. static ssize_t adis16480_show_firmware_date(struct file *file,
  196. char __user *userbuf, size_t count, loff_t *ppos)
  197. {
  198. struct adis16480 *adis16480 = file->private_data;
  199. u16 md, year;
  200. char buf[12];
  201. size_t len;
  202. int ret;
  203. ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_Y, &year);
  204. if (ret)
  205. return ret;
  206. ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_DM, &md);
  207. if (ret)
  208. return ret;
  209. len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n",
  210. md >> 8, md & 0xff, year);
  211. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  212. }
  213. static const struct file_operations adis16480_firmware_date_fops = {
  214. .open = simple_open,
  215. .read = adis16480_show_firmware_date,
  216. .llseek = default_llseek,
  217. .owner = THIS_MODULE,
  218. };
  219. static int adis16480_show_serial_number(void *arg, u64 *val)
  220. {
  221. struct adis16480 *adis16480 = arg;
  222. u16 serial;
  223. int ret;
  224. ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_SERIAL_NUM,
  225. &serial);
  226. if (ret)
  227. return ret;
  228. *val = serial;
  229. return 0;
  230. }
  231. DEFINE_DEBUGFS_ATTRIBUTE(adis16480_serial_number_fops,
  232. adis16480_show_serial_number, NULL, "0x%.4llx\n");
  233. static int adis16480_show_product_id(void *arg, u64 *val)
  234. {
  235. struct adis16480 *adis16480 = arg;
  236. u16 prod_id;
  237. int ret;
  238. ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_PROD_ID,
  239. &prod_id);
  240. if (ret)
  241. return ret;
  242. *val = prod_id;
  243. return 0;
  244. }
  245. DEFINE_DEBUGFS_ATTRIBUTE(adis16480_product_id_fops,
  246. adis16480_show_product_id, NULL, "%llu\n");
  247. static int adis16480_show_flash_count(void *arg, u64 *val)
  248. {
  249. struct adis16480 *adis16480 = arg;
  250. u32 flash_count;
  251. int ret;
  252. ret = adis_read_reg_32(&adis16480->adis, ADIS16480_REG_FLASH_CNT,
  253. &flash_count);
  254. if (ret)
  255. return ret;
  256. *val = flash_count;
  257. return 0;
  258. }
  259. DEFINE_DEBUGFS_ATTRIBUTE(adis16480_flash_count_fops,
  260. adis16480_show_flash_count, NULL, "%lld\n");
  261. static void adis16480_debugfs_init(struct iio_dev *indio_dev)
  262. {
  263. struct adis16480 *adis16480 = iio_priv(indio_dev);
  264. struct dentry *d = iio_get_debugfs_dentry(indio_dev);
  265. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  266. return;
  267. debugfs_create_file_unsafe("firmware_revision", 0400,
  268. d, adis16480, &adis16480_firmware_revision_fops);
  269. debugfs_create_file_unsafe("firmware_date", 0400,
  270. d, adis16480, &adis16480_firmware_date_fops);
  271. debugfs_create_file_unsafe("serial_number", 0400,
  272. d, adis16480, &adis16480_serial_number_fops);
  273. debugfs_create_file_unsafe("product_id", 0400,
  274. d, adis16480, &adis16480_product_id_fops);
  275. debugfs_create_file_unsafe("flash_count", 0400,
  276. d, adis16480, &adis16480_flash_count_fops);
  277. }
  278. static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
  279. {
  280. struct adis16480 *st = iio_priv(indio_dev);
  281. unsigned int t, sample_rate = st->clk_freq;
  282. int ret;
  283. if (val < 0 || val2 < 0)
  284. return -EINVAL;
  285. t = val * 1000 + val2 / 1000;
  286. if (t == 0)
  287. return -EINVAL;
  288. adis_dev_auto_lock(&st->adis);
  289. /*
  290. * When using PPS mode, the input clock needs to be scaled so that we have an IMU
  291. * sample rate between (optimally) 4000 and 4250. After this, we can use the
  292. * decimation filter to lower the sampling rate in order to get what the user wants.
  293. * Optimally, the user sample rate is a multiple of both the IMU sample rate and
  294. * the input clock. Hence, calculating the sync_scale dynamically gives us better
  295. * chances of achieving a perfect/integer value for DEC_RATE. The math here is:
  296. * 1. lcm of the input clock and the desired output rate.
  297. * 2. get the highest multiple of the previous result lower than the adis max rate.
  298. * 3. The last result becomes the IMU sample rate. Use that to calculate SYNC_SCALE
  299. * and DEC_RATE (to get the user output rate)
  300. */
  301. if (st->clk_mode == ADIS16480_CLK_PPS) {
  302. unsigned long scaled_rate = lcm(st->clk_freq, t);
  303. int sync_scale;
  304. /*
  305. * If lcm is bigger than the IMU maximum sampling rate there's no perfect
  306. * solution. In this case, we get the highest multiple of the input clock
  307. * lower than the IMU max sample rate.
  308. */
  309. if (scaled_rate > st->chip_info->int_clk)
  310. scaled_rate = st->chip_info->int_clk / st->clk_freq * st->clk_freq;
  311. else
  312. scaled_rate = st->chip_info->int_clk / scaled_rate * scaled_rate;
  313. /*
  314. * This is not an hard requirement but it's not advised to run the IMU
  315. * with a sample rate lower than 4000Hz due to possible undersampling
  316. * issues. However, there are users that might really want to take the risk.
  317. * Hence, we provide a module parameter for them. If set, we allow sample
  318. * rates lower than 4KHz. By default, we won't allow this and we just roundup
  319. * the rate to the next multiple of the input clock bigger than 4KHz. This
  320. * is done like this as in some cases (when DEC_RATE is 0) might give
  321. * us the closest value to the one desired by the user...
  322. */
  323. if (scaled_rate < 4000000 && !low_rate_allow)
  324. scaled_rate = roundup(4000000, st->clk_freq);
  325. sync_scale = scaled_rate / st->clk_freq;
  326. ret = __adis_write_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, sync_scale);
  327. if (ret)
  328. return ret;
  329. sample_rate = scaled_rate;
  330. }
  331. t = DIV_ROUND_CLOSEST(sample_rate, t);
  332. if (t)
  333. t--;
  334. if (t > st->chip_info->max_dec_rate)
  335. t = st->chip_info->max_dec_rate;
  336. return __adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
  337. }
  338. static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
  339. {
  340. struct adis16480 *st = iio_priv(indio_dev);
  341. uint16_t t;
  342. int ret;
  343. unsigned int freq, sample_rate = st->clk_freq;
  344. adis_dev_auto_lock(&st->adis);
  345. if (st->clk_mode == ADIS16480_CLK_PPS) {
  346. u16 sync_scale;
  347. ret = __adis_read_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, &sync_scale);
  348. if (ret)
  349. return ret;
  350. sample_rate = st->clk_freq * sync_scale;
  351. }
  352. ret = __adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
  353. if (ret)
  354. return ret;
  355. freq = DIV_ROUND_CLOSEST(sample_rate, (t + 1));
  356. *val = freq / 1000;
  357. *val2 = (freq % 1000) * 1000;
  358. return IIO_VAL_INT_PLUS_MICRO;
  359. }
  360. enum {
  361. ADIS16480_SCAN_GYRO_X,
  362. ADIS16480_SCAN_GYRO_Y,
  363. ADIS16480_SCAN_GYRO_Z,
  364. ADIS16480_SCAN_ACCEL_X,
  365. ADIS16480_SCAN_ACCEL_Y,
  366. ADIS16480_SCAN_ACCEL_Z,
  367. ADIS16480_SCAN_MAGN_X,
  368. ADIS16480_SCAN_MAGN_Y,
  369. ADIS16480_SCAN_MAGN_Z,
  370. ADIS16480_SCAN_BARO,
  371. ADIS16480_SCAN_TEMP,
  372. ADIS16480_SCAN_DELTANG_X,
  373. ADIS16480_SCAN_DELTANG_Y,
  374. ADIS16480_SCAN_DELTANG_Z,
  375. ADIS16480_SCAN_DELTVEL_X,
  376. ADIS16480_SCAN_DELTVEL_Y,
  377. ADIS16480_SCAN_DELTVEL_Z,
  378. };
  379. static const unsigned int adis16480_calibbias_regs[] = {
  380. [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_BIAS,
  381. [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_BIAS,
  382. [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_BIAS,
  383. [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_BIAS,
  384. [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_BIAS,
  385. [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_BIAS,
  386. [ADIS16480_SCAN_MAGN_X] = ADIS16480_REG_X_HARD_IRON,
  387. [ADIS16480_SCAN_MAGN_Y] = ADIS16480_REG_Y_HARD_IRON,
  388. [ADIS16480_SCAN_MAGN_Z] = ADIS16480_REG_Z_HARD_IRON,
  389. [ADIS16480_SCAN_BARO] = ADIS16480_REG_BAROM_BIAS,
  390. };
  391. static const unsigned int adis16480_calibscale_regs[] = {
  392. [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_SCALE,
  393. [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_SCALE,
  394. [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_SCALE,
  395. [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_SCALE,
  396. [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_SCALE,
  397. [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_SCALE,
  398. };
  399. static int adis16480_set_calibbias(struct iio_dev *indio_dev,
  400. const struct iio_chan_spec *chan, int bias)
  401. {
  402. unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
  403. struct adis16480 *st = iio_priv(indio_dev);
  404. switch (chan->type) {
  405. case IIO_MAGN:
  406. case IIO_PRESSURE:
  407. if (bias < -0x8000 || bias >= 0x8000)
  408. return -EINVAL;
  409. return adis_write_reg_16(&st->adis, reg, bias);
  410. case IIO_ANGL_VEL:
  411. case IIO_ACCEL:
  412. return adis_write_reg_32(&st->adis, reg, bias);
  413. default:
  414. break;
  415. }
  416. return -EINVAL;
  417. }
  418. static int adis16480_get_calibbias(struct iio_dev *indio_dev,
  419. const struct iio_chan_spec *chan, int *bias)
  420. {
  421. unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
  422. struct adis16480 *st = iio_priv(indio_dev);
  423. uint16_t val16;
  424. uint32_t val32;
  425. int ret;
  426. switch (chan->type) {
  427. case IIO_MAGN:
  428. case IIO_PRESSURE:
  429. ret = adis_read_reg_16(&st->adis, reg, &val16);
  430. if (ret == 0)
  431. *bias = sign_extend32(val16, 15);
  432. break;
  433. case IIO_ANGL_VEL:
  434. case IIO_ACCEL:
  435. ret = adis_read_reg_32(&st->adis, reg, &val32);
  436. if (ret == 0)
  437. *bias = sign_extend32(val32, 31);
  438. break;
  439. default:
  440. ret = -EINVAL;
  441. }
  442. if (ret)
  443. return ret;
  444. return IIO_VAL_INT;
  445. }
  446. static int adis16480_set_calibscale(struct iio_dev *indio_dev,
  447. const struct iio_chan_spec *chan, int scale)
  448. {
  449. unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
  450. struct adis16480 *st = iio_priv(indio_dev);
  451. if (scale < -0x8000 || scale >= 0x8000)
  452. return -EINVAL;
  453. return adis_write_reg_16(&st->adis, reg, scale);
  454. }
  455. static int adis16480_get_calibscale(struct iio_dev *indio_dev,
  456. const struct iio_chan_spec *chan, int *scale)
  457. {
  458. unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
  459. struct adis16480 *st = iio_priv(indio_dev);
  460. uint16_t val16;
  461. int ret;
  462. ret = adis_read_reg_16(&st->adis, reg, &val16);
  463. if (ret)
  464. return ret;
  465. *scale = sign_extend32(val16, 15);
  466. return IIO_VAL_INT;
  467. }
  468. static const unsigned int adis16480_def_filter_freqs[] = {
  469. 310,
  470. 55,
  471. 275,
  472. 63,
  473. };
  474. static const unsigned int adis16495_def_filter_freqs[] = {
  475. 300,
  476. 100,
  477. 300,
  478. 100,
  479. };
  480. static const unsigned int ad16480_filter_data[][2] = {
  481. [ADIS16480_SCAN_GYRO_X] = { ADIS16480_REG_FILTER_BNK0, 0 },
  482. [ADIS16480_SCAN_GYRO_Y] = { ADIS16480_REG_FILTER_BNK0, 3 },
  483. [ADIS16480_SCAN_GYRO_Z] = { ADIS16480_REG_FILTER_BNK0, 6 },
  484. [ADIS16480_SCAN_ACCEL_X] = { ADIS16480_REG_FILTER_BNK0, 9 },
  485. [ADIS16480_SCAN_ACCEL_Y] = { ADIS16480_REG_FILTER_BNK0, 12 },
  486. [ADIS16480_SCAN_ACCEL_Z] = { ADIS16480_REG_FILTER_BNK1, 0 },
  487. [ADIS16480_SCAN_MAGN_X] = { ADIS16480_REG_FILTER_BNK1, 3 },
  488. [ADIS16480_SCAN_MAGN_Y] = { ADIS16480_REG_FILTER_BNK1, 6 },
  489. [ADIS16480_SCAN_MAGN_Z] = { ADIS16480_REG_FILTER_BNK1, 9 },
  490. };
  491. static int adis16480_get_filter_freq(struct iio_dev *indio_dev,
  492. const struct iio_chan_spec *chan, int *freq)
  493. {
  494. struct adis16480 *st = iio_priv(indio_dev);
  495. unsigned int enable_mask, offset, reg;
  496. uint16_t val;
  497. int ret;
  498. reg = ad16480_filter_data[chan->scan_index][0];
  499. offset = ad16480_filter_data[chan->scan_index][1];
  500. enable_mask = BIT(offset + 2);
  501. ret = adis_read_reg_16(&st->adis, reg, &val);
  502. if (ret)
  503. return ret;
  504. if (!(val & enable_mask))
  505. *freq = 0;
  506. else
  507. *freq = st->chip_info->filter_freqs[(val >> offset) & 0x3];
  508. return IIO_VAL_INT;
  509. }
  510. static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
  511. const struct iio_chan_spec *chan, unsigned int freq)
  512. {
  513. struct adis16480 *st = iio_priv(indio_dev);
  514. unsigned int enable_mask, offset, reg;
  515. unsigned int diff, best_diff;
  516. unsigned int i, best_freq;
  517. uint16_t val;
  518. int ret;
  519. reg = ad16480_filter_data[chan->scan_index][0];
  520. offset = ad16480_filter_data[chan->scan_index][1];
  521. enable_mask = BIT(offset + 2);
  522. adis_dev_auto_lock(&st->adis);
  523. ret = __adis_read_reg_16(&st->adis, reg, &val);
  524. if (ret)
  525. return ret;
  526. if (freq == 0) {
  527. val &= ~enable_mask;
  528. } else {
  529. best_freq = 0;
  530. best_diff = st->chip_info->filter_freqs[0];
  531. for (i = 0; i < ARRAY_SIZE(adis16480_def_filter_freqs); i++) {
  532. if (st->chip_info->filter_freqs[i] >= freq) {
  533. diff = st->chip_info->filter_freqs[i] - freq;
  534. if (diff < best_diff) {
  535. best_diff = diff;
  536. best_freq = i;
  537. }
  538. }
  539. }
  540. val &= ~(0x3 << offset);
  541. val |= best_freq << offset;
  542. val |= enable_mask;
  543. }
  544. return __adis_write_reg_16(&st->adis, reg, val);
  545. }
  546. static int adis16480_read_raw(struct iio_dev *indio_dev,
  547. const struct iio_chan_spec *chan, int *val, int *val2, long info)
  548. {
  549. struct adis16480 *st = iio_priv(indio_dev);
  550. unsigned int temp;
  551. switch (info) {
  552. case IIO_CHAN_INFO_RAW:
  553. return adis_single_conversion(indio_dev, chan, 0, val);
  554. case IIO_CHAN_INFO_SCALE:
  555. switch (chan->type) {
  556. case IIO_ANGL_VEL:
  557. *val = st->chip_info->gyro_max_scale;
  558. *val2 = st->chip_info->gyro_max_val;
  559. return IIO_VAL_FRACTIONAL;
  560. case IIO_ACCEL:
  561. *val = st->chip_info->accel_max_scale;
  562. *val2 = st->chip_info->accel_max_val;
  563. return IIO_VAL_FRACTIONAL;
  564. case IIO_MAGN:
  565. *val = 0;
  566. *val2 = 100; /* 0.0001 gauss */
  567. return IIO_VAL_INT_PLUS_MICRO;
  568. case IIO_TEMP:
  569. /*
  570. * +85 degrees Celsius = temp_max_scale
  571. * +25 degrees Celsius = 0
  572. * LSB, 25 degrees Celsius = 60 / temp_max_scale
  573. */
  574. *val = st->chip_info->temp_scale / 1000;
  575. *val2 = (st->chip_info->temp_scale % 1000) * 1000;
  576. return IIO_VAL_INT_PLUS_MICRO;
  577. case IIO_PRESSURE:
  578. /*
  579. * max scale is 1310 mbar
  580. * max raw value is 32767 shifted for 32bits
  581. */
  582. *val = 131; /* 1310mbar = 131 kPa */
  583. *val2 = 32767 << 16;
  584. return IIO_VAL_FRACTIONAL;
  585. case IIO_DELTA_ANGL:
  586. *val = st->chip_info->deltang_max_val;
  587. *val2 = 31;
  588. return IIO_VAL_FRACTIONAL_LOG2;
  589. case IIO_DELTA_VELOCITY:
  590. *val = st->chip_info->deltvel_max_val;
  591. *val2 = 31;
  592. return IIO_VAL_FRACTIONAL_LOG2;
  593. default:
  594. return -EINVAL;
  595. }
  596. case IIO_CHAN_INFO_OFFSET:
  597. /* Only the temperature channel has a offset */
  598. temp = 25 * 1000000LL; /* 25 degree Celsius = 0x0000 */
  599. *val = DIV_ROUND_CLOSEST_ULL(temp, st->chip_info->temp_scale);
  600. return IIO_VAL_INT;
  601. case IIO_CHAN_INFO_CALIBBIAS:
  602. return adis16480_get_calibbias(indio_dev, chan, val);
  603. case IIO_CHAN_INFO_CALIBSCALE:
  604. return adis16480_get_calibscale(indio_dev, chan, val);
  605. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  606. return adis16480_get_filter_freq(indio_dev, chan, val);
  607. case IIO_CHAN_INFO_SAMP_FREQ:
  608. return adis16480_get_freq(indio_dev, val, val2);
  609. default:
  610. return -EINVAL;
  611. }
  612. }
  613. static int adis16480_write_raw(struct iio_dev *indio_dev,
  614. const struct iio_chan_spec *chan, int val, int val2, long info)
  615. {
  616. switch (info) {
  617. case IIO_CHAN_INFO_CALIBBIAS:
  618. return adis16480_set_calibbias(indio_dev, chan, val);
  619. case IIO_CHAN_INFO_CALIBSCALE:
  620. return adis16480_set_calibscale(indio_dev, chan, val);
  621. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  622. return adis16480_set_filter_freq(indio_dev, chan, val);
  623. case IIO_CHAN_INFO_SAMP_FREQ:
  624. return adis16480_set_freq(indio_dev, val, val2);
  625. default:
  626. return -EINVAL;
  627. }
  628. }
  629. #define ADIS16480_MOD_CHANNEL(_type, _mod, _address, _si, _info_sep, _bits) \
  630. { \
  631. .type = (_type), \
  632. .modified = 1, \
  633. .channel2 = (_mod), \
  634. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  635. BIT(IIO_CHAN_INFO_CALIBBIAS) | \
  636. _info_sep, \
  637. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  638. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  639. .address = (_address), \
  640. .scan_index = (_si), \
  641. .scan_type = { \
  642. .sign = 's', \
  643. .realbits = (_bits), \
  644. .storagebits = (_bits), \
  645. .endianness = IIO_BE, \
  646. }, \
  647. }
  648. #define ADIS16480_GYRO_CHANNEL(_mod) \
  649. ADIS16480_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
  650. ADIS16480_REG_ ## _mod ## _GYRO_OUT, ADIS16480_SCAN_GYRO_ ## _mod, \
  651. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  652. BIT(IIO_CHAN_INFO_CALIBSCALE), \
  653. 32)
  654. #define ADIS16480_ACCEL_CHANNEL(_mod) \
  655. ADIS16480_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
  656. ADIS16480_REG_ ## _mod ## _ACCEL_OUT, ADIS16480_SCAN_ACCEL_ ## _mod, \
  657. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  658. BIT(IIO_CHAN_INFO_CALIBSCALE), \
  659. 32)
  660. #define ADIS16480_DELTANG_CHANNEL(_mod) \
  661. ADIS16480_MOD_CHANNEL(IIO_DELTA_ANGL, IIO_MOD_ ## _mod, \
  662. ADIS16480_REG_ ## _mod ## _DELTAANG_OUT, ADIS16480_SCAN_DELTANG_ ## _mod, \
  663. 0, 32)
  664. #define ADIS16480_DELTANG_CHANNEL_NO_SCAN(_mod) \
  665. ADIS16480_MOD_CHANNEL(IIO_DELTA_ANGL, IIO_MOD_ ## _mod, \
  666. ADIS16480_REG_ ## _mod ## _DELTAANG_OUT, -1, 0, 32)
  667. #define ADIS16480_DELTVEL_CHANNEL(_mod) \
  668. ADIS16480_MOD_CHANNEL(IIO_DELTA_VELOCITY, IIO_MOD_ ## _mod, \
  669. ADIS16480_REG_ ## _mod ## _DELTAVEL_OUT, ADIS16480_SCAN_DELTVEL_ ## _mod, \
  670. 0, 32)
  671. #define ADIS16480_DELTVEL_CHANNEL_NO_SCAN(_mod) \
  672. ADIS16480_MOD_CHANNEL(IIO_DELTA_VELOCITY, IIO_MOD_ ## _mod, \
  673. ADIS16480_REG_ ## _mod ## _DELTAVEL_OUT, -1, 0, 32)
  674. #define ADIS16480_MAGN_CHANNEL(_mod) \
  675. ADIS16480_MOD_CHANNEL(IIO_MAGN, IIO_MOD_ ## _mod, \
  676. ADIS16480_REG_ ## _mod ## _MAGN_OUT, ADIS16480_SCAN_MAGN_ ## _mod, \
  677. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
  678. 16)
  679. #define ADIS16480_PRESSURE_CHANNEL() \
  680. { \
  681. .type = IIO_PRESSURE, \
  682. .indexed = 1, \
  683. .channel = 0, \
  684. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  685. BIT(IIO_CHAN_INFO_CALIBBIAS) | \
  686. BIT(IIO_CHAN_INFO_SCALE), \
  687. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  688. .address = ADIS16480_REG_BAROM_OUT, \
  689. .scan_index = ADIS16480_SCAN_BARO, \
  690. .scan_type = { \
  691. .sign = 's', \
  692. .realbits = 32, \
  693. .storagebits = 32, \
  694. .endianness = IIO_BE, \
  695. }, \
  696. }
  697. #define ADIS16480_TEMP_CHANNEL() { \
  698. .type = IIO_TEMP, \
  699. .indexed = 1, \
  700. .channel = 0, \
  701. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  702. BIT(IIO_CHAN_INFO_SCALE) | \
  703. BIT(IIO_CHAN_INFO_OFFSET), \
  704. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  705. .address = ADIS16480_REG_TEMP_OUT, \
  706. .scan_index = ADIS16480_SCAN_TEMP, \
  707. .scan_type = { \
  708. .sign = 's', \
  709. .realbits = 16, \
  710. .storagebits = 16, \
  711. .endianness = IIO_BE, \
  712. }, \
  713. }
  714. static const struct iio_chan_spec adis16480_channels[] = {
  715. ADIS16480_GYRO_CHANNEL(X),
  716. ADIS16480_GYRO_CHANNEL(Y),
  717. ADIS16480_GYRO_CHANNEL(Z),
  718. ADIS16480_ACCEL_CHANNEL(X),
  719. ADIS16480_ACCEL_CHANNEL(Y),
  720. ADIS16480_ACCEL_CHANNEL(Z),
  721. ADIS16480_MAGN_CHANNEL(X),
  722. ADIS16480_MAGN_CHANNEL(Y),
  723. ADIS16480_MAGN_CHANNEL(Z),
  724. ADIS16480_PRESSURE_CHANNEL(),
  725. ADIS16480_TEMP_CHANNEL(),
  726. IIO_CHAN_SOFT_TIMESTAMP(11),
  727. ADIS16480_DELTANG_CHANNEL_NO_SCAN(X),
  728. ADIS16480_DELTANG_CHANNEL_NO_SCAN(Y),
  729. ADIS16480_DELTANG_CHANNEL_NO_SCAN(Z),
  730. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(X),
  731. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(Y),
  732. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(Z),
  733. };
  734. static const struct iio_chan_spec adis16485_channels[] = {
  735. ADIS16480_GYRO_CHANNEL(X),
  736. ADIS16480_GYRO_CHANNEL(Y),
  737. ADIS16480_GYRO_CHANNEL(Z),
  738. ADIS16480_ACCEL_CHANNEL(X),
  739. ADIS16480_ACCEL_CHANNEL(Y),
  740. ADIS16480_ACCEL_CHANNEL(Z),
  741. ADIS16480_TEMP_CHANNEL(),
  742. IIO_CHAN_SOFT_TIMESTAMP(7),
  743. ADIS16480_DELTANG_CHANNEL_NO_SCAN(X),
  744. ADIS16480_DELTANG_CHANNEL_NO_SCAN(Y),
  745. ADIS16480_DELTANG_CHANNEL_NO_SCAN(Z),
  746. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(X),
  747. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(Y),
  748. ADIS16480_DELTVEL_CHANNEL_NO_SCAN(Z),
  749. };
  750. static const struct iio_chan_spec adis16545_channels[] = {
  751. ADIS16480_GYRO_CHANNEL(X),
  752. ADIS16480_GYRO_CHANNEL(Y),
  753. ADIS16480_GYRO_CHANNEL(Z),
  754. ADIS16480_ACCEL_CHANNEL(X),
  755. ADIS16480_ACCEL_CHANNEL(Y),
  756. ADIS16480_ACCEL_CHANNEL(Z),
  757. ADIS16480_TEMP_CHANNEL(),
  758. ADIS16480_DELTANG_CHANNEL(X),
  759. ADIS16480_DELTANG_CHANNEL(Y),
  760. ADIS16480_DELTANG_CHANNEL(Z),
  761. ADIS16480_DELTVEL_CHANNEL(X),
  762. ADIS16480_DELTVEL_CHANNEL(Y),
  763. ADIS16480_DELTVEL_CHANNEL(Z),
  764. IIO_CHAN_SOFT_TIMESTAMP(17),
  765. };
  766. enum adis16480_variant {
  767. ADIS16375,
  768. ADIS16480,
  769. ADIS16485,
  770. ADIS16488,
  771. ADIS16490,
  772. ADIS16495_1,
  773. ADIS16495_2,
  774. ADIS16495_3,
  775. ADIS16497_1,
  776. ADIS16497_2,
  777. ADIS16497_3,
  778. ADIS16545_1,
  779. ADIS16545_2,
  780. ADIS16545_3,
  781. ADIS16547_1,
  782. ADIS16547_2,
  783. ADIS16547_3
  784. };
  785. #define ADIS16480_DIAG_STAT_XGYRO_FAIL 0
  786. #define ADIS16480_DIAG_STAT_YGYRO_FAIL 1
  787. #define ADIS16480_DIAG_STAT_ZGYRO_FAIL 2
  788. #define ADIS16480_DIAG_STAT_XACCL_FAIL 3
  789. #define ADIS16480_DIAG_STAT_YACCL_FAIL 4
  790. #define ADIS16480_DIAG_STAT_ZACCL_FAIL 5
  791. #define ADIS16480_DIAG_STAT_XMAGN_FAIL 8
  792. #define ADIS16480_DIAG_STAT_YMAGN_FAIL 9
  793. #define ADIS16480_DIAG_STAT_ZMAGN_FAIL 10
  794. #define ADIS16480_DIAG_STAT_BARO_FAIL 11
  795. static const char * const adis16480_status_error_msgs[] = {
  796. [ADIS16480_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
  797. [ADIS16480_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
  798. [ADIS16480_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
  799. [ADIS16480_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
  800. [ADIS16480_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
  801. [ADIS16480_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
  802. [ADIS16480_DIAG_STAT_XMAGN_FAIL] = "X-axis magnetometer self-test failure",
  803. [ADIS16480_DIAG_STAT_YMAGN_FAIL] = "Y-axis magnetometer self-test failure",
  804. [ADIS16480_DIAG_STAT_ZMAGN_FAIL] = "Z-axis magnetometer self-test failure",
  805. [ADIS16480_DIAG_STAT_BARO_FAIL] = "Barometer self-test failure",
  806. };
  807. static int adis16480_enable_irq(struct adis *adis, bool enable);
  808. #define ADIS16480_DATA(_prod_id, _timeouts, _burst_len, _burst_max_speed) \
  809. { \
  810. .diag_stat_reg = ADIS16480_REG_DIAG_STS, \
  811. .glob_cmd_reg = ADIS16480_REG_GLOB_CMD, \
  812. .prod_id_reg = ADIS16480_REG_PROD_ID, \
  813. .prod_id = (_prod_id), \
  814. .has_paging = true, \
  815. .read_delay = 5, \
  816. .write_delay = 5, \
  817. .self_test_mask = BIT(1), \
  818. .self_test_reg = ADIS16480_REG_GLOB_CMD, \
  819. .status_error_msgs = adis16480_status_error_msgs, \
  820. .status_error_mask = BIT(ADIS16480_DIAG_STAT_XGYRO_FAIL) | \
  821. BIT(ADIS16480_DIAG_STAT_YGYRO_FAIL) | \
  822. BIT(ADIS16480_DIAG_STAT_ZGYRO_FAIL) | \
  823. BIT(ADIS16480_DIAG_STAT_XACCL_FAIL) | \
  824. BIT(ADIS16480_DIAG_STAT_YACCL_FAIL) | \
  825. BIT(ADIS16480_DIAG_STAT_ZACCL_FAIL) | \
  826. BIT(ADIS16480_DIAG_STAT_XMAGN_FAIL) | \
  827. BIT(ADIS16480_DIAG_STAT_YMAGN_FAIL) | \
  828. BIT(ADIS16480_DIAG_STAT_ZMAGN_FAIL) | \
  829. BIT(ADIS16480_DIAG_STAT_BARO_FAIL), \
  830. .enable_irq = adis16480_enable_irq, \
  831. .timeouts = (_timeouts), \
  832. .burst_reg_cmd = ADIS16495_REG_BURST_CMD, \
  833. .burst_len = (_burst_len), \
  834. .burst_max_speed_hz = _burst_max_speed \
  835. }
  836. static const struct adis_timeout adis16485_timeouts = {
  837. .reset_ms = 560,
  838. .sw_reset_ms = 120,
  839. .self_test_ms = 12,
  840. };
  841. static const struct adis_timeout adis16480_timeouts = {
  842. .reset_ms = 560,
  843. .sw_reset_ms = 560,
  844. .self_test_ms = 12,
  845. };
  846. static const struct adis_timeout adis16495_timeouts = {
  847. .reset_ms = 170,
  848. .sw_reset_ms = 130,
  849. .self_test_ms = 40,
  850. };
  851. static const struct adis_timeout adis16495_1_timeouts = {
  852. .reset_ms = 250,
  853. .sw_reset_ms = 210,
  854. .self_test_ms = 20,
  855. };
  856. static const struct adis_timeout adis16545_timeouts = {
  857. .reset_ms = 315,
  858. .sw_reset_ms = 270,
  859. .self_test_ms = 35,
  860. };
  861. static const struct adis16480_chip_info adis16480_chip_info[] = {
  862. [ADIS16375] = {
  863. .channels = adis16485_channels,
  864. .num_channels = ARRAY_SIZE(adis16485_channels),
  865. /*
  866. * Typically we do IIO_RAD_TO_DEGREE in the denominator, which
  867. * is exactly the same as IIO_DEGREE_TO_RAD in numerator, since
  868. * it gives better approximation. However, in this case we
  869. * cannot do it since it would not fit in a 32bit variable.
  870. */
  871. .gyro_max_val = 22887 << 16,
  872. .gyro_max_scale = IIO_DEGREE_TO_RAD(300),
  873. .accel_max_val = IIO_M_S_2_TO_G(21973 << 16),
  874. .accel_max_scale = 18,
  875. .temp_scale = 5650, /* 5.65 milli degree Celsius */
  876. .deltang_max_val = IIO_DEGREE_TO_RAD(180),
  877. .deltvel_max_val = 100,
  878. .int_clk = 2460000,
  879. .max_dec_rate = 2048,
  880. .has_sleep_cnt = true,
  881. .filter_freqs = adis16480_def_filter_freqs,
  882. .adis_data = ADIS16480_DATA(16375, &adis16485_timeouts, 0, 0),
  883. },
  884. [ADIS16480] = {
  885. .channels = adis16480_channels,
  886. .num_channels = ARRAY_SIZE(adis16480_channels),
  887. .gyro_max_val = 22500 << 16,
  888. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  889. .accel_max_val = IIO_M_S_2_TO_G(12500 << 16),
  890. .accel_max_scale = 10,
  891. .temp_scale = 5650, /* 5.65 milli degree Celsius */
  892. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  893. .deltvel_max_val = 200,
  894. .int_clk = 2460000,
  895. .max_dec_rate = 2048,
  896. .has_sleep_cnt = true,
  897. .filter_freqs = adis16480_def_filter_freqs,
  898. .adis_data = ADIS16480_DATA(16480, &adis16480_timeouts, 0, 0),
  899. },
  900. [ADIS16485] = {
  901. .channels = adis16485_channels,
  902. .num_channels = ARRAY_SIZE(adis16485_channels),
  903. .gyro_max_val = 22500 << 16,
  904. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  905. .accel_max_val = IIO_M_S_2_TO_G(20000 << 16),
  906. .accel_max_scale = 5,
  907. .temp_scale = 5650, /* 5.65 milli degree Celsius */
  908. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  909. .deltvel_max_val = 50,
  910. .int_clk = 2460000,
  911. .max_dec_rate = 2048,
  912. .has_sleep_cnt = true,
  913. .filter_freqs = adis16480_def_filter_freqs,
  914. .adis_data = ADIS16480_DATA(16485, &adis16485_timeouts, 0, 0),
  915. },
  916. [ADIS16488] = {
  917. .channels = adis16480_channels,
  918. .num_channels = ARRAY_SIZE(adis16480_channels),
  919. .gyro_max_val = 22500 << 16,
  920. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  921. .accel_max_val = IIO_M_S_2_TO_G(22500 << 16),
  922. .accel_max_scale = 18,
  923. .temp_scale = 5650, /* 5.65 milli degree Celsius */
  924. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  925. .deltvel_max_val = 200,
  926. .int_clk = 2460000,
  927. .max_dec_rate = 2048,
  928. .has_sleep_cnt = true,
  929. .filter_freqs = adis16480_def_filter_freqs,
  930. .adis_data = ADIS16480_DATA(16488, &adis16485_timeouts, 0, 0),
  931. },
  932. [ADIS16490] = {
  933. .channels = adis16485_channels,
  934. .num_channels = ARRAY_SIZE(adis16485_channels),
  935. .gyro_max_val = 20000 << 16,
  936. .gyro_max_scale = IIO_DEGREE_TO_RAD(100),
  937. .accel_max_val = IIO_M_S_2_TO_G(16000 << 16),
  938. .accel_max_scale = 8,
  939. .temp_scale = 14285, /* 14.285 milli degree Celsius */
  940. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  941. .deltvel_max_val = 200,
  942. .int_clk = 4250000,
  943. .max_dec_rate = 4250,
  944. .filter_freqs = adis16495_def_filter_freqs,
  945. .has_pps_clk_mode = true,
  946. .adis_data = ADIS16480_DATA(16490, &adis16495_timeouts, 0, 0),
  947. },
  948. [ADIS16495_1] = {
  949. .channels = adis16485_channels,
  950. .num_channels = ARRAY_SIZE(adis16485_channels),
  951. .gyro_max_val = 20000 << 16,
  952. .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
  953. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  954. .accel_max_scale = 8,
  955. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  956. .deltang_max_val = IIO_DEGREE_TO_RAD(360),
  957. .deltvel_max_val = 100,
  958. .int_clk = 4250000,
  959. .max_dec_rate = 4250,
  960. .filter_freqs = adis16495_def_filter_freqs,
  961. .has_pps_clk_mode = true,
  962. /* 20 elements of 16bits */
  963. .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
  964. ADIS16495_BURST_MAX_DATA * 2,
  965. 6000000),
  966. },
  967. [ADIS16495_2] = {
  968. .channels = adis16485_channels,
  969. .num_channels = ARRAY_SIZE(adis16485_channels),
  970. .gyro_max_val = 18000 << 16,
  971. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  972. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  973. .accel_max_scale = 8,
  974. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  975. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  976. .deltvel_max_val = 100,
  977. .int_clk = 4250000,
  978. .max_dec_rate = 4250,
  979. .filter_freqs = adis16495_def_filter_freqs,
  980. .has_pps_clk_mode = true,
  981. /* 20 elements of 16bits */
  982. .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
  983. ADIS16495_BURST_MAX_DATA * 2,
  984. 6000000),
  985. },
  986. [ADIS16495_3] = {
  987. .channels = adis16485_channels,
  988. .num_channels = ARRAY_SIZE(adis16485_channels),
  989. .gyro_max_val = 20000 << 16,
  990. .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
  991. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  992. .accel_max_scale = 8,
  993. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  994. .deltang_max_val = IIO_DEGREE_TO_RAD(2160),
  995. .deltvel_max_val = 100,
  996. .int_clk = 4250000,
  997. .max_dec_rate = 4250,
  998. .filter_freqs = adis16495_def_filter_freqs,
  999. .has_pps_clk_mode = true,
  1000. /* 20 elements of 16bits */
  1001. .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
  1002. ADIS16495_BURST_MAX_DATA * 2,
  1003. 6000000),
  1004. },
  1005. [ADIS16497_1] = {
  1006. .channels = adis16485_channels,
  1007. .num_channels = ARRAY_SIZE(adis16485_channels),
  1008. .gyro_max_val = 20000 << 16,
  1009. .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
  1010. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1011. .accel_max_scale = 40,
  1012. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  1013. .deltang_max_val = IIO_DEGREE_TO_RAD(360),
  1014. .deltvel_max_val = 400,
  1015. .int_clk = 4250000,
  1016. .max_dec_rate = 4250,
  1017. .filter_freqs = adis16495_def_filter_freqs,
  1018. .has_pps_clk_mode = true,
  1019. /* 20 elements of 16bits */
  1020. .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
  1021. ADIS16495_BURST_MAX_DATA * 2,
  1022. 6000000),
  1023. },
  1024. [ADIS16497_2] = {
  1025. .channels = adis16485_channels,
  1026. .num_channels = ARRAY_SIZE(adis16485_channels),
  1027. .gyro_max_val = 18000 << 16,
  1028. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  1029. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1030. .accel_max_scale = 40,
  1031. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  1032. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  1033. .deltvel_max_val = 400,
  1034. .int_clk = 4250000,
  1035. .max_dec_rate = 4250,
  1036. .filter_freqs = adis16495_def_filter_freqs,
  1037. .has_pps_clk_mode = true,
  1038. /* 20 elements of 16bits */
  1039. .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
  1040. ADIS16495_BURST_MAX_DATA * 2,
  1041. 6000000),
  1042. },
  1043. [ADIS16497_3] = {
  1044. .channels = adis16485_channels,
  1045. .num_channels = ARRAY_SIZE(adis16485_channels),
  1046. .gyro_max_val = 20000 << 16,
  1047. .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
  1048. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1049. .accel_max_scale = 40,
  1050. .temp_scale = 12500, /* 12.5 milli degree Celsius */
  1051. .deltang_max_val = IIO_DEGREE_TO_RAD(2160),
  1052. .deltvel_max_val = 400,
  1053. .int_clk = 4250000,
  1054. .max_dec_rate = 4250,
  1055. .filter_freqs = adis16495_def_filter_freqs,
  1056. .has_pps_clk_mode = true,
  1057. /* 20 elements of 16bits */
  1058. .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
  1059. ADIS16495_BURST_MAX_DATA * 2,
  1060. 6000000),
  1061. },
  1062. [ADIS16545_1] = {
  1063. .channels = adis16545_channels,
  1064. .num_channels = ARRAY_SIZE(adis16545_channels),
  1065. .gyro_max_val = 20000 << 16,
  1066. .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
  1067. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1068. .accel_max_scale = 8,
  1069. .temp_scale = 7000, /* 7 milli degree Celsius */
  1070. .deltang_max_val = IIO_DEGREE_TO_RAD(360),
  1071. .deltvel_max_val = 100,
  1072. .int_clk = 4250000,
  1073. .max_dec_rate = 4250,
  1074. .filter_freqs = adis16495_def_filter_freqs,
  1075. .has_pps_clk_mode = true,
  1076. .has_burst_delta_data = true,
  1077. /* 20 elements of 16bits */
  1078. .adis_data = ADIS16480_DATA(16545, &adis16545_timeouts,
  1079. ADIS16495_BURST_MAX_DATA * 2,
  1080. 6500000),
  1081. },
  1082. [ADIS16545_2] = {
  1083. .channels = adis16545_channels,
  1084. .num_channels = ARRAY_SIZE(adis16545_channels),
  1085. .gyro_max_val = 18000 << 16,
  1086. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  1087. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1088. .accel_max_scale = 8,
  1089. .temp_scale = 7000, /* 7 milli degree Celsius */
  1090. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  1091. .deltvel_max_val = 100,
  1092. .int_clk = 4250000,
  1093. .max_dec_rate = 4250,
  1094. .filter_freqs = adis16495_def_filter_freqs,
  1095. .has_pps_clk_mode = true,
  1096. .has_burst_delta_data = true,
  1097. /* 20 elements of 16bits */
  1098. .adis_data = ADIS16480_DATA(16545, &adis16545_timeouts,
  1099. ADIS16495_BURST_MAX_DATA * 2,
  1100. 6500000),
  1101. },
  1102. [ADIS16545_3] = {
  1103. .channels = adis16545_channels,
  1104. .num_channels = ARRAY_SIZE(adis16545_channels),
  1105. .gyro_max_val = 20000 << 16,
  1106. .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
  1107. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1108. .accel_max_scale = 8,
  1109. .temp_scale = 7000, /* 7 milli degree Celsius */
  1110. .deltang_max_val = IIO_DEGREE_TO_RAD(2160),
  1111. .deltvel_max_val = 100,
  1112. .int_clk = 4250000,
  1113. .max_dec_rate = 4250,
  1114. .filter_freqs = adis16495_def_filter_freqs,
  1115. .has_pps_clk_mode = true,
  1116. .has_burst_delta_data = true,
  1117. /* 20 elements of 16bits */
  1118. .adis_data = ADIS16480_DATA(16545, &adis16545_timeouts,
  1119. ADIS16495_BURST_MAX_DATA * 2,
  1120. 6500000),
  1121. },
  1122. [ADIS16547_1] = {
  1123. .channels = adis16545_channels,
  1124. .num_channels = ARRAY_SIZE(adis16545_channels),
  1125. .gyro_max_val = 20000 << 16,
  1126. .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
  1127. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1128. .accel_max_scale = 40,
  1129. .temp_scale = 7000, /* 7 milli degree Celsius */
  1130. .deltang_max_val = IIO_DEGREE_TO_RAD(360),
  1131. .deltvel_max_val = 400,
  1132. .int_clk = 4250000,
  1133. .max_dec_rate = 4250,
  1134. .filter_freqs = adis16495_def_filter_freqs,
  1135. .has_pps_clk_mode = true,
  1136. .has_burst_delta_data = true,
  1137. /* 20 elements of 16bits */
  1138. .adis_data = ADIS16480_DATA(16547, &adis16545_timeouts,
  1139. ADIS16495_BURST_MAX_DATA * 2,
  1140. 6500000),
  1141. },
  1142. [ADIS16547_2] = {
  1143. .channels = adis16545_channels,
  1144. .num_channels = ARRAY_SIZE(adis16545_channels),
  1145. .gyro_max_val = 18000 << 16,
  1146. .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
  1147. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1148. .accel_max_scale = 40,
  1149. .temp_scale = 7000, /* 7 milli degree Celsius */
  1150. .deltang_max_val = IIO_DEGREE_TO_RAD(720),
  1151. .deltvel_max_val = 400,
  1152. .int_clk = 4250000,
  1153. .max_dec_rate = 4250,
  1154. .filter_freqs = adis16495_def_filter_freqs,
  1155. .has_pps_clk_mode = true,
  1156. .has_burst_delta_data = true,
  1157. /* 20 elements of 16bits */
  1158. .adis_data = ADIS16480_DATA(16547, &adis16545_timeouts,
  1159. ADIS16495_BURST_MAX_DATA * 2,
  1160. 6500000),
  1161. },
  1162. [ADIS16547_3] = {
  1163. .channels = adis16545_channels,
  1164. .num_channels = ARRAY_SIZE(adis16545_channels),
  1165. .gyro_max_val = 20000 << 16,
  1166. .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
  1167. .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
  1168. .accel_max_scale = 40,
  1169. .temp_scale = 7000, /* 7 milli degree Celsius */
  1170. .deltang_max_val = IIO_DEGREE_TO_RAD(2160),
  1171. .deltvel_max_val = 400,
  1172. .int_clk = 4250000,
  1173. .max_dec_rate = 4250,
  1174. .filter_freqs = adis16495_def_filter_freqs,
  1175. .has_pps_clk_mode = true,
  1176. .has_burst_delta_data = true,
  1177. /* 20 elements of 16bits */
  1178. .adis_data = ADIS16480_DATA(16547, &adis16545_timeouts,
  1179. ADIS16495_BURST_MAX_DATA * 2,
  1180. 6500000),
  1181. },
  1182. };
  1183. static bool adis16480_validate_crc(const u16 *buf, const u8 n_elem, const u32 crc)
  1184. {
  1185. u32 crc_calc;
  1186. u16 crc_buf[15];
  1187. int j;
  1188. for (j = 0; j < n_elem; j++)
  1189. crc_buf[j] = swab16(buf[j]);
  1190. crc_calc = crc32(~0, crc_buf, n_elem * 2);
  1191. crc_calc ^= ~0;
  1192. return (crc == crc_calc);
  1193. }
  1194. static irqreturn_t adis16480_trigger_handler(int irq, void *p)
  1195. {
  1196. struct iio_poll_func *pf = p;
  1197. struct iio_dev *indio_dev = pf->indio_dev;
  1198. struct adis16480 *st = iio_priv(indio_dev);
  1199. struct adis *adis = &st->adis;
  1200. struct device *dev = &adis->spi->dev;
  1201. int ret, bit, offset, i = 0, buff_offset = 0;
  1202. __be16 *buffer;
  1203. u32 crc;
  1204. bool valid;
  1205. adis_dev_auto_scoped_lock(adis) {
  1206. if (adis->current_page != 0) {
  1207. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  1208. adis->tx[1] = 0;
  1209. ret = spi_write(adis->spi, adis->tx, 2);
  1210. if (ret) {
  1211. dev_err(dev, "Failed to change device page: %d\n", ret);
  1212. goto irq_done;
  1213. }
  1214. adis->current_page = 0;
  1215. }
  1216. ret = spi_sync(adis->spi, &adis->msg);
  1217. if (ret) {
  1218. dev_err(dev, "Failed to read data: %d\n", ret);
  1219. goto irq_done;
  1220. }
  1221. }
  1222. /*
  1223. * After making the burst request, the response can have one or two
  1224. * 16-bit responses containing the BURST_ID depending on the sclk. If
  1225. * clk > 3.6MHz, then we will have two BURST_ID in a row. If clk < 3MHZ,
  1226. * we have only one. To manage that variation, we use the transition from the
  1227. * BURST_ID to the SYS_E_FLAG register, which will not be equal to 0xA5A5/0xC3C3.
  1228. * If we not find this variation in the first 4 segments, then the data should
  1229. * not be valid.
  1230. */
  1231. buffer = adis->buffer;
  1232. for (offset = 0; offset < 4; offset++) {
  1233. u16 curr = be16_to_cpu(buffer[offset]);
  1234. u16 next = be16_to_cpu(buffer[offset + 1]);
  1235. if (curr == st->burst_id && next != st->burst_id) {
  1236. offset++;
  1237. break;
  1238. }
  1239. }
  1240. if (offset == 4) {
  1241. dev_err(dev, "Invalid burst data\n");
  1242. goto irq_done;
  1243. }
  1244. crc = be16_to_cpu(buffer[offset + 16]) << 16 | be16_to_cpu(buffer[offset + 15]);
  1245. valid = adis16480_validate_crc((u16 *)&buffer[offset], 15, crc);
  1246. if (!valid) {
  1247. dev_err(dev, "Invalid crc\n");
  1248. goto irq_done;
  1249. }
  1250. iio_for_each_active_channel(indio_dev, bit) {
  1251. /*
  1252. * When burst mode is used, temperature is the first data
  1253. * channel in the sequence, but the temperature scan index
  1254. * is 10.
  1255. */
  1256. switch (bit) {
  1257. case ADIS16480_SCAN_TEMP:
  1258. st->data[i++] = buffer[offset + 1];
  1259. /*
  1260. * The temperature channel has 16-bit storage size.
  1261. * We need to perform the padding to have the buffer
  1262. * elements naturally aligned in case there are any
  1263. * 32-bit storage size channels enabled which are added
  1264. * in the buffer after the temprature data. In case
  1265. * there is no data being added after the temperature
  1266. * data, the padding is harmless.
  1267. */
  1268. st->data[i++] = 0;
  1269. break;
  1270. case ADIS16480_SCAN_DELTANG_X ... ADIS16480_SCAN_DELTVEL_Z:
  1271. buff_offset = ADIS16480_SCAN_DELTANG_X;
  1272. fallthrough;
  1273. case ADIS16480_SCAN_GYRO_X ... ADIS16480_SCAN_ACCEL_Z:
  1274. /* The lower register data is sequenced first */
  1275. st->data[i++] = buffer[2 * (bit - buff_offset) + offset + 3];
  1276. st->data[i++] = buffer[2 * (bit - buff_offset) + offset + 2];
  1277. break;
  1278. }
  1279. }
  1280. iio_push_to_buffers_with_timestamp(indio_dev, st->data, pf->timestamp);
  1281. irq_done:
  1282. iio_trigger_notify_done(indio_dev->trig);
  1283. return IRQ_HANDLED;
  1284. }
  1285. static const unsigned long adis16545_channel_masks[] = {
  1286. ADIS16545_BURST_DATA_SEL_0_CHN_MASK | BIT(ADIS16480_SCAN_TEMP) | BIT(17),
  1287. ADIS16545_BURST_DATA_SEL_1_CHN_MASK | BIT(ADIS16480_SCAN_TEMP) | BIT(17),
  1288. 0,
  1289. };
  1290. static int adis16480_update_scan_mode(struct iio_dev *indio_dev,
  1291. const unsigned long *scan_mask)
  1292. {
  1293. u16 en;
  1294. int ret;
  1295. struct adis16480 *st = iio_priv(indio_dev);
  1296. if (st->chip_info->has_burst_delta_data) {
  1297. if (*scan_mask & ADIS16545_BURST_DATA_SEL_0_CHN_MASK) {
  1298. en = FIELD_PREP(ADIS16545_BURST_DATA_SEL_MASK, 0);
  1299. st->burst_id = ADIS16495_GYRO_ACCEL_BURST_ID;
  1300. } else {
  1301. en = FIELD_PREP(ADIS16545_BURST_DATA_SEL_MASK, 1);
  1302. st->burst_id = ADIS16545_DELTA_ANG_VEL_BURST_ID;
  1303. }
  1304. ret = __adis_update_bits(&st->adis, ADIS16480_REG_CONFIG,
  1305. ADIS16545_BURST_DATA_SEL_MASK, en);
  1306. if (ret)
  1307. return ret;
  1308. }
  1309. return adis_update_scan_mode(indio_dev, scan_mask);
  1310. }
  1311. static const struct iio_info adis16480_info = {
  1312. .read_raw = &adis16480_read_raw,
  1313. .write_raw = &adis16480_write_raw,
  1314. .update_scan_mode = &adis16480_update_scan_mode,
  1315. .debugfs_reg_access = adis_debugfs_reg_access,
  1316. };
  1317. static int adis16480_stop_device(struct iio_dev *indio_dev)
  1318. {
  1319. struct adis16480 *st = iio_priv(indio_dev);
  1320. struct device *dev = &st->adis.spi->dev;
  1321. int ret;
  1322. ret = adis_write_reg_16(&st->adis, ADIS16480_REG_SLP_CNT, BIT(9));
  1323. if (ret)
  1324. dev_err(dev, "Could not power down device: %d\n", ret);
  1325. return ret;
  1326. }
  1327. static int adis16480_enable_irq(struct adis *adis, bool enable)
  1328. {
  1329. uint16_t val;
  1330. int ret;
  1331. ret = __adis_read_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL, &val);
  1332. if (ret)
  1333. return ret;
  1334. val &= ~ADIS16480_DRDY_EN_MSK;
  1335. val |= ADIS16480_DRDY_EN(enable);
  1336. return __adis_write_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL, val);
  1337. }
  1338. static int adis16480_config_irq_pin(struct adis16480 *st)
  1339. {
  1340. struct device *dev = &st->adis.spi->dev;
  1341. struct fwnode_handle *fwnode = dev_fwnode(dev);
  1342. enum adis16480_int_pin pin;
  1343. unsigned int irq_type;
  1344. uint16_t val;
  1345. int i, irq = 0;
  1346. /* Disable data ready since the default after reset is on */
  1347. val = ADIS16480_DRDY_EN(0);
  1348. /*
  1349. * Get the interrupt from the devicetre by reading the interrupt-names
  1350. * property. If it is not specified, use DIO1 pin as default.
  1351. * According to the datasheet, the factory default assigns DIO2 as data
  1352. * ready signal. However, in the previous versions of the driver, DIO1
  1353. * pin was used. So, we should leave it as is since some devices might
  1354. * be expecting the interrupt on the wrong physical pin.
  1355. */
  1356. pin = ADIS16480_PIN_DIO1;
  1357. for (i = 0; i < ARRAY_SIZE(adis16480_int_pin_names); i++) {
  1358. irq = fwnode_irq_get_byname(fwnode, adis16480_int_pin_names[i]);
  1359. if (irq > 0) {
  1360. pin = i;
  1361. break;
  1362. }
  1363. }
  1364. val |= ADIS16480_DRDY_SEL(pin);
  1365. /*
  1366. * Get the interrupt line behaviour. The data ready polarity can be
  1367. * configured as positive or negative, corresponding to
  1368. * IRQ_TYPE_EDGE_RISING or IRQ_TYPE_EDGE_FALLING respectively.
  1369. */
  1370. irq_type = irq_get_trigger_type(st->adis.spi->irq);
  1371. if (irq_type == IRQ_TYPE_EDGE_RISING) { /* Default */
  1372. val |= ADIS16480_DRDY_POL(1);
  1373. } else if (irq_type == IRQ_TYPE_EDGE_FALLING) {
  1374. val |= ADIS16480_DRDY_POL(0);
  1375. } else {
  1376. dev_err(dev, "Invalid interrupt type 0x%x specified\n", irq_type);
  1377. return -EINVAL;
  1378. }
  1379. /* Write the data ready configuration to the FNCTIO_CTRL register */
  1380. return adis_write_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, val);
  1381. }
  1382. static int adis16480_fw_get_ext_clk_pin(struct adis16480 *st)
  1383. {
  1384. struct device *dev = &st->adis.spi->dev;
  1385. const char *ext_clk_pin;
  1386. enum adis16480_int_pin pin;
  1387. int i;
  1388. pin = ADIS16480_PIN_DIO2;
  1389. if (device_property_read_string(dev, "adi,ext-clk-pin", &ext_clk_pin))
  1390. goto clk_input_not_found;
  1391. for (i = 0; i < ARRAY_SIZE(adis16480_int_pin_names); i++) {
  1392. if (strcasecmp(ext_clk_pin, adis16480_int_pin_names[i]) == 0)
  1393. return i;
  1394. }
  1395. clk_input_not_found:
  1396. dev_info(dev, "clk input line not specified, using DIO2\n");
  1397. return pin;
  1398. }
  1399. static int adis16480_ext_clk_config(struct adis16480 *st, bool enable)
  1400. {
  1401. struct device *dev = &st->adis.spi->dev;
  1402. unsigned int mode, mask;
  1403. enum adis16480_int_pin pin;
  1404. uint16_t val;
  1405. int ret;
  1406. ret = adis_read_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, &val);
  1407. if (ret)
  1408. return ret;
  1409. pin = adis16480_fw_get_ext_clk_pin(st);
  1410. /*
  1411. * Each DIOx pin supports only one function at a time. When a single pin
  1412. * has two assignments, the enable bit for a lower priority function
  1413. * automatically resets to zero (disabling the lower priority function).
  1414. */
  1415. if (pin == ADIS16480_DRDY_SEL(val))
  1416. dev_warn(dev, "DIO%x pin supports only one function at a time\n", pin + 1);
  1417. mode = ADIS16480_SYNC_EN(enable) | ADIS16480_SYNC_SEL(pin);
  1418. mask = ADIS16480_SYNC_EN_MSK | ADIS16480_SYNC_SEL_MSK;
  1419. /* Only ADIS1649x devices support pps ext clock mode */
  1420. if (st->chip_info->has_pps_clk_mode) {
  1421. mode |= ADIS16480_SYNC_MODE(st->clk_mode);
  1422. mask |= ADIS16480_SYNC_MODE_MSK;
  1423. }
  1424. val &= ~mask;
  1425. val |= mode;
  1426. ret = adis_write_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, val);
  1427. if (ret)
  1428. return ret;
  1429. return clk_prepare_enable(st->ext_clk);
  1430. }
  1431. static int adis16480_get_ext_clocks(struct adis16480 *st)
  1432. {
  1433. struct device *dev = &st->adis.spi->dev;
  1434. st->ext_clk = devm_clk_get_optional(dev, "sync");
  1435. if (IS_ERR(st->ext_clk))
  1436. return dev_err_probe(dev, PTR_ERR(st->ext_clk), "failed to get ext clk\n");
  1437. if (st->ext_clk) {
  1438. st->clk_mode = ADIS16480_CLK_SYNC;
  1439. return 0;
  1440. }
  1441. if (st->chip_info->has_pps_clk_mode) {
  1442. st->ext_clk = devm_clk_get_optional(dev, "pps");
  1443. if (IS_ERR(st->ext_clk))
  1444. return dev_err_probe(dev, PTR_ERR(st->ext_clk), "failed to get ext clk\n");
  1445. if (st->ext_clk) {
  1446. st->clk_mode = ADIS16480_CLK_PPS;
  1447. return 0;
  1448. }
  1449. }
  1450. st->clk_mode = ADIS16480_CLK_INT;
  1451. return 0;
  1452. }
  1453. static void adis16480_stop(void *data)
  1454. {
  1455. adis16480_stop_device(data);
  1456. }
  1457. static void adis16480_clk_disable(void *data)
  1458. {
  1459. clk_disable_unprepare(data);
  1460. }
  1461. static int adis16480_probe(struct spi_device *spi)
  1462. {
  1463. const struct spi_device_id *id = spi_get_device_id(spi);
  1464. const struct adis_data *adis16480_data;
  1465. irq_handler_t trigger_handler = NULL;
  1466. struct device *dev = &spi->dev;
  1467. struct iio_dev *indio_dev;
  1468. struct adis16480 *st;
  1469. int ret;
  1470. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1471. if (indio_dev == NULL)
  1472. return -ENOMEM;
  1473. st = iio_priv(indio_dev);
  1474. st->chip_info = &adis16480_chip_info[id->driver_data];
  1475. indio_dev->name = spi_get_device_id(spi)->name;
  1476. indio_dev->channels = st->chip_info->channels;
  1477. indio_dev->num_channels = st->chip_info->num_channels;
  1478. if (st->chip_info->has_burst_delta_data)
  1479. indio_dev->available_scan_masks = adis16545_channel_masks;
  1480. indio_dev->info = &adis16480_info;
  1481. indio_dev->modes = INDIO_DIRECT_MODE;
  1482. adis16480_data = &st->chip_info->adis_data;
  1483. ret = adis_init(&st->adis, indio_dev, spi, adis16480_data);
  1484. if (ret)
  1485. return ret;
  1486. ret = __adis_initial_startup(&st->adis);
  1487. if (ret)
  1488. return ret;
  1489. /*
  1490. * By default, use burst id for gyroscope and accelerometer data.
  1491. * This is the only option for devices which do not offer delta angle
  1492. * and delta velocity burst readings.
  1493. */
  1494. st->burst_id = ADIS16495_GYRO_ACCEL_BURST_ID;
  1495. if (st->chip_info->has_sleep_cnt) {
  1496. ret = devm_add_action_or_reset(dev, adis16480_stop, indio_dev);
  1497. if (ret)
  1498. return ret;
  1499. }
  1500. ret = adis16480_config_irq_pin(st);
  1501. if (ret)
  1502. return ret;
  1503. ret = adis16480_get_ext_clocks(st);
  1504. if (ret)
  1505. return ret;
  1506. if (st->ext_clk) {
  1507. ret = adis16480_ext_clk_config(st, true);
  1508. if (ret)
  1509. return ret;
  1510. ret = devm_add_action_or_reset(dev, adis16480_clk_disable, st->ext_clk);
  1511. if (ret)
  1512. return ret;
  1513. st->clk_freq = clk_get_rate(st->ext_clk);
  1514. st->clk_freq *= 1000; /* micro */
  1515. if (st->clk_mode == ADIS16480_CLK_PPS) {
  1516. u16 sync_scale;
  1517. /*
  1518. * In PPS mode, the IMU sample rate is the clk_freq * sync_scale. Hence,
  1519. * default the IMU sample rate to the highest multiple of the input clock
  1520. * lower than the IMU max sample rate. The internal sample rate is the
  1521. * max...
  1522. */
  1523. sync_scale = st->chip_info->int_clk / st->clk_freq;
  1524. ret = __adis_write_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, sync_scale);
  1525. if (ret)
  1526. return ret;
  1527. }
  1528. } else {
  1529. st->clk_freq = st->chip_info->int_clk;
  1530. }
  1531. /* Only use our trigger handler if burst mode is supported */
  1532. if (adis16480_data->burst_len)
  1533. trigger_handler = adis16480_trigger_handler;
  1534. ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev,
  1535. trigger_handler);
  1536. if (ret)
  1537. return ret;
  1538. ret = devm_iio_device_register(dev, indio_dev);
  1539. if (ret)
  1540. return ret;
  1541. adis16480_debugfs_init(indio_dev);
  1542. return 0;
  1543. }
  1544. static const struct spi_device_id adis16480_ids[] = {
  1545. { "adis16375", ADIS16375 },
  1546. { "adis16480", ADIS16480 },
  1547. { "adis16485", ADIS16485 },
  1548. { "adis16488", ADIS16488 },
  1549. { "adis16490", ADIS16490 },
  1550. { "adis16495-1", ADIS16495_1 },
  1551. { "adis16495-2", ADIS16495_2 },
  1552. { "adis16495-3", ADIS16495_3 },
  1553. { "adis16497-1", ADIS16497_1 },
  1554. { "adis16497-2", ADIS16497_2 },
  1555. { "adis16497-3", ADIS16497_3 },
  1556. { "adis16545-1", ADIS16545_1 },
  1557. { "adis16545-2", ADIS16545_2 },
  1558. { "adis16545-3", ADIS16545_3 },
  1559. { "adis16547-1", ADIS16547_1 },
  1560. { "adis16547-2", ADIS16547_2 },
  1561. { "adis16547-3", ADIS16547_3 },
  1562. { }
  1563. };
  1564. MODULE_DEVICE_TABLE(spi, adis16480_ids);
  1565. static const struct of_device_id adis16480_of_match[] = {
  1566. { .compatible = "adi,adis16375" },
  1567. { .compatible = "adi,adis16480" },
  1568. { .compatible = "adi,adis16485" },
  1569. { .compatible = "adi,adis16488" },
  1570. { .compatible = "adi,adis16490" },
  1571. { .compatible = "adi,adis16495-1" },
  1572. { .compatible = "adi,adis16495-2" },
  1573. { .compatible = "adi,adis16495-3" },
  1574. { .compatible = "adi,adis16497-1" },
  1575. { .compatible = "adi,adis16497-2" },
  1576. { .compatible = "adi,adis16497-3" },
  1577. { .compatible = "adi,adis16545-1" },
  1578. { .compatible = "adi,adis16545-2" },
  1579. { .compatible = "adi,adis16545-3" },
  1580. { .compatible = "adi,adis16547-1" },
  1581. { .compatible = "adi,adis16547-2" },
  1582. { .compatible = "adi,adis16547-3" },
  1583. { },
  1584. };
  1585. MODULE_DEVICE_TABLE(of, adis16480_of_match);
  1586. static struct spi_driver adis16480_driver = {
  1587. .driver = {
  1588. .name = "adis16480",
  1589. .of_match_table = adis16480_of_match,
  1590. },
  1591. .id_table = adis16480_ids,
  1592. .probe = adis16480_probe,
  1593. };
  1594. module_spi_driver(adis16480_driver);
  1595. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  1596. MODULE_DESCRIPTION("Analog Devices ADIS16480 IMU driver");
  1597. MODULE_LICENSE("GPL v2");
  1598. MODULE_IMPORT_NS(IIO_ADISLIB);