adis16475.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ADIS16475 IMU driver
  4. *
  5. * Copyright 2019 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/iio/buffer.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/imu/adis.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/irq.h>
  19. #include <linux/lcm.h>
  20. #include <linux/math.h>
  21. #include <linux/module.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/property.h>
  24. #include <linux/spi/spi.h>
  25. #define ADIS16475_REG_DIAG_STAT 0x02
  26. #define ADIS16475_REG_X_GYRO_L 0x04
  27. #define ADIS16475_REG_Y_GYRO_L 0x08
  28. #define ADIS16475_REG_Z_GYRO_L 0x0C
  29. #define ADIS16475_REG_X_ACCEL_L 0x10
  30. #define ADIS16475_REG_Y_ACCEL_L 0x14
  31. #define ADIS16475_REG_Z_ACCEL_L 0x18
  32. #define ADIS16475_REG_TEMP_OUT 0x1c
  33. #define ADIS16475_REG_X_GYRO_BIAS_L 0x40
  34. #define ADIS16475_REG_Y_GYRO_BIAS_L 0x44
  35. #define ADIS16475_REG_Z_GYRO_BIAS_L 0x48
  36. #define ADIS16475_REG_X_ACCEL_BIAS_L 0x4c
  37. #define ADIS16475_REG_Y_ACCEL_BIAS_L 0x50
  38. #define ADIS16475_REG_Z_ACCEL_BIAS_L 0x54
  39. #define ADIS16475_REG_FILT_CTRL 0x5c
  40. #define ADIS16475_FILT_CTRL_MASK GENMASK(2, 0)
  41. #define ADIS16475_FILT_CTRL(x) FIELD_PREP(ADIS16475_FILT_CTRL_MASK, x)
  42. #define ADIS16475_REG_MSG_CTRL 0x60
  43. #define ADIS16475_MSG_CTRL_DR_POL_MASK BIT(0)
  44. #define ADIS16475_MSG_CTRL_DR_POL(x) \
  45. FIELD_PREP(ADIS16475_MSG_CTRL_DR_POL_MASK, x)
  46. #define ADIS16475_SYNC_MODE_MASK GENMASK(4, 2)
  47. #define ADIS16475_SYNC_MODE(x) FIELD_PREP(ADIS16475_SYNC_MODE_MASK, x)
  48. #define ADIS16475_REG_UP_SCALE 0x62
  49. #define ADIS16475_REG_DEC_RATE 0x64
  50. #define ADIS16475_REG_GLOB_CMD 0x68
  51. #define ADIS16475_REG_FIRM_REV 0x6c
  52. #define ADIS16475_REG_FIRM_DM 0x6e
  53. #define ADIS16475_REG_FIRM_Y 0x70
  54. #define ADIS16475_REG_PROD_ID 0x72
  55. #define ADIS16475_REG_SERIAL_NUM 0x74
  56. #define ADIS16475_REG_FLASH_CNT 0x7c
  57. #define ADIS16500_BURST32_MASK BIT(9)
  58. #define ADIS16500_BURST32(x) FIELD_PREP(ADIS16500_BURST32_MASK, x)
  59. /* number of data elements in burst mode */
  60. #define ADIS16475_BURST32_MAX_DATA 32
  61. #define ADIS16475_BURST_MAX_DATA 20
  62. #define ADIS16475_MAX_SCAN_DATA 20
  63. /* spi max speed in brust mode */
  64. #define ADIS16475_BURST_MAX_SPEED 1000000
  65. #define ADIS16475_LSB_DEC_MASK BIT(0)
  66. #define ADIS16475_LSB_FIR_MASK BIT(1)
  67. enum {
  68. ADIS16475_SYNC_DIRECT = 1,
  69. ADIS16475_SYNC_SCALED,
  70. ADIS16475_SYNC_OUTPUT,
  71. ADIS16475_SYNC_PULSE = 5,
  72. };
  73. struct adis16475_sync {
  74. u16 sync_mode;
  75. u16 min_rate;
  76. u16 max_rate;
  77. };
  78. struct adis16475_chip_info {
  79. const struct iio_chan_spec *channels;
  80. const struct adis16475_sync *sync;
  81. const struct adis_data adis_data;
  82. const char *name;
  83. u32 num_channels;
  84. u32 gyro_max_val;
  85. u32 gyro_max_scale;
  86. u32 accel_max_val;
  87. u32 accel_max_scale;
  88. u32 temp_scale;
  89. u32 int_clk;
  90. u16 max_dec;
  91. u8 num_sync;
  92. bool has_burst32;
  93. };
  94. struct adis16475 {
  95. const struct adis16475_chip_info *info;
  96. struct adis adis;
  97. u32 clk_freq;
  98. bool burst32;
  99. unsigned long lsb_flag;
  100. u16 sync_mode;
  101. /* Alignment needed for the timestamp */
  102. __be16 data[ADIS16475_MAX_SCAN_DATA] __aligned(8);
  103. };
  104. enum {
  105. ADIS16475_SCAN_GYRO_X,
  106. ADIS16475_SCAN_GYRO_Y,
  107. ADIS16475_SCAN_GYRO_Z,
  108. ADIS16475_SCAN_ACCEL_X,
  109. ADIS16475_SCAN_ACCEL_Y,
  110. ADIS16475_SCAN_ACCEL_Z,
  111. ADIS16475_SCAN_TEMP,
  112. ADIS16475_SCAN_DIAG_S_FLAGS,
  113. ADIS16475_SCAN_CRC_FAILURE,
  114. };
  115. static bool low_rate_allow;
  116. module_param(low_rate_allow, bool, 0444);
  117. MODULE_PARM_DESC(low_rate_allow,
  118. "Allow IMU rates below the minimum advisable when external clk is used in SCALED mode (default: N)");
  119. #ifdef CONFIG_DEBUG_FS
  120. static ssize_t adis16475_show_firmware_revision(struct file *file,
  121. char __user *userbuf,
  122. size_t count, loff_t *ppos)
  123. {
  124. struct adis16475 *st = file->private_data;
  125. char buf[7];
  126. size_t len;
  127. u16 rev;
  128. int ret;
  129. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_REV, &rev);
  130. if (ret)
  131. return ret;
  132. len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
  133. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  134. }
  135. static const struct file_operations adis16475_firmware_revision_fops = {
  136. .open = simple_open,
  137. .read = adis16475_show_firmware_revision,
  138. .llseek = default_llseek,
  139. .owner = THIS_MODULE,
  140. };
  141. static ssize_t adis16475_show_firmware_date(struct file *file,
  142. char __user *userbuf,
  143. size_t count, loff_t *ppos)
  144. {
  145. struct adis16475 *st = file->private_data;
  146. u16 md, year;
  147. char buf[12];
  148. size_t len;
  149. int ret;
  150. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_Y, &year);
  151. if (ret)
  152. return ret;
  153. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_DM, &md);
  154. if (ret)
  155. return ret;
  156. len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n", md >> 8, md & 0xff,
  157. year);
  158. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  159. }
  160. static const struct file_operations adis16475_firmware_date_fops = {
  161. .open = simple_open,
  162. .read = adis16475_show_firmware_date,
  163. .llseek = default_llseek,
  164. .owner = THIS_MODULE,
  165. };
  166. static int adis16475_show_serial_number(void *arg, u64 *val)
  167. {
  168. struct adis16475 *st = arg;
  169. u16 serial;
  170. int ret;
  171. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_SERIAL_NUM, &serial);
  172. if (ret)
  173. return ret;
  174. *val = serial;
  175. return 0;
  176. }
  177. DEFINE_DEBUGFS_ATTRIBUTE(adis16475_serial_number_fops,
  178. adis16475_show_serial_number, NULL, "0x%.4llx\n");
  179. static int adis16475_show_product_id(void *arg, u64 *val)
  180. {
  181. struct adis16475 *st = arg;
  182. u16 prod_id;
  183. int ret;
  184. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_PROD_ID, &prod_id);
  185. if (ret)
  186. return ret;
  187. *val = prod_id;
  188. return 0;
  189. }
  190. DEFINE_DEBUGFS_ATTRIBUTE(adis16475_product_id_fops,
  191. adis16475_show_product_id, NULL, "%llu\n");
  192. static int adis16475_show_flash_count(void *arg, u64 *val)
  193. {
  194. struct adis16475 *st = arg;
  195. u32 flash_count;
  196. int ret;
  197. ret = adis_read_reg_32(&st->adis, ADIS16475_REG_FLASH_CNT,
  198. &flash_count);
  199. if (ret)
  200. return ret;
  201. *val = flash_count;
  202. return 0;
  203. }
  204. DEFINE_DEBUGFS_ATTRIBUTE(adis16475_flash_count_fops,
  205. adis16475_show_flash_count, NULL, "%lld\n");
  206. static void adis16475_debugfs_init(struct iio_dev *indio_dev)
  207. {
  208. struct adis16475 *st = iio_priv(indio_dev);
  209. struct dentry *d = iio_get_debugfs_dentry(indio_dev);
  210. debugfs_create_file_unsafe("serial_number", 0400,
  211. d, st, &adis16475_serial_number_fops);
  212. debugfs_create_file_unsafe("product_id", 0400,
  213. d, st, &adis16475_product_id_fops);
  214. debugfs_create_file_unsafe("flash_count", 0400,
  215. d, st, &adis16475_flash_count_fops);
  216. debugfs_create_file("firmware_revision", 0400,
  217. d, st, &adis16475_firmware_revision_fops);
  218. debugfs_create_file("firmware_date", 0400, d,
  219. st, &adis16475_firmware_date_fops);
  220. }
  221. #else
  222. static void adis16475_debugfs_init(struct iio_dev *indio_dev)
  223. {
  224. }
  225. #endif
  226. static int adis16475_get_freq(struct adis16475 *st, u32 *freq)
  227. {
  228. int ret;
  229. u16 dec;
  230. u32 sample_rate = st->clk_freq;
  231. adis_dev_lock(&st->adis);
  232. if (st->sync_mode == ADIS16475_SYNC_SCALED) {
  233. u16 sync_scale;
  234. ret = __adis_read_reg_16(&st->adis, ADIS16475_REG_UP_SCALE, &sync_scale);
  235. if (ret)
  236. goto error;
  237. sample_rate = st->clk_freq * sync_scale;
  238. }
  239. ret = __adis_read_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, &dec);
  240. if (ret)
  241. goto error;
  242. adis_dev_unlock(&st->adis);
  243. *freq = DIV_ROUND_CLOSEST(sample_rate, dec + 1);
  244. return 0;
  245. error:
  246. adis_dev_unlock(&st->adis);
  247. return ret;
  248. }
  249. static int adis16475_set_freq(struct adis16475 *st, const u32 freq)
  250. {
  251. u16 dec;
  252. int ret;
  253. u32 sample_rate = st->clk_freq;
  254. if (!freq)
  255. return -EINVAL;
  256. adis_dev_lock(&st->adis);
  257. /*
  258. * When using sync scaled mode, the input clock needs to be scaled so that we have
  259. * an IMU sample rate between (optimally) 1900 and 2100. After this, we can use the
  260. * decimation filter to lower the sampling rate in order to get what the user wants.
  261. * Optimally, the user sample rate is a multiple of both the IMU sample rate and
  262. * the input clock. Hence, calculating the sync_scale dynamically gives us better
  263. * chances of achieving a perfect/integer value for DEC_RATE. The math here is:
  264. * 1. lcm of the input clock and the desired output rate.
  265. * 2. get the highest multiple of the previous result lower than the adis max rate.
  266. * 3. The last result becomes the IMU sample rate. Use that to calculate SYNC_SCALE
  267. * and DEC_RATE (to get the user output rate)
  268. */
  269. if (st->sync_mode == ADIS16475_SYNC_SCALED) {
  270. unsigned long scaled_rate = lcm(st->clk_freq, freq);
  271. int sync_scale;
  272. /*
  273. * If lcm is bigger than the IMU maximum sampling rate there's no perfect
  274. * solution. In this case, we get the highest multiple of the input clock
  275. * lower than the IMU max sample rate.
  276. */
  277. if (scaled_rate > 2100000)
  278. scaled_rate = 2100000 / st->clk_freq * st->clk_freq;
  279. else
  280. scaled_rate = 2100000 / scaled_rate * scaled_rate;
  281. /*
  282. * This is not an hard requirement but it's not advised to run the IMU
  283. * with a sample rate lower than 4000Hz due to possible undersampling
  284. * issues. However, there are users that might really want to take the risk.
  285. * Hence, we provide a module parameter for them. If set, we allow sample
  286. * rates lower than 4KHz. By default, we won't allow this and we just roundup
  287. * the rate to the next multiple of the input clock bigger than 4KHz. This
  288. * is done like this as in some cases (when DEC_RATE is 0) might give
  289. * us the closest value to the one desired by the user...
  290. */
  291. if (scaled_rate < 1900000 && !low_rate_allow)
  292. scaled_rate = roundup(1900000, st->clk_freq);
  293. sync_scale = scaled_rate / st->clk_freq;
  294. ret = __adis_write_reg_16(&st->adis, ADIS16475_REG_UP_SCALE, sync_scale);
  295. if (ret)
  296. goto error;
  297. sample_rate = scaled_rate;
  298. }
  299. dec = DIV_ROUND_CLOSEST(sample_rate, freq);
  300. if (dec)
  301. dec--;
  302. if (dec > st->info->max_dec)
  303. dec = st->info->max_dec;
  304. ret = __adis_write_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, dec);
  305. if (ret)
  306. goto error;
  307. adis_dev_unlock(&st->adis);
  308. /*
  309. * If decimation is used, then gyro and accel data will have meaningful
  310. * bits on the LSB registers. This info is used on the trigger handler.
  311. */
  312. assign_bit(ADIS16475_LSB_DEC_MASK, &st->lsb_flag, dec);
  313. return 0;
  314. error:
  315. adis_dev_unlock(&st->adis);
  316. return ret;
  317. }
  318. /* The values are approximated. */
  319. static const u32 adis16475_3db_freqs[] = {
  320. [0] = 720, /* Filter disabled, full BW (~720Hz) */
  321. [1] = 360,
  322. [2] = 164,
  323. [3] = 80,
  324. [4] = 40,
  325. [5] = 20,
  326. [6] = 10,
  327. };
  328. static int adis16475_get_filter(struct adis16475 *st, u32 *filter)
  329. {
  330. u16 filter_sz;
  331. int ret;
  332. const int mask = ADIS16475_FILT_CTRL_MASK;
  333. ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL, &filter_sz);
  334. if (ret)
  335. return ret;
  336. *filter = adis16475_3db_freqs[filter_sz & mask];
  337. return 0;
  338. }
  339. static int adis16475_set_filter(struct adis16475 *st, const u32 filter)
  340. {
  341. int i = ARRAY_SIZE(adis16475_3db_freqs);
  342. int ret;
  343. while (--i) {
  344. if (adis16475_3db_freqs[i] >= filter)
  345. break;
  346. }
  347. ret = adis_write_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL,
  348. ADIS16475_FILT_CTRL(i));
  349. if (ret)
  350. return ret;
  351. /*
  352. * If FIR is used, then gyro and accel data will have meaningful
  353. * bits on the LSB registers. This info is used on the trigger handler.
  354. */
  355. assign_bit(ADIS16475_LSB_FIR_MASK, &st->lsb_flag, i);
  356. return 0;
  357. }
  358. static const u32 adis16475_calib_regs[] = {
  359. [ADIS16475_SCAN_GYRO_X] = ADIS16475_REG_X_GYRO_BIAS_L,
  360. [ADIS16475_SCAN_GYRO_Y] = ADIS16475_REG_Y_GYRO_BIAS_L,
  361. [ADIS16475_SCAN_GYRO_Z] = ADIS16475_REG_Z_GYRO_BIAS_L,
  362. [ADIS16475_SCAN_ACCEL_X] = ADIS16475_REG_X_ACCEL_BIAS_L,
  363. [ADIS16475_SCAN_ACCEL_Y] = ADIS16475_REG_Y_ACCEL_BIAS_L,
  364. [ADIS16475_SCAN_ACCEL_Z] = ADIS16475_REG_Z_ACCEL_BIAS_L,
  365. };
  366. static int adis16475_read_raw(struct iio_dev *indio_dev,
  367. const struct iio_chan_spec *chan,
  368. int *val, int *val2, long info)
  369. {
  370. struct adis16475 *st = iio_priv(indio_dev);
  371. int ret;
  372. u32 tmp;
  373. switch (info) {
  374. case IIO_CHAN_INFO_RAW:
  375. return adis_single_conversion(indio_dev, chan, 0, val);
  376. case IIO_CHAN_INFO_SCALE:
  377. switch (chan->type) {
  378. case IIO_ANGL_VEL:
  379. *val = st->info->gyro_max_val;
  380. *val2 = st->info->gyro_max_scale;
  381. return IIO_VAL_FRACTIONAL;
  382. case IIO_ACCEL:
  383. *val = st->info->accel_max_val;
  384. *val2 = st->info->accel_max_scale;
  385. return IIO_VAL_FRACTIONAL;
  386. case IIO_TEMP:
  387. *val = st->info->temp_scale;
  388. return IIO_VAL_INT;
  389. default:
  390. return -EINVAL;
  391. }
  392. case IIO_CHAN_INFO_CALIBBIAS:
  393. ret = adis_read_reg_32(&st->adis,
  394. adis16475_calib_regs[chan->scan_index],
  395. val);
  396. if (ret)
  397. return ret;
  398. return IIO_VAL_INT;
  399. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  400. ret = adis16475_get_filter(st, val);
  401. if (ret)
  402. return ret;
  403. return IIO_VAL_INT;
  404. case IIO_CHAN_INFO_SAMP_FREQ:
  405. ret = adis16475_get_freq(st, &tmp);
  406. if (ret)
  407. return ret;
  408. *val = tmp / 1000;
  409. *val2 = (tmp % 1000) * 1000;
  410. return IIO_VAL_INT_PLUS_MICRO;
  411. default:
  412. return -EINVAL;
  413. }
  414. }
  415. static int adis16475_write_raw(struct iio_dev *indio_dev,
  416. const struct iio_chan_spec *chan,
  417. int val, int val2, long info)
  418. {
  419. struct adis16475 *st = iio_priv(indio_dev);
  420. u32 tmp;
  421. switch (info) {
  422. case IIO_CHAN_INFO_SAMP_FREQ:
  423. tmp = val * 1000 + val2 / 1000;
  424. return adis16475_set_freq(st, tmp);
  425. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  426. return adis16475_set_filter(st, val);
  427. case IIO_CHAN_INFO_CALIBBIAS:
  428. return adis_write_reg_32(&st->adis,
  429. adis16475_calib_regs[chan->scan_index],
  430. val);
  431. default:
  432. return -EINVAL;
  433. }
  434. }
  435. #define ADIS16475_MOD_CHAN(_type, _mod, _address, _si, _r_bits, _s_bits) \
  436. { \
  437. .type = (_type), \
  438. .modified = 1, \
  439. .channel2 = (_mod), \
  440. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  441. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  442. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  443. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  444. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
  445. .address = (_address), \
  446. .scan_index = (_si), \
  447. .scan_type = { \
  448. .sign = 's', \
  449. .realbits = (_r_bits), \
  450. .storagebits = (_s_bits), \
  451. .endianness = IIO_BE, \
  452. }, \
  453. }
  454. #define ADIS16475_GYRO_CHANNEL(_mod) \
  455. ADIS16475_MOD_CHAN(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
  456. ADIS16475_REG_ ## _mod ## _GYRO_L, \
  457. ADIS16475_SCAN_GYRO_ ## _mod, 32, 32)
  458. #define ADIS16475_ACCEL_CHANNEL(_mod) \
  459. ADIS16475_MOD_CHAN(IIO_ACCEL, IIO_MOD_ ## _mod, \
  460. ADIS16475_REG_ ## _mod ## _ACCEL_L, \
  461. ADIS16475_SCAN_ACCEL_ ## _mod, 32, 32)
  462. #define ADIS16475_TEMP_CHANNEL() { \
  463. .type = IIO_TEMP, \
  464. .indexed = 1, \
  465. .channel = 0, \
  466. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  467. BIT(IIO_CHAN_INFO_SCALE), \
  468. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  469. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
  470. .address = ADIS16475_REG_TEMP_OUT, \
  471. .scan_index = ADIS16475_SCAN_TEMP, \
  472. .scan_type = { \
  473. .sign = 's', \
  474. .realbits = 16, \
  475. .storagebits = 16, \
  476. .endianness = IIO_BE, \
  477. }, \
  478. }
  479. static const struct iio_chan_spec adis16475_channels[] = {
  480. ADIS16475_GYRO_CHANNEL(X),
  481. ADIS16475_GYRO_CHANNEL(Y),
  482. ADIS16475_GYRO_CHANNEL(Z),
  483. ADIS16475_ACCEL_CHANNEL(X),
  484. ADIS16475_ACCEL_CHANNEL(Y),
  485. ADIS16475_ACCEL_CHANNEL(Z),
  486. ADIS16475_TEMP_CHANNEL(),
  487. IIO_CHAN_SOFT_TIMESTAMP(7)
  488. };
  489. enum adis16475_variant {
  490. ADIS16470,
  491. ADIS16475_1,
  492. ADIS16475_2,
  493. ADIS16475_3,
  494. ADIS16477_1,
  495. ADIS16477_2,
  496. ADIS16477_3,
  497. ADIS16465_1,
  498. ADIS16465_2,
  499. ADIS16465_3,
  500. ADIS16467_1,
  501. ADIS16467_2,
  502. ADIS16467_3,
  503. ADIS16500,
  504. ADIS16505_1,
  505. ADIS16505_2,
  506. ADIS16505_3,
  507. ADIS16507_1,
  508. ADIS16507_2,
  509. ADIS16507_3,
  510. };
  511. enum {
  512. ADIS16475_DIAG_STAT_DATA_PATH = 1,
  513. ADIS16475_DIAG_STAT_FLASH_MEM,
  514. ADIS16475_DIAG_STAT_SPI,
  515. ADIS16475_DIAG_STAT_STANDBY,
  516. ADIS16475_DIAG_STAT_SENSOR,
  517. ADIS16475_DIAG_STAT_MEMORY,
  518. ADIS16475_DIAG_STAT_CLK,
  519. };
  520. static const char * const adis16475_status_error_msgs[] = {
  521. [ADIS16475_DIAG_STAT_DATA_PATH] = "Data Path Overrun",
  522. [ADIS16475_DIAG_STAT_FLASH_MEM] = "Flash memory update failure",
  523. [ADIS16475_DIAG_STAT_SPI] = "SPI communication error",
  524. [ADIS16475_DIAG_STAT_STANDBY] = "Standby mode",
  525. [ADIS16475_DIAG_STAT_SENSOR] = "Sensor failure",
  526. [ADIS16475_DIAG_STAT_MEMORY] = "Memory failure",
  527. [ADIS16475_DIAG_STAT_CLK] = "Clock error",
  528. };
  529. #define ADIS16475_DATA(_prod_id, _timeouts) \
  530. { \
  531. .msc_ctrl_reg = ADIS16475_REG_MSG_CTRL, \
  532. .glob_cmd_reg = ADIS16475_REG_GLOB_CMD, \
  533. .diag_stat_reg = ADIS16475_REG_DIAG_STAT, \
  534. .prod_id_reg = ADIS16475_REG_PROD_ID, \
  535. .prod_id = (_prod_id), \
  536. .self_test_mask = BIT(2), \
  537. .self_test_reg = ADIS16475_REG_GLOB_CMD, \
  538. .cs_change_delay = 16, \
  539. .read_delay = 5, \
  540. .write_delay = 5, \
  541. .status_error_msgs = adis16475_status_error_msgs, \
  542. .status_error_mask = BIT(ADIS16475_DIAG_STAT_DATA_PATH) | \
  543. BIT(ADIS16475_DIAG_STAT_FLASH_MEM) | \
  544. BIT(ADIS16475_DIAG_STAT_SPI) | \
  545. BIT(ADIS16475_DIAG_STAT_STANDBY) | \
  546. BIT(ADIS16475_DIAG_STAT_SENSOR) | \
  547. BIT(ADIS16475_DIAG_STAT_MEMORY) | \
  548. BIT(ADIS16475_DIAG_STAT_CLK), \
  549. .unmasked_drdy = true, \
  550. .timeouts = (_timeouts), \
  551. .burst_reg_cmd = ADIS16475_REG_GLOB_CMD, \
  552. .burst_len = ADIS16475_BURST_MAX_DATA, \
  553. .burst_max_len = ADIS16475_BURST32_MAX_DATA, \
  554. .burst_max_speed_hz = ADIS16475_BURST_MAX_SPEED \
  555. }
  556. static const struct adis16475_sync adis16475_sync_mode[] = {
  557. { ADIS16475_SYNC_OUTPUT },
  558. { ADIS16475_SYNC_DIRECT, 1900, 2100 },
  559. { ADIS16475_SYNC_SCALED, 1, 128 },
  560. { ADIS16475_SYNC_PULSE, 1000, 2100 },
  561. };
  562. static const struct adis_timeout adis16475_timeouts = {
  563. .reset_ms = 200,
  564. .sw_reset_ms = 200,
  565. .self_test_ms = 20,
  566. };
  567. static const struct adis_timeout adis1650x_timeouts = {
  568. .reset_ms = 260,
  569. .sw_reset_ms = 260,
  570. .self_test_ms = 30,
  571. };
  572. static const struct adis16475_chip_info adis16475_chip_info[] = {
  573. [ADIS16470] = {
  574. .name = "adis16470",
  575. .num_channels = ARRAY_SIZE(adis16475_channels),
  576. .channels = adis16475_channels,
  577. .gyro_max_val = 1,
  578. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  579. .accel_max_val = 1,
  580. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  581. .temp_scale = 100,
  582. .int_clk = 2000,
  583. .max_dec = 1999,
  584. .sync = adis16475_sync_mode,
  585. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  586. .adis_data = ADIS16475_DATA(16470, &adis16475_timeouts),
  587. },
  588. [ADIS16475_1] = {
  589. .name = "adis16475-1",
  590. .num_channels = ARRAY_SIZE(adis16475_channels),
  591. .channels = adis16475_channels,
  592. .gyro_max_val = 1,
  593. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  594. .accel_max_val = 1,
  595. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  596. .temp_scale = 100,
  597. .int_clk = 2000,
  598. .max_dec = 1999,
  599. .sync = adis16475_sync_mode,
  600. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  601. .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
  602. },
  603. [ADIS16475_2] = {
  604. .name = "adis16475-2",
  605. .num_channels = ARRAY_SIZE(adis16475_channels),
  606. .channels = adis16475_channels,
  607. .gyro_max_val = 1,
  608. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  609. .accel_max_val = 1,
  610. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  611. .temp_scale = 100,
  612. .int_clk = 2000,
  613. .max_dec = 1999,
  614. .sync = adis16475_sync_mode,
  615. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  616. .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
  617. },
  618. [ADIS16475_3] = {
  619. .name = "adis16475-3",
  620. .num_channels = ARRAY_SIZE(adis16475_channels),
  621. .channels = adis16475_channels,
  622. .gyro_max_val = 1,
  623. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  624. .accel_max_val = 1,
  625. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  626. .temp_scale = 100,
  627. .int_clk = 2000,
  628. .max_dec = 1999,
  629. .sync = adis16475_sync_mode,
  630. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  631. .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
  632. },
  633. [ADIS16477_1] = {
  634. .name = "adis16477-1",
  635. .num_channels = ARRAY_SIZE(adis16475_channels),
  636. .channels = adis16475_channels,
  637. .gyro_max_val = 1,
  638. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  639. .accel_max_val = 1,
  640. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  641. .temp_scale = 100,
  642. .int_clk = 2000,
  643. .max_dec = 1999,
  644. .sync = adis16475_sync_mode,
  645. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  646. .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
  647. },
  648. [ADIS16477_2] = {
  649. .name = "adis16477-2",
  650. .num_channels = ARRAY_SIZE(adis16475_channels),
  651. .channels = adis16475_channels,
  652. .gyro_max_val = 1,
  653. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  654. .accel_max_val = 1,
  655. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  656. .temp_scale = 100,
  657. .int_clk = 2000,
  658. .max_dec = 1999,
  659. .sync = adis16475_sync_mode,
  660. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  661. .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
  662. },
  663. [ADIS16477_3] = {
  664. .name = "adis16477-3",
  665. .num_channels = ARRAY_SIZE(adis16475_channels),
  666. .channels = adis16475_channels,
  667. .gyro_max_val = 1,
  668. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  669. .accel_max_val = 1,
  670. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  671. .temp_scale = 100,
  672. .int_clk = 2000,
  673. .max_dec = 1999,
  674. .sync = adis16475_sync_mode,
  675. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  676. .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
  677. },
  678. [ADIS16465_1] = {
  679. .name = "adis16465-1",
  680. .num_channels = ARRAY_SIZE(adis16475_channels),
  681. .channels = adis16475_channels,
  682. .gyro_max_val = 1,
  683. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  684. .accel_max_val = 1,
  685. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  686. .temp_scale = 100,
  687. .int_clk = 2000,
  688. .max_dec = 1999,
  689. .sync = adis16475_sync_mode,
  690. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  691. .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
  692. },
  693. [ADIS16465_2] = {
  694. .name = "adis16465-2",
  695. .num_channels = ARRAY_SIZE(adis16475_channels),
  696. .channels = adis16475_channels,
  697. .gyro_max_val = 1,
  698. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  699. .accel_max_val = 1,
  700. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  701. .temp_scale = 100,
  702. .int_clk = 2000,
  703. .max_dec = 1999,
  704. .sync = adis16475_sync_mode,
  705. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  706. .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
  707. },
  708. [ADIS16465_3] = {
  709. .name = "adis16465-3",
  710. .num_channels = ARRAY_SIZE(adis16475_channels),
  711. .channels = adis16475_channels,
  712. .gyro_max_val = 1,
  713. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  714. .accel_max_val = 1,
  715. .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
  716. .temp_scale = 100,
  717. .int_clk = 2000,
  718. .max_dec = 1999,
  719. .sync = adis16475_sync_mode,
  720. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  721. .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
  722. },
  723. [ADIS16467_1] = {
  724. .name = "adis16467-1",
  725. .num_channels = ARRAY_SIZE(adis16475_channels),
  726. .channels = adis16475_channels,
  727. .gyro_max_val = 1,
  728. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  729. .accel_max_val = 1,
  730. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  731. .temp_scale = 100,
  732. .int_clk = 2000,
  733. .max_dec = 1999,
  734. .sync = adis16475_sync_mode,
  735. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  736. .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
  737. },
  738. [ADIS16467_2] = {
  739. .name = "adis16467-2",
  740. .num_channels = ARRAY_SIZE(adis16475_channels),
  741. .channels = adis16475_channels,
  742. .gyro_max_val = 1,
  743. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  744. .accel_max_val = 1,
  745. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  746. .temp_scale = 100,
  747. .int_clk = 2000,
  748. .max_dec = 1999,
  749. .sync = adis16475_sync_mode,
  750. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  751. .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
  752. },
  753. [ADIS16467_3] = {
  754. .name = "adis16467-3",
  755. .num_channels = ARRAY_SIZE(adis16475_channels),
  756. .channels = adis16475_channels,
  757. .gyro_max_val = 1,
  758. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  759. .accel_max_val = 1,
  760. .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
  761. .temp_scale = 100,
  762. .int_clk = 2000,
  763. .max_dec = 1999,
  764. .sync = adis16475_sync_mode,
  765. .num_sync = ARRAY_SIZE(adis16475_sync_mode),
  766. .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
  767. },
  768. [ADIS16500] = {
  769. .name = "adis16500",
  770. .num_channels = ARRAY_SIZE(adis16475_channels),
  771. .channels = adis16475_channels,
  772. .gyro_max_val = 1,
  773. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  774. .accel_max_val = 392,
  775. .accel_max_scale = 32000 << 16,
  776. .temp_scale = 100,
  777. .int_clk = 2000,
  778. .max_dec = 1999,
  779. .sync = adis16475_sync_mode,
  780. /* pulse sync not supported */
  781. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  782. .has_burst32 = true,
  783. .adis_data = ADIS16475_DATA(16500, &adis1650x_timeouts),
  784. },
  785. [ADIS16505_1] = {
  786. .name = "adis16505-1",
  787. .num_channels = ARRAY_SIZE(adis16475_channels),
  788. .channels = adis16475_channels,
  789. .gyro_max_val = 1,
  790. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  791. .accel_max_val = 78,
  792. .accel_max_scale = 32000 << 16,
  793. .temp_scale = 100,
  794. .int_clk = 2000,
  795. .max_dec = 1999,
  796. .sync = adis16475_sync_mode,
  797. /* pulse sync not supported */
  798. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  799. .has_burst32 = true,
  800. .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
  801. },
  802. [ADIS16505_2] = {
  803. .name = "adis16505-2",
  804. .num_channels = ARRAY_SIZE(adis16475_channels),
  805. .channels = adis16475_channels,
  806. .gyro_max_val = 1,
  807. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  808. .accel_max_val = 78,
  809. .accel_max_scale = 32000 << 16,
  810. .temp_scale = 100,
  811. .int_clk = 2000,
  812. .max_dec = 1999,
  813. .sync = adis16475_sync_mode,
  814. /* pulse sync not supported */
  815. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  816. .has_burst32 = true,
  817. .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
  818. },
  819. [ADIS16505_3] = {
  820. .name = "adis16505-3",
  821. .num_channels = ARRAY_SIZE(adis16475_channels),
  822. .channels = adis16475_channels,
  823. .gyro_max_val = 1,
  824. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  825. .accel_max_val = 78,
  826. .accel_max_scale = 32000 << 16,
  827. .temp_scale = 100,
  828. .int_clk = 2000,
  829. .max_dec = 1999,
  830. .sync = adis16475_sync_mode,
  831. /* pulse sync not supported */
  832. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  833. .has_burst32 = true,
  834. .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
  835. },
  836. [ADIS16507_1] = {
  837. .name = "adis16507-1",
  838. .num_channels = ARRAY_SIZE(adis16475_channels),
  839. .channels = adis16475_channels,
  840. .gyro_max_val = 1,
  841. .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
  842. .accel_max_val = 392,
  843. .accel_max_scale = 32000 << 16,
  844. .temp_scale = 100,
  845. .int_clk = 2000,
  846. .max_dec = 1999,
  847. .sync = adis16475_sync_mode,
  848. /* pulse sync not supported */
  849. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  850. .has_burst32 = true,
  851. .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
  852. },
  853. [ADIS16507_2] = {
  854. .name = "adis16507-2",
  855. .num_channels = ARRAY_SIZE(adis16475_channels),
  856. .channels = adis16475_channels,
  857. .gyro_max_val = 1,
  858. .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
  859. .accel_max_val = 392,
  860. .accel_max_scale = 32000 << 16,
  861. .temp_scale = 100,
  862. .int_clk = 2000,
  863. .max_dec = 1999,
  864. .sync = adis16475_sync_mode,
  865. /* pulse sync not supported */
  866. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  867. .has_burst32 = true,
  868. .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
  869. },
  870. [ADIS16507_3] = {
  871. .name = "adis16507-3",
  872. .num_channels = ARRAY_SIZE(adis16475_channels),
  873. .channels = adis16475_channels,
  874. .gyro_max_val = 1,
  875. .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
  876. .accel_max_val = 392,
  877. .accel_max_scale = 32000 << 16,
  878. .temp_scale = 100,
  879. .int_clk = 2000,
  880. .max_dec = 1999,
  881. .sync = adis16475_sync_mode,
  882. /* pulse sync not supported */
  883. .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
  884. .has_burst32 = true,
  885. .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
  886. },
  887. };
  888. static const struct iio_info adis16475_info = {
  889. .read_raw = &adis16475_read_raw,
  890. .write_raw = &adis16475_write_raw,
  891. .update_scan_mode = adis_update_scan_mode,
  892. .debugfs_reg_access = adis_debugfs_reg_access,
  893. };
  894. static bool adis16475_validate_crc(const u8 *buffer, u16 crc,
  895. const bool burst32)
  896. {
  897. int i;
  898. /* extra 6 elements for low gyro and accel */
  899. const u16 sz = burst32 ? ADIS16475_BURST32_MAX_DATA :
  900. ADIS16475_BURST_MAX_DATA;
  901. for (i = 0; i < sz - 2; i++)
  902. crc -= buffer[i];
  903. return crc == 0;
  904. }
  905. static void adis16475_burst32_check(struct adis16475 *st)
  906. {
  907. int ret;
  908. struct adis *adis = &st->adis;
  909. if (!st->info->has_burst32)
  910. return;
  911. if (st->lsb_flag && !st->burst32) {
  912. const u16 en = ADIS16500_BURST32(1);
  913. ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
  914. ADIS16500_BURST32_MASK, en);
  915. if (ret)
  916. return;
  917. st->burst32 = true;
  918. /*
  919. * In 32-bit mode we need extra 2 bytes for all gyro
  920. * and accel channels.
  921. */
  922. adis->burst_extra_len = 6 * sizeof(u16);
  923. adis->xfer[1].len += 6 * sizeof(u16);
  924. dev_dbg(&adis->spi->dev, "Enable burst32 mode, xfer:%d",
  925. adis->xfer[1].len);
  926. } else if (!st->lsb_flag && st->burst32) {
  927. const u16 en = ADIS16500_BURST32(0);
  928. ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
  929. ADIS16500_BURST32_MASK, en);
  930. if (ret)
  931. return;
  932. st->burst32 = false;
  933. /* Remove the extra bits */
  934. adis->burst_extra_len = 0;
  935. adis->xfer[1].len -= 6 * sizeof(u16);
  936. dev_dbg(&adis->spi->dev, "Disable burst32 mode, xfer:%d\n",
  937. adis->xfer[1].len);
  938. }
  939. }
  940. static irqreturn_t adis16475_trigger_handler(int irq, void *p)
  941. {
  942. struct iio_poll_func *pf = p;
  943. struct iio_dev *indio_dev = pf->indio_dev;
  944. struct adis16475 *st = iio_priv(indio_dev);
  945. struct adis *adis = &st->adis;
  946. int ret, bit, i = 0;
  947. __be16 *buffer;
  948. u16 crc;
  949. bool valid;
  950. /* offset until the first element after gyro and accel */
  951. const u8 offset = st->burst32 ? 13 : 7;
  952. ret = spi_sync(adis->spi, &adis->msg);
  953. if (ret)
  954. goto check_burst32;
  955. buffer = adis->buffer;
  956. crc = be16_to_cpu(buffer[offset + 2]);
  957. valid = adis16475_validate_crc(adis->buffer, crc, st->burst32);
  958. if (!valid) {
  959. dev_err(&adis->spi->dev, "Invalid crc\n");
  960. goto check_burst32;
  961. }
  962. for_each_set_bit(bit, indio_dev->active_scan_mask,
  963. indio_dev->masklength) {
  964. /*
  965. * When burst mode is used, system flags is the first data
  966. * channel in the sequence, but the scan index is 7.
  967. */
  968. switch (bit) {
  969. case ADIS16475_SCAN_TEMP:
  970. st->data[i++] = buffer[offset];
  971. break;
  972. case ADIS16475_SCAN_GYRO_X ... ADIS16475_SCAN_ACCEL_Z:
  973. /*
  974. * The first 2 bytes on the received data are the
  975. * DIAG_STAT reg, hence the +1 offset here...
  976. */
  977. if (st->burst32) {
  978. /* upper 16 */
  979. st->data[i++] = buffer[bit * 2 + 2];
  980. /* lower 16 */
  981. st->data[i++] = buffer[bit * 2 + 1];
  982. } else {
  983. st->data[i++] = buffer[bit + 1];
  984. /*
  985. * Don't bother in doing the manual read if the
  986. * device supports burst32. burst32 will be
  987. * enabled in the next call to
  988. * adis16475_burst32_check()...
  989. */
  990. if (st->lsb_flag && !st->info->has_burst32) {
  991. u16 val = 0;
  992. const u32 reg = ADIS16475_REG_X_GYRO_L +
  993. bit * 4;
  994. adis_read_reg_16(adis, reg, &val);
  995. st->data[i++] = cpu_to_be16(val);
  996. } else {
  997. /* lower not used */
  998. st->data[i++] = 0;
  999. }
  1000. }
  1001. break;
  1002. }
  1003. }
  1004. iio_push_to_buffers_with_timestamp(indio_dev, st->data, pf->timestamp);
  1005. check_burst32:
  1006. /*
  1007. * We only check the burst mode at the end of the current capture since
  1008. * it takes a full data ready cycle for the device to update the burst
  1009. * array.
  1010. */
  1011. adis16475_burst32_check(st);
  1012. iio_trigger_notify_done(indio_dev->trig);
  1013. return IRQ_HANDLED;
  1014. }
  1015. static int adis16475_config_sync_mode(struct adis16475 *st)
  1016. {
  1017. int ret;
  1018. struct device *dev = &st->adis.spi->dev;
  1019. const struct adis16475_sync *sync;
  1020. u32 sync_mode;
  1021. /* default to internal clk */
  1022. st->clk_freq = st->info->int_clk * 1000;
  1023. ret = device_property_read_u32(dev, "adi,sync-mode", &sync_mode);
  1024. if (ret)
  1025. return 0;
  1026. if (sync_mode >= st->info->num_sync) {
  1027. dev_err(dev, "Invalid sync mode: %u for %s\n", sync_mode,
  1028. st->info->name);
  1029. return -EINVAL;
  1030. }
  1031. sync = &st->info->sync[sync_mode];
  1032. st->sync_mode = sync->sync_mode;
  1033. /* All the other modes require external input signal */
  1034. if (sync->sync_mode != ADIS16475_SYNC_OUTPUT) {
  1035. struct clk *clk = devm_clk_get_enabled(dev, NULL);
  1036. if (IS_ERR(clk))
  1037. return PTR_ERR(clk);
  1038. st->clk_freq = clk_get_rate(clk);
  1039. if (st->clk_freq < sync->min_rate ||
  1040. st->clk_freq > sync->max_rate) {
  1041. dev_err(dev,
  1042. "Clk rate:%u not in a valid range:[%u %u]\n",
  1043. st->clk_freq, sync->min_rate, sync->max_rate);
  1044. return -EINVAL;
  1045. }
  1046. if (sync->sync_mode == ADIS16475_SYNC_SCALED) {
  1047. u16 up_scale;
  1048. /*
  1049. * In sync scaled mode, the IMU sample rate is the clk_freq * sync_scale.
  1050. * Hence, default the IMU sample rate to the highest multiple of the input
  1051. * clock lower than the IMU max sample rate. The optimal range is
  1052. * 1900-2100 sps...
  1053. */
  1054. up_scale = 2100 / st->clk_freq;
  1055. ret = __adis_write_reg_16(&st->adis,
  1056. ADIS16475_REG_UP_SCALE,
  1057. up_scale);
  1058. if (ret)
  1059. return ret;
  1060. }
  1061. st->clk_freq *= 1000;
  1062. }
  1063. /*
  1064. * Keep in mind that the mask for the clk modes in adis1650*
  1065. * chips is different (1100 instead of 11100). However, we
  1066. * are not configuring BIT(4) in these chips and the default
  1067. * value is 0, so we are fine in doing the below operations.
  1068. * I'm keeping this for simplicity and avoiding extra variables
  1069. * in chip_info.
  1070. */
  1071. ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
  1072. ADIS16475_SYNC_MODE_MASK, sync->sync_mode);
  1073. if (ret)
  1074. return ret;
  1075. usleep_range(250, 260);
  1076. return 0;
  1077. }
  1078. static int adis16475_config_irq_pin(struct adis16475 *st)
  1079. {
  1080. int ret;
  1081. struct irq_data *desc;
  1082. u32 irq_type;
  1083. u16 val = 0;
  1084. u8 polarity;
  1085. struct spi_device *spi = st->adis.spi;
  1086. desc = irq_get_irq_data(spi->irq);
  1087. if (!desc) {
  1088. dev_err(&spi->dev, "Could not find IRQ %d\n", spi->irq);
  1089. return -EINVAL;
  1090. }
  1091. /*
  1092. * It is possible to configure the data ready polarity. Furthermore, we
  1093. * need to update the adis struct if we want data ready as active low.
  1094. */
  1095. irq_type = irqd_get_trigger_type(desc);
  1096. if (irq_type == IRQ_TYPE_EDGE_RISING) {
  1097. polarity = 1;
  1098. st->adis.irq_flag = IRQF_TRIGGER_RISING;
  1099. } else if (irq_type == IRQ_TYPE_EDGE_FALLING) {
  1100. polarity = 0;
  1101. st->adis.irq_flag = IRQF_TRIGGER_FALLING;
  1102. } else {
  1103. dev_err(&spi->dev, "Invalid interrupt type 0x%x specified\n",
  1104. irq_type);
  1105. return -EINVAL;
  1106. }
  1107. val = ADIS16475_MSG_CTRL_DR_POL(polarity);
  1108. ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
  1109. ADIS16475_MSG_CTRL_DR_POL_MASK, val);
  1110. if (ret)
  1111. return ret;
  1112. /*
  1113. * There is a delay writing to any bits written to the MSC_CTRL
  1114. * register. It should not be bigger than 200us, so 250 should be more
  1115. * than enough!
  1116. */
  1117. usleep_range(250, 260);
  1118. return 0;
  1119. }
  1120. static const struct of_device_id adis16475_of_match[] = {
  1121. { .compatible = "adi,adis16470",
  1122. .data = &adis16475_chip_info[ADIS16470] },
  1123. { .compatible = "adi,adis16475-1",
  1124. .data = &adis16475_chip_info[ADIS16475_1] },
  1125. { .compatible = "adi,adis16475-2",
  1126. .data = &adis16475_chip_info[ADIS16475_2] },
  1127. { .compatible = "adi,adis16475-3",
  1128. .data = &adis16475_chip_info[ADIS16475_3] },
  1129. { .compatible = "adi,adis16477-1",
  1130. .data = &adis16475_chip_info[ADIS16477_1] },
  1131. { .compatible = "adi,adis16477-2",
  1132. .data = &adis16475_chip_info[ADIS16477_2] },
  1133. { .compatible = "adi,adis16477-3",
  1134. .data = &adis16475_chip_info[ADIS16477_3] },
  1135. { .compatible = "adi,adis16465-1",
  1136. .data = &adis16475_chip_info[ADIS16465_1] },
  1137. { .compatible = "adi,adis16465-2",
  1138. .data = &adis16475_chip_info[ADIS16465_2] },
  1139. { .compatible = "adi,adis16465-3",
  1140. .data = &adis16475_chip_info[ADIS16465_3] },
  1141. { .compatible = "adi,adis16467-1",
  1142. .data = &adis16475_chip_info[ADIS16467_1] },
  1143. { .compatible = "adi,adis16467-2",
  1144. .data = &adis16475_chip_info[ADIS16467_2] },
  1145. { .compatible = "adi,adis16467-3",
  1146. .data = &adis16475_chip_info[ADIS16467_3] },
  1147. { .compatible = "adi,adis16500",
  1148. .data = &adis16475_chip_info[ADIS16500] },
  1149. { .compatible = "adi,adis16505-1",
  1150. .data = &adis16475_chip_info[ADIS16505_1] },
  1151. { .compatible = "adi,adis16505-2",
  1152. .data = &adis16475_chip_info[ADIS16505_2] },
  1153. { .compatible = "adi,adis16505-3",
  1154. .data = &adis16475_chip_info[ADIS16505_3] },
  1155. { .compatible = "adi,adis16507-1",
  1156. .data = &adis16475_chip_info[ADIS16507_1] },
  1157. { .compatible = "adi,adis16507-2",
  1158. .data = &adis16475_chip_info[ADIS16507_2] },
  1159. { .compatible = "adi,adis16507-3",
  1160. .data = &adis16475_chip_info[ADIS16507_3] },
  1161. { },
  1162. };
  1163. MODULE_DEVICE_TABLE(of, adis16475_of_match);
  1164. static int adis16475_probe(struct spi_device *spi)
  1165. {
  1166. struct iio_dev *indio_dev;
  1167. struct adis16475 *st;
  1168. int ret;
  1169. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  1170. if (!indio_dev)
  1171. return -ENOMEM;
  1172. st = iio_priv(indio_dev);
  1173. st->info = device_get_match_data(&spi->dev);
  1174. if (!st->info)
  1175. return -EINVAL;
  1176. ret = adis_init(&st->adis, indio_dev, spi, &st->info->adis_data);
  1177. if (ret)
  1178. return ret;
  1179. indio_dev->name = st->info->name;
  1180. indio_dev->channels = st->info->channels;
  1181. indio_dev->num_channels = st->info->num_channels;
  1182. indio_dev->info = &adis16475_info;
  1183. indio_dev->modes = INDIO_DIRECT_MODE;
  1184. ret = __adis_initial_startup(&st->adis);
  1185. if (ret)
  1186. return ret;
  1187. ret = adis16475_config_irq_pin(st);
  1188. if (ret)
  1189. return ret;
  1190. ret = adis16475_config_sync_mode(st);
  1191. if (ret)
  1192. return ret;
  1193. ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev,
  1194. adis16475_trigger_handler);
  1195. if (ret)
  1196. return ret;
  1197. ret = devm_iio_device_register(&spi->dev, indio_dev);
  1198. if (ret)
  1199. return ret;
  1200. adis16475_debugfs_init(indio_dev);
  1201. return 0;
  1202. }
  1203. static struct spi_driver adis16475_driver = {
  1204. .driver = {
  1205. .name = "adis16475",
  1206. .of_match_table = adis16475_of_match,
  1207. },
  1208. .probe = adis16475_probe,
  1209. };
  1210. module_spi_driver(adis16475_driver);
  1211. MODULE_AUTHOR("Nuno Sa <[email protected]>");
  1212. MODULE_DESCRIPTION("Analog Devices ADIS16475 IMU driver");
  1213. MODULE_LICENSE("GPL");
  1214. MODULE_IMPORT_NS(IIO_ADISLIB);