bno055.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IIO driver for Bosch BNO055 IMU
  4. *
  5. * Copyright (C) 2021-2022 Istituto Italiano di Tecnologia
  6. * Electronic Design Laboratory
  7. * Written by Andrea Merello <[email protected]>
  8. *
  9. * Portions of this driver are taken from the BNO055 driver patch
  10. * from Vlad Dogaru which is Copyright (c) 2016, Intel Corporation.
  11. *
  12. * This driver is also based on BMI160 driver, which is:
  13. * Copyright (c) 2016, Intel Corporation.
  14. * Copyright (c) 2019, Martin Kelly.
  15. */
  16. #include <linux/bitfield.h>
  17. #include <linux/bitmap.h>
  18. #include <linux/clk.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/device.h>
  21. #include <linux/firmware.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/regmap.h>
  26. #include <linux/util_macros.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/iio.h>
  29. #include <linux/iio/sysfs.h>
  30. #include <linux/iio/trigger_consumer.h>
  31. #include <linux/iio/triggered_buffer.h>
  32. #include "bno055.h"
  33. #define BNO055_FW_UID_FMT "bno055-caldata-%*phN.dat"
  34. #define BNO055_FW_GENERIC_NAME "bno055-caldata.dat"
  35. /* common registers */
  36. #define BNO055_PAGESEL_REG 0x7
  37. /* page 0 registers */
  38. #define BNO055_CHIP_ID_REG 0x0
  39. #define BNO055_CHIP_ID_MAGIC 0xA0
  40. #define BNO055_SW_REV_LSB_REG 0x4
  41. #define BNO055_SW_REV_MSB_REG 0x5
  42. #define BNO055_ACC_DATA_X_LSB_REG 0x8
  43. #define BNO055_ACC_DATA_Y_LSB_REG 0xA
  44. #define BNO055_ACC_DATA_Z_LSB_REG 0xC
  45. #define BNO055_MAG_DATA_X_LSB_REG 0xE
  46. #define BNO055_MAG_DATA_Y_LSB_REG 0x10
  47. #define BNO055_MAG_DATA_Z_LSB_REG 0x12
  48. #define BNO055_GYR_DATA_X_LSB_REG 0x14
  49. #define BNO055_GYR_DATA_Y_LSB_REG 0x16
  50. #define BNO055_GYR_DATA_Z_LSB_REG 0x18
  51. #define BNO055_EUL_DATA_X_LSB_REG 0x1A
  52. #define BNO055_EUL_DATA_Y_LSB_REG 0x1C
  53. #define BNO055_EUL_DATA_Z_LSB_REG 0x1E
  54. #define BNO055_QUAT_DATA_W_LSB_REG 0x20
  55. #define BNO055_LIA_DATA_X_LSB_REG 0x28
  56. #define BNO055_LIA_DATA_Y_LSB_REG 0x2A
  57. #define BNO055_LIA_DATA_Z_LSB_REG 0x2C
  58. #define BNO055_GRAVITY_DATA_X_LSB_REG 0x2E
  59. #define BNO055_GRAVITY_DATA_Y_LSB_REG 0x30
  60. #define BNO055_GRAVITY_DATA_Z_LSB_REG 0x32
  61. #define BNO055_SCAN_CH_COUNT ((BNO055_GRAVITY_DATA_Z_LSB_REG - BNO055_ACC_DATA_X_LSB_REG) / 2)
  62. #define BNO055_TEMP_REG 0x34
  63. #define BNO055_CALIB_STAT_REG 0x35
  64. #define BNO055_CALIB_STAT_MAGN_SHIFT 0
  65. #define BNO055_CALIB_STAT_ACCEL_SHIFT 2
  66. #define BNO055_CALIB_STAT_GYRO_SHIFT 4
  67. #define BNO055_CALIB_STAT_SYS_SHIFT 6
  68. #define BNO055_SYS_ERR_REG 0x3A
  69. #define BNO055_POWER_MODE_REG 0x3E
  70. #define BNO055_POWER_MODE_NORMAL 0
  71. #define BNO055_SYS_TRIGGER_REG 0x3F
  72. #define BNO055_SYS_TRIGGER_RST_SYS BIT(5)
  73. #define BNO055_SYS_TRIGGER_CLK_SEL BIT(7)
  74. #define BNO055_OPR_MODE_REG 0x3D
  75. #define BNO055_OPR_MODE_CONFIG 0x0
  76. #define BNO055_OPR_MODE_AMG 0x7
  77. #define BNO055_OPR_MODE_FUSION_FMC_OFF 0xB
  78. #define BNO055_OPR_MODE_FUSION 0xC
  79. #define BNO055_UNIT_SEL_REG 0x3B
  80. /* Android orientation mode means: pitch value decreases turning clockwise */
  81. #define BNO055_UNIT_SEL_ANDROID BIT(7)
  82. #define BNO055_UNIT_SEL_GYR_RPS BIT(1)
  83. #define BNO055_CALDATA_START 0x55
  84. #define BNO055_CALDATA_END 0x6A
  85. #define BNO055_CALDATA_LEN 22
  86. /*
  87. * The difference in address between the register that contains the
  88. * value and the register that contains the offset. This applies for
  89. * accel, gyro and magn channels.
  90. */
  91. #define BNO055_REG_OFFSET_ADDR 0x4D
  92. /* page 1 registers */
  93. #define BNO055_PG1(x) ((x) | 0x80)
  94. #define BNO055_ACC_CONFIG_REG BNO055_PG1(0x8)
  95. #define BNO055_ACC_CONFIG_LPF_MASK GENMASK(4, 2)
  96. #define BNO055_ACC_CONFIG_RANGE_MASK GENMASK(1, 0)
  97. #define BNO055_MAG_CONFIG_REG BNO055_PG1(0x9)
  98. #define BNO055_MAG_CONFIG_HIGHACCURACY 0x18
  99. #define BNO055_MAG_CONFIG_ODR_MASK GENMASK(2, 0)
  100. #define BNO055_GYR_CONFIG_REG BNO055_PG1(0xA)
  101. #define BNO055_GYR_CONFIG_RANGE_MASK GENMASK(2, 0)
  102. #define BNO055_GYR_CONFIG_LPF_MASK GENMASK(5, 3)
  103. #define BNO055_GYR_AM_SET_REG BNO055_PG1(0x1F)
  104. #define BNO055_UID_LOWER_REG BNO055_PG1(0x50)
  105. #define BNO055_UID_HIGHER_REG BNO055_PG1(0x5F)
  106. #define BNO055_UID_LEN 16
  107. struct bno055_sysfs_attr {
  108. int *vals;
  109. int len;
  110. int *fusion_vals;
  111. int *hw_xlate;
  112. int type;
  113. };
  114. static int bno055_acc_lpf_vals[] = {
  115. 7, 810000, 15, 630000, 31, 250000, 62, 500000,
  116. 125, 0, 250, 0, 500, 0, 1000, 0,
  117. };
  118. static struct bno055_sysfs_attr bno055_acc_lpf = {
  119. .vals = bno055_acc_lpf_vals,
  120. .len = ARRAY_SIZE(bno055_acc_lpf_vals),
  121. .fusion_vals = (int[]){62, 500000},
  122. .type = IIO_VAL_INT_PLUS_MICRO,
  123. };
  124. static int bno055_acc_range_vals[] = {
  125. /* G: 2, 4, 8, 16 */
  126. 1962, 3924, 7848, 15696
  127. };
  128. static struct bno055_sysfs_attr bno055_acc_range = {
  129. .vals = bno055_acc_range_vals,
  130. .len = ARRAY_SIZE(bno055_acc_range_vals),
  131. .fusion_vals = (int[]){3924}, /* 4G */
  132. .type = IIO_VAL_INT,
  133. };
  134. /*
  135. * Theoretically the IMU should return data in a given (i.e. fixed) unit
  136. * regardless of the range setting. This happens for the accelerometer, but not
  137. * for the gyroscope; the gyroscope range setting affects the scale.
  138. * This is probably due to this[0] bug.
  139. * For this reason we map the internal range setting onto the standard IIO scale
  140. * attribute for gyro.
  141. * Since the bug[0] may be fixed in future, we check for the IMU FW version and
  142. * eventually warn the user.
  143. * Currently we just don't care about "range" attributes for gyro.
  144. *
  145. * [0] https://community.bosch-sensortec.com/t5/MEMS-sensors-forum/BNO055-Wrong-sensitivity-resolution-in-datasheet/td-p/10266
  146. */
  147. /*
  148. * dps = hwval * (dps_range/2^15)
  149. * rps = hwval * (rps_range/2^15)
  150. * = hwval * (dps_range/(2^15 * k))
  151. * where k is rad-to-deg factor
  152. */
  153. static int bno055_gyr_scale_vals[] = {
  154. 125, 1877467, 250, 1877467, 500, 1877467,
  155. 1000, 1877467, 2000, 1877467,
  156. };
  157. static struct bno055_sysfs_attr bno055_gyr_scale = {
  158. .vals = bno055_gyr_scale_vals,
  159. .len = ARRAY_SIZE(bno055_gyr_scale_vals),
  160. .fusion_vals = (int[]){1, 900},
  161. .hw_xlate = (int[]){4, 3, 2, 1, 0},
  162. .type = IIO_VAL_FRACTIONAL,
  163. };
  164. static int bno055_gyr_lpf_vals[] = {12, 23, 32, 47, 64, 116, 230, 523};
  165. static struct bno055_sysfs_attr bno055_gyr_lpf = {
  166. .vals = bno055_gyr_lpf_vals,
  167. .len = ARRAY_SIZE(bno055_gyr_lpf_vals),
  168. .fusion_vals = (int[]){32},
  169. .hw_xlate = (int[]){5, 4, 7, 3, 6, 2, 1, 0},
  170. .type = IIO_VAL_INT,
  171. };
  172. static int bno055_mag_odr_vals[] = {2, 6, 8, 10, 15, 20, 25, 30};
  173. static struct bno055_sysfs_attr bno055_mag_odr = {
  174. .vals = bno055_mag_odr_vals,
  175. .len = ARRAY_SIZE(bno055_mag_odr_vals),
  176. .fusion_vals = (int[]){20},
  177. .type = IIO_VAL_INT,
  178. };
  179. struct bno055_priv {
  180. struct regmap *regmap;
  181. struct device *dev;
  182. struct clk *clk;
  183. int operation_mode;
  184. int xfer_burst_break_thr;
  185. struct mutex lock;
  186. u8 uid[BNO055_UID_LEN];
  187. struct gpio_desc *reset_gpio;
  188. bool sw_reset;
  189. struct {
  190. __le16 chans[BNO055_SCAN_CH_COUNT];
  191. s64 timestamp __aligned(8);
  192. } buf;
  193. struct dentry *debugfs;
  194. };
  195. static bool bno055_regmap_volatile(struct device *dev, unsigned int reg)
  196. {
  197. /* data and status registers */
  198. if (reg >= BNO055_ACC_DATA_X_LSB_REG && reg <= BNO055_SYS_ERR_REG)
  199. return true;
  200. /* when in fusion mode, config is updated by chip */
  201. if (reg == BNO055_MAG_CONFIG_REG ||
  202. reg == BNO055_ACC_CONFIG_REG ||
  203. reg == BNO055_GYR_CONFIG_REG)
  204. return true;
  205. /* calibration data may be updated by the IMU */
  206. if (reg >= BNO055_CALDATA_START && reg <= BNO055_CALDATA_END)
  207. return true;
  208. return false;
  209. }
  210. static bool bno055_regmap_readable(struct device *dev, unsigned int reg)
  211. {
  212. /* unnamed PG0 reserved areas */
  213. if ((reg < BNO055_PG1(0) && reg > BNO055_CALDATA_END) ||
  214. reg == 0x3C)
  215. return false;
  216. /* unnamed PG1 reserved areas */
  217. if (reg > BNO055_PG1(BNO055_UID_HIGHER_REG) ||
  218. (reg < BNO055_PG1(BNO055_UID_LOWER_REG) && reg > BNO055_PG1(BNO055_GYR_AM_SET_REG)) ||
  219. reg == BNO055_PG1(0xE) ||
  220. (reg < BNO055_PG1(BNO055_PAGESEL_REG) && reg >= BNO055_PG1(0x0)))
  221. return false;
  222. return true;
  223. }
  224. static bool bno055_regmap_writeable(struct device *dev, unsigned int reg)
  225. {
  226. /*
  227. * Unreadable registers are indeed reserved; there are no WO regs
  228. * (except for a single bit in SYS_TRIGGER register)
  229. */
  230. if (!bno055_regmap_readable(dev, reg))
  231. return false;
  232. /* data and status registers */
  233. if (reg >= BNO055_ACC_DATA_X_LSB_REG && reg <= BNO055_SYS_ERR_REG)
  234. return false;
  235. /* ID areas */
  236. if (reg < BNO055_PAGESEL_REG ||
  237. (reg <= BNO055_UID_HIGHER_REG && reg >= BNO055_UID_LOWER_REG))
  238. return false;
  239. return true;
  240. }
  241. static const struct regmap_range_cfg bno055_regmap_ranges[] = {
  242. {
  243. .range_min = 0,
  244. .range_max = 0x7f * 2,
  245. .selector_reg = BNO055_PAGESEL_REG,
  246. .selector_mask = GENMASK(7, 0),
  247. .selector_shift = 0,
  248. .window_start = 0,
  249. .window_len = 0x80,
  250. },
  251. };
  252. const struct regmap_config bno055_regmap_config = {
  253. .name = "bno055",
  254. .reg_bits = 8,
  255. .val_bits = 8,
  256. .ranges = bno055_regmap_ranges,
  257. .num_ranges = 1,
  258. .volatile_reg = bno055_regmap_volatile,
  259. .max_register = 0x80 * 2,
  260. .writeable_reg = bno055_regmap_writeable,
  261. .readable_reg = bno055_regmap_readable,
  262. .cache_type = REGCACHE_RBTREE,
  263. };
  264. EXPORT_SYMBOL_NS_GPL(bno055_regmap_config, IIO_BNO055);
  265. /* must be called in configuration mode */
  266. static int bno055_calibration_load(struct bno055_priv *priv, const u8 *data, int len)
  267. {
  268. if (len != BNO055_CALDATA_LEN) {
  269. dev_dbg(priv->dev, "Invalid calibration file size %d (expected %d)",
  270. len, BNO055_CALDATA_LEN);
  271. return -EINVAL;
  272. }
  273. dev_dbg(priv->dev, "loading cal data: %*ph", BNO055_CALDATA_LEN, data);
  274. return regmap_bulk_write(priv->regmap, BNO055_CALDATA_START,
  275. data, BNO055_CALDATA_LEN);
  276. }
  277. static int bno055_operation_mode_do_set(struct bno055_priv *priv,
  278. int operation_mode)
  279. {
  280. int ret;
  281. ret = regmap_write(priv->regmap, BNO055_OPR_MODE_REG,
  282. operation_mode);
  283. if (ret)
  284. return ret;
  285. /* Following datasheet specifications: sensor takes 7mS up to 19 mS to switch mode */
  286. msleep(20);
  287. return 0;
  288. }
  289. static int bno055_system_reset(struct bno055_priv *priv)
  290. {
  291. int ret;
  292. if (priv->reset_gpio) {
  293. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  294. usleep_range(5000, 10000);
  295. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  296. } else if (priv->sw_reset) {
  297. ret = regmap_write(priv->regmap, BNO055_SYS_TRIGGER_REG,
  298. BNO055_SYS_TRIGGER_RST_SYS);
  299. if (ret)
  300. return ret;
  301. } else {
  302. return 0;
  303. }
  304. regcache_drop_region(priv->regmap, 0x0, 0xff);
  305. usleep_range(650000, 700000);
  306. return 0;
  307. }
  308. static int bno055_init(struct bno055_priv *priv, const u8 *caldata, int len)
  309. {
  310. int ret;
  311. ret = bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_CONFIG);
  312. if (ret)
  313. return ret;
  314. ret = regmap_write(priv->regmap, BNO055_POWER_MODE_REG,
  315. BNO055_POWER_MODE_NORMAL);
  316. if (ret)
  317. return ret;
  318. ret = regmap_write(priv->regmap, BNO055_SYS_TRIGGER_REG,
  319. priv->clk ? BNO055_SYS_TRIGGER_CLK_SEL : 0);
  320. if (ret)
  321. return ret;
  322. /* use standard SI units */
  323. ret = regmap_write(priv->regmap, BNO055_UNIT_SEL_REG,
  324. BNO055_UNIT_SEL_ANDROID | BNO055_UNIT_SEL_GYR_RPS);
  325. if (ret)
  326. return ret;
  327. if (caldata) {
  328. ret = bno055_calibration_load(priv, caldata, len);
  329. if (ret)
  330. dev_warn(priv->dev, "failed to load calibration data with error %d\n",
  331. ret);
  332. }
  333. return 0;
  334. }
  335. static ssize_t bno055_operation_mode_set(struct bno055_priv *priv,
  336. int operation_mode)
  337. {
  338. u8 caldata[BNO055_CALDATA_LEN];
  339. int ret;
  340. mutex_lock(&priv->lock);
  341. ret = bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_CONFIG);
  342. if (ret)
  343. goto exit_unlock;
  344. if (operation_mode == BNO055_OPR_MODE_FUSION ||
  345. operation_mode == BNO055_OPR_MODE_FUSION_FMC_OFF) {
  346. /* for entering fusion mode, reset the chip to clear the algo state */
  347. ret = regmap_bulk_read(priv->regmap, BNO055_CALDATA_START, caldata,
  348. BNO055_CALDATA_LEN);
  349. if (ret)
  350. goto exit_unlock;
  351. ret = bno055_system_reset(priv);
  352. if (ret)
  353. goto exit_unlock;
  354. ret = bno055_init(priv, caldata, BNO055_CALDATA_LEN);
  355. if (ret)
  356. goto exit_unlock;
  357. }
  358. ret = bno055_operation_mode_do_set(priv, operation_mode);
  359. if (ret)
  360. goto exit_unlock;
  361. priv->operation_mode = operation_mode;
  362. exit_unlock:
  363. mutex_unlock(&priv->lock);
  364. return ret;
  365. }
  366. static void bno055_uninit(void *arg)
  367. {
  368. struct bno055_priv *priv = arg;
  369. /* stop the IMU */
  370. bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_CONFIG);
  371. }
  372. #define BNO055_CHANNEL(_type, _axis, _index, _address, _sep, _sh, _avail) { \
  373. .address = _address, \
  374. .type = _type, \
  375. .modified = 1, \
  376. .channel2 = IIO_MOD_##_axis, \
  377. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | (_sep), \
  378. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | (_sh), \
  379. .info_mask_shared_by_type_available = _avail, \
  380. .scan_index = _index, \
  381. .scan_type = { \
  382. .sign = 's', \
  383. .realbits = 16, \
  384. .storagebits = 16, \
  385. .endianness = IIO_LE, \
  386. .repeat = IIO_MOD_##_axis == IIO_MOD_QUATERNION ? 4 : 0, \
  387. }, \
  388. }
  389. /* scan indexes follow DATA register order */
  390. enum bno055_scan_axis {
  391. BNO055_SCAN_ACCEL_X,
  392. BNO055_SCAN_ACCEL_Y,
  393. BNO055_SCAN_ACCEL_Z,
  394. BNO055_SCAN_MAGN_X,
  395. BNO055_SCAN_MAGN_Y,
  396. BNO055_SCAN_MAGN_Z,
  397. BNO055_SCAN_GYRO_X,
  398. BNO055_SCAN_GYRO_Y,
  399. BNO055_SCAN_GYRO_Z,
  400. BNO055_SCAN_YAW,
  401. BNO055_SCAN_ROLL,
  402. BNO055_SCAN_PITCH,
  403. BNO055_SCAN_QUATERNION,
  404. BNO055_SCAN_LIA_X,
  405. BNO055_SCAN_LIA_Y,
  406. BNO055_SCAN_LIA_Z,
  407. BNO055_SCAN_GRAVITY_X,
  408. BNO055_SCAN_GRAVITY_Y,
  409. BNO055_SCAN_GRAVITY_Z,
  410. BNO055_SCAN_TIMESTAMP,
  411. _BNO055_SCAN_MAX
  412. };
  413. static const struct iio_chan_spec bno055_channels[] = {
  414. /* accelerometer */
  415. BNO055_CHANNEL(IIO_ACCEL, X, BNO055_SCAN_ACCEL_X,
  416. BNO055_ACC_DATA_X_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  417. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  418. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)),
  419. BNO055_CHANNEL(IIO_ACCEL, Y, BNO055_SCAN_ACCEL_Y,
  420. BNO055_ACC_DATA_Y_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  421. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  422. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)),
  423. BNO055_CHANNEL(IIO_ACCEL, Z, BNO055_SCAN_ACCEL_Z,
  424. BNO055_ACC_DATA_Z_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  425. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  426. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)),
  427. /* gyroscope */
  428. BNO055_CHANNEL(IIO_ANGL_VEL, X, BNO055_SCAN_GYRO_X,
  429. BNO055_GYR_DATA_X_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  430. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  431. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) |
  432. BIT(IIO_CHAN_INFO_SCALE)),
  433. BNO055_CHANNEL(IIO_ANGL_VEL, Y, BNO055_SCAN_GYRO_Y,
  434. BNO055_GYR_DATA_Y_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  435. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  436. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) |
  437. BIT(IIO_CHAN_INFO_SCALE)),
  438. BNO055_CHANNEL(IIO_ANGL_VEL, Z, BNO055_SCAN_GYRO_Z,
  439. BNO055_GYR_DATA_Z_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  440. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  441. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) |
  442. BIT(IIO_CHAN_INFO_SCALE)),
  443. /* magnetometer */
  444. BNO055_CHANNEL(IIO_MAGN, X, BNO055_SCAN_MAGN_X,
  445. BNO055_MAG_DATA_X_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  446. BIT(IIO_CHAN_INFO_SAMP_FREQ), BIT(IIO_CHAN_INFO_SAMP_FREQ)),
  447. BNO055_CHANNEL(IIO_MAGN, Y, BNO055_SCAN_MAGN_Y,
  448. BNO055_MAG_DATA_Y_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  449. BIT(IIO_CHAN_INFO_SAMP_FREQ), BIT(IIO_CHAN_INFO_SAMP_FREQ)),
  450. BNO055_CHANNEL(IIO_MAGN, Z, BNO055_SCAN_MAGN_Z,
  451. BNO055_MAG_DATA_Z_LSB_REG, BIT(IIO_CHAN_INFO_OFFSET),
  452. BIT(IIO_CHAN_INFO_SAMP_FREQ), BIT(IIO_CHAN_INFO_SAMP_FREQ)),
  453. /* euler angle */
  454. BNO055_CHANNEL(IIO_ROT, YAW, BNO055_SCAN_YAW,
  455. BNO055_EUL_DATA_X_LSB_REG, 0, 0, 0),
  456. BNO055_CHANNEL(IIO_ROT, ROLL, BNO055_SCAN_ROLL,
  457. BNO055_EUL_DATA_Y_LSB_REG, 0, 0, 0),
  458. BNO055_CHANNEL(IIO_ROT, PITCH, BNO055_SCAN_PITCH,
  459. BNO055_EUL_DATA_Z_LSB_REG, 0, 0, 0),
  460. /* quaternion */
  461. BNO055_CHANNEL(IIO_ROT, QUATERNION, BNO055_SCAN_QUATERNION,
  462. BNO055_QUAT_DATA_W_LSB_REG, 0, 0, 0),
  463. /* linear acceleration */
  464. BNO055_CHANNEL(IIO_ACCEL, LINEAR_X, BNO055_SCAN_LIA_X,
  465. BNO055_LIA_DATA_X_LSB_REG, 0, 0, 0),
  466. BNO055_CHANNEL(IIO_ACCEL, LINEAR_Y, BNO055_SCAN_LIA_Y,
  467. BNO055_LIA_DATA_Y_LSB_REG, 0, 0, 0),
  468. BNO055_CHANNEL(IIO_ACCEL, LINEAR_Z, BNO055_SCAN_LIA_Z,
  469. BNO055_LIA_DATA_Z_LSB_REG, 0, 0, 0),
  470. /* gravity vector */
  471. BNO055_CHANNEL(IIO_GRAVITY, X, BNO055_SCAN_GRAVITY_X,
  472. BNO055_GRAVITY_DATA_X_LSB_REG, 0, 0, 0),
  473. BNO055_CHANNEL(IIO_GRAVITY, Y, BNO055_SCAN_GRAVITY_Y,
  474. BNO055_GRAVITY_DATA_Y_LSB_REG, 0, 0, 0),
  475. BNO055_CHANNEL(IIO_GRAVITY, Z, BNO055_SCAN_GRAVITY_Z,
  476. BNO055_GRAVITY_DATA_Z_LSB_REG, 0, 0, 0),
  477. {
  478. .type = IIO_TEMP,
  479. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  480. .scan_index = -1,
  481. },
  482. IIO_CHAN_SOFT_TIMESTAMP(BNO055_SCAN_TIMESTAMP),
  483. };
  484. static int bno055_get_regmask(struct bno055_priv *priv, int *val, int *val2,
  485. int reg, int mask, struct bno055_sysfs_attr *attr)
  486. {
  487. const int shift = __ffs(mask);
  488. int hwval, idx;
  489. int ret;
  490. int i;
  491. ret = regmap_read(priv->regmap, reg, &hwval);
  492. if (ret)
  493. return ret;
  494. idx = (hwval & mask) >> shift;
  495. if (attr->hw_xlate)
  496. for (i = 0; i < attr->len; i++)
  497. if (attr->hw_xlate[i] == idx) {
  498. idx = i;
  499. break;
  500. }
  501. if (attr->type == IIO_VAL_INT) {
  502. *val = attr->vals[idx];
  503. } else { /* IIO_VAL_INT_PLUS_MICRO or IIO_VAL_FRACTIONAL */
  504. *val = attr->vals[idx * 2];
  505. *val2 = attr->vals[idx * 2 + 1];
  506. }
  507. return attr->type;
  508. }
  509. static int bno055_set_regmask(struct bno055_priv *priv, int val, int val2,
  510. int reg, int mask, struct bno055_sysfs_attr *attr)
  511. {
  512. const int shift = __ffs(mask);
  513. int best_delta;
  514. int req_val;
  515. int tbl_val;
  516. bool first;
  517. int delta;
  518. int hwval;
  519. int ret;
  520. int len;
  521. int i;
  522. /*
  523. * The closest value the HW supports is only one in fusion mode,
  524. * and it is autoselected, so don't do anything, just return OK,
  525. * as the closest possible value has been (virtually) selected
  526. */
  527. if (priv->operation_mode != BNO055_OPR_MODE_AMG)
  528. return 0;
  529. len = attr->len;
  530. /*
  531. * We always get a request in INT_PLUS_MICRO, but we
  532. * take care of the micro part only when we really have
  533. * non-integer tables. This prevents 32-bit overflow with
  534. * larger integers contained in integer tables.
  535. */
  536. req_val = val;
  537. if (attr->type != IIO_VAL_INT) {
  538. len /= 2;
  539. req_val = min(val, 2147) * 1000000 + val2;
  540. }
  541. first = true;
  542. for (i = 0; i < len; i++) {
  543. switch (attr->type) {
  544. case IIO_VAL_INT:
  545. tbl_val = attr->vals[i];
  546. break;
  547. case IIO_VAL_INT_PLUS_MICRO:
  548. WARN_ON(attr->vals[i * 2] > 2147);
  549. tbl_val = attr->vals[i * 2] * 1000000 +
  550. attr->vals[i * 2 + 1];
  551. break;
  552. case IIO_VAL_FRACTIONAL:
  553. WARN_ON(attr->vals[i * 2] > 4294);
  554. tbl_val = attr->vals[i * 2] * 1000000 /
  555. attr->vals[i * 2 + 1];
  556. break;
  557. default:
  558. return -EINVAL;
  559. }
  560. delta = abs(tbl_val - req_val);
  561. if (first || delta < best_delta) {
  562. best_delta = delta;
  563. hwval = i;
  564. first = false;
  565. }
  566. }
  567. if (attr->hw_xlate)
  568. hwval = attr->hw_xlate[hwval];
  569. ret = bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_CONFIG);
  570. if (ret)
  571. return ret;
  572. ret = regmap_update_bits(priv->regmap, reg, mask, hwval << shift);
  573. if (ret)
  574. return ret;
  575. return bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_AMG);
  576. }
  577. static int bno055_read_simple_chan(struct iio_dev *indio_dev,
  578. struct iio_chan_spec const *chan,
  579. int *val, int *val2, long mask)
  580. {
  581. struct bno055_priv *priv = iio_priv(indio_dev);
  582. __le16 raw_val;
  583. int ret;
  584. switch (mask) {
  585. case IIO_CHAN_INFO_RAW:
  586. ret = regmap_bulk_read(priv->regmap, chan->address,
  587. &raw_val, sizeof(raw_val));
  588. if (ret < 0)
  589. return ret;
  590. *val = sign_extend32(le16_to_cpu(raw_val), 15);
  591. return IIO_VAL_INT;
  592. case IIO_CHAN_INFO_OFFSET:
  593. if (priv->operation_mode != BNO055_OPR_MODE_AMG) {
  594. *val = 0;
  595. } else {
  596. ret = regmap_bulk_read(priv->regmap,
  597. chan->address +
  598. BNO055_REG_OFFSET_ADDR,
  599. &raw_val, sizeof(raw_val));
  600. if (ret < 0)
  601. return ret;
  602. /*
  603. * IMU reports sensor offsets; IIO wants correction
  604. * offsets, thus we need the 'minus' here.
  605. */
  606. *val = -sign_extend32(le16_to_cpu(raw_val), 15);
  607. }
  608. return IIO_VAL_INT;
  609. case IIO_CHAN_INFO_SCALE:
  610. *val = 1;
  611. switch (chan->type) {
  612. case IIO_GRAVITY:
  613. /* Table 3-35: 1 m/s^2 = 100 LSB */
  614. case IIO_ACCEL:
  615. /* Table 3-17: 1 m/s^2 = 100 LSB */
  616. *val2 = 100;
  617. break;
  618. case IIO_MAGN:
  619. /*
  620. * Table 3-19: 1 uT = 16 LSB. But we need
  621. * Gauss: 1G = 0.1 uT.
  622. */
  623. *val2 = 160;
  624. break;
  625. case IIO_ANGL_VEL:
  626. /*
  627. * Table 3-22: 1 Rps = 900 LSB
  628. * .. but this is not exactly true. See comment at the
  629. * beginning of this file.
  630. */
  631. if (priv->operation_mode != BNO055_OPR_MODE_AMG) {
  632. *val = bno055_gyr_scale.fusion_vals[0];
  633. *val2 = bno055_gyr_scale.fusion_vals[1];
  634. return IIO_VAL_FRACTIONAL;
  635. }
  636. return bno055_get_regmask(priv, val, val2,
  637. BNO055_GYR_CONFIG_REG,
  638. BNO055_GYR_CONFIG_RANGE_MASK,
  639. &bno055_gyr_scale);
  640. break;
  641. case IIO_ROT:
  642. /* Table 3-28: 1 degree = 16 LSB */
  643. *val2 = 16;
  644. break;
  645. default:
  646. return -EINVAL;
  647. }
  648. return IIO_VAL_FRACTIONAL;
  649. case IIO_CHAN_INFO_SAMP_FREQ:
  650. if (chan->type != IIO_MAGN)
  651. return -EINVAL;
  652. return bno055_get_regmask(priv, val, val2,
  653. BNO055_MAG_CONFIG_REG,
  654. BNO055_MAG_CONFIG_ODR_MASK,
  655. &bno055_mag_odr);
  656. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  657. switch (chan->type) {
  658. case IIO_ANGL_VEL:
  659. return bno055_get_regmask(priv, val, val2,
  660. BNO055_GYR_CONFIG_REG,
  661. BNO055_GYR_CONFIG_LPF_MASK,
  662. &bno055_gyr_lpf);
  663. case IIO_ACCEL:
  664. return bno055_get_regmask(priv, val, val2,
  665. BNO055_ACC_CONFIG_REG,
  666. BNO055_ACC_CONFIG_LPF_MASK,
  667. &bno055_acc_lpf);
  668. default:
  669. return -EINVAL;
  670. }
  671. default:
  672. return -EINVAL;
  673. }
  674. }
  675. static int bno055_sysfs_attr_avail(struct bno055_priv *priv, struct bno055_sysfs_attr *attr,
  676. const int **vals, int *length)
  677. {
  678. if (priv->operation_mode != BNO055_OPR_MODE_AMG) {
  679. /* locked when fusion enabled */
  680. *vals = attr->fusion_vals;
  681. if (attr->type == IIO_VAL_INT)
  682. *length = 1;
  683. else
  684. *length = 2; /* IIO_VAL_INT_PLUS_MICRO or IIO_VAL_FRACTIONAL*/
  685. } else {
  686. *vals = attr->vals;
  687. *length = attr->len;
  688. }
  689. return attr->type;
  690. }
  691. static int bno055_read_avail(struct iio_dev *indio_dev,
  692. struct iio_chan_spec const *chan,
  693. const int **vals, int *type, int *length,
  694. long mask)
  695. {
  696. struct bno055_priv *priv = iio_priv(indio_dev);
  697. switch (mask) {
  698. case IIO_CHAN_INFO_SCALE:
  699. switch (chan->type) {
  700. case IIO_ANGL_VEL:
  701. *type = bno055_sysfs_attr_avail(priv, &bno055_gyr_scale,
  702. vals, length);
  703. return IIO_AVAIL_LIST;
  704. default:
  705. return -EINVAL;
  706. }
  707. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  708. switch (chan->type) {
  709. case IIO_ANGL_VEL:
  710. *type = bno055_sysfs_attr_avail(priv, &bno055_gyr_lpf,
  711. vals, length);
  712. return IIO_AVAIL_LIST;
  713. case IIO_ACCEL:
  714. *type = bno055_sysfs_attr_avail(priv, &bno055_acc_lpf,
  715. vals, length);
  716. return IIO_AVAIL_LIST;
  717. default:
  718. return -EINVAL;
  719. }
  720. break;
  721. case IIO_CHAN_INFO_SAMP_FREQ:
  722. switch (chan->type) {
  723. case IIO_MAGN:
  724. *type = bno055_sysfs_attr_avail(priv, &bno055_mag_odr,
  725. vals, length);
  726. return IIO_AVAIL_LIST;
  727. default:
  728. return -EINVAL;
  729. }
  730. default:
  731. return -EINVAL;
  732. }
  733. }
  734. static int bno055_read_temp_chan(struct iio_dev *indio_dev, int *val)
  735. {
  736. struct bno055_priv *priv = iio_priv(indio_dev);
  737. unsigned int raw_val;
  738. int ret;
  739. ret = regmap_read(priv->regmap, BNO055_TEMP_REG, &raw_val);
  740. if (ret < 0)
  741. return ret;
  742. /*
  743. * Tables 3-36 and 3-37: one byte of priv, signed, 1 LSB = 1C.
  744. * ABI wants milliC.
  745. */
  746. *val = raw_val * 1000;
  747. return IIO_VAL_INT;
  748. }
  749. static int bno055_read_quaternion(struct iio_dev *indio_dev,
  750. struct iio_chan_spec const *chan,
  751. int size, int *vals, int *val_len,
  752. long mask)
  753. {
  754. struct bno055_priv *priv = iio_priv(indio_dev);
  755. __le16 raw_vals[4];
  756. int i, ret;
  757. switch (mask) {
  758. case IIO_CHAN_INFO_RAW:
  759. if (size < 4)
  760. return -EINVAL;
  761. ret = regmap_bulk_read(priv->regmap,
  762. BNO055_QUAT_DATA_W_LSB_REG,
  763. raw_vals, sizeof(raw_vals));
  764. if (ret < 0)
  765. return ret;
  766. for (i = 0; i < 4; i++)
  767. vals[i] = sign_extend32(le16_to_cpu(raw_vals[i]), 15);
  768. *val_len = 4;
  769. return IIO_VAL_INT_MULTIPLE;
  770. case IIO_CHAN_INFO_SCALE:
  771. /* Table 3-31: 1 quaternion = 2^14 LSB */
  772. if (size < 2)
  773. return -EINVAL;
  774. vals[0] = 1;
  775. vals[1] = 14;
  776. return IIO_VAL_FRACTIONAL_LOG2;
  777. default:
  778. return -EINVAL;
  779. }
  780. }
  781. static bool bno055_is_chan_readable(struct iio_dev *indio_dev,
  782. struct iio_chan_spec const *chan)
  783. {
  784. struct bno055_priv *priv = iio_priv(indio_dev);
  785. if (priv->operation_mode != BNO055_OPR_MODE_AMG)
  786. return true;
  787. switch (chan->type) {
  788. case IIO_GRAVITY:
  789. case IIO_ROT:
  790. return false;
  791. case IIO_ACCEL:
  792. if (chan->channel2 == IIO_MOD_LINEAR_X ||
  793. chan->channel2 == IIO_MOD_LINEAR_Y ||
  794. chan->channel2 == IIO_MOD_LINEAR_Z)
  795. return false;
  796. return true;
  797. default:
  798. return true;
  799. }
  800. }
  801. static int _bno055_read_raw_multi(struct iio_dev *indio_dev,
  802. struct iio_chan_spec const *chan,
  803. int size, int *vals, int *val_len,
  804. long mask)
  805. {
  806. if (!bno055_is_chan_readable(indio_dev, chan))
  807. return -EBUSY;
  808. switch (chan->type) {
  809. case IIO_MAGN:
  810. case IIO_ACCEL:
  811. case IIO_ANGL_VEL:
  812. case IIO_GRAVITY:
  813. if (size < 2)
  814. return -EINVAL;
  815. *val_len = 2;
  816. return bno055_read_simple_chan(indio_dev, chan,
  817. &vals[0], &vals[1],
  818. mask);
  819. case IIO_TEMP:
  820. *val_len = 1;
  821. return bno055_read_temp_chan(indio_dev, &vals[0]);
  822. case IIO_ROT:
  823. /*
  824. * Rotation is exposed as either a quaternion or three
  825. * Euler angles.
  826. */
  827. if (chan->channel2 == IIO_MOD_QUATERNION)
  828. return bno055_read_quaternion(indio_dev, chan,
  829. size, vals,
  830. val_len, mask);
  831. if (size < 2)
  832. return -EINVAL;
  833. *val_len = 2;
  834. return bno055_read_simple_chan(indio_dev, chan,
  835. &vals[0], &vals[1],
  836. mask);
  837. default:
  838. return -EINVAL;
  839. }
  840. }
  841. static int bno055_read_raw_multi(struct iio_dev *indio_dev,
  842. struct iio_chan_spec const *chan,
  843. int size, int *vals, int *val_len,
  844. long mask)
  845. {
  846. struct bno055_priv *priv = iio_priv(indio_dev);
  847. int ret;
  848. mutex_lock(&priv->lock);
  849. ret = _bno055_read_raw_multi(indio_dev, chan, size,
  850. vals, val_len, mask);
  851. mutex_unlock(&priv->lock);
  852. return ret;
  853. }
  854. static int _bno055_write_raw(struct iio_dev *iio_dev,
  855. struct iio_chan_spec const *chan,
  856. int val, int val2, long mask)
  857. {
  858. struct bno055_priv *priv = iio_priv(iio_dev);
  859. switch (chan->type) {
  860. case IIO_MAGN:
  861. switch (mask) {
  862. case IIO_CHAN_INFO_SAMP_FREQ:
  863. return bno055_set_regmask(priv, val, val2,
  864. BNO055_MAG_CONFIG_REG,
  865. BNO055_MAG_CONFIG_ODR_MASK,
  866. &bno055_mag_odr);
  867. default:
  868. return -EINVAL;
  869. }
  870. case IIO_ACCEL:
  871. switch (mask) {
  872. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  873. return bno055_set_regmask(priv, val, val2,
  874. BNO055_ACC_CONFIG_REG,
  875. BNO055_ACC_CONFIG_LPF_MASK,
  876. &bno055_acc_lpf);
  877. default:
  878. return -EINVAL;
  879. }
  880. case IIO_ANGL_VEL:
  881. switch (mask) {
  882. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  883. return bno055_set_regmask(priv, val, val2,
  884. BNO055_GYR_CONFIG_REG,
  885. BNO055_GYR_CONFIG_LPF_MASK,
  886. &bno055_gyr_lpf);
  887. case IIO_CHAN_INFO_SCALE:
  888. return bno055_set_regmask(priv, val, val2,
  889. BNO055_GYR_CONFIG_REG,
  890. BNO055_GYR_CONFIG_RANGE_MASK,
  891. &bno055_gyr_scale);
  892. default:
  893. return -EINVAL;
  894. }
  895. default:
  896. return -EINVAL;
  897. }
  898. }
  899. static int bno055_write_raw(struct iio_dev *iio_dev,
  900. struct iio_chan_spec const *chan,
  901. int val, int val2, long mask)
  902. {
  903. struct bno055_priv *priv = iio_priv(iio_dev);
  904. int ret;
  905. mutex_lock(&priv->lock);
  906. ret = _bno055_write_raw(iio_dev, chan, val, val2, mask);
  907. mutex_unlock(&priv->lock);
  908. return ret;
  909. }
  910. static ssize_t in_accel_range_raw_available_show(struct device *dev,
  911. struct device_attribute *attr,
  912. char *buf)
  913. {
  914. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  915. int len = 0;
  916. int i;
  917. if (priv->operation_mode != BNO055_OPR_MODE_AMG)
  918. return sysfs_emit(buf, "%d\n", bno055_acc_range.fusion_vals[0]);
  919. for (i = 0; i < bno055_acc_range.len; i++)
  920. len += sysfs_emit_at(buf, len, "%d ", bno055_acc_range.vals[i]);
  921. buf[len - 1] = '\n';
  922. return len;
  923. }
  924. static ssize_t fusion_enable_show(struct device *dev,
  925. struct device_attribute *attr,
  926. char *buf)
  927. {
  928. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  929. return sysfs_emit(buf, "%d\n",
  930. priv->operation_mode != BNO055_OPR_MODE_AMG);
  931. }
  932. static ssize_t fusion_enable_store(struct device *dev,
  933. struct device_attribute *attr,
  934. const char *buf, size_t len)
  935. {
  936. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  937. struct bno055_priv *priv = iio_priv(indio_dev);
  938. bool en;
  939. int ret;
  940. if (indio_dev->active_scan_mask &&
  941. !bitmap_empty(indio_dev->active_scan_mask, _BNO055_SCAN_MAX))
  942. return -EBUSY;
  943. ret = kstrtobool(buf, &en);
  944. if (ret)
  945. return -EINVAL;
  946. if (!en)
  947. return bno055_operation_mode_set(priv, BNO055_OPR_MODE_AMG) ?: len;
  948. /*
  949. * Coming from AMG means the FMC was off, just switch to fusion but
  950. * don't change anything that doesn't belong to us (i.e let FMC stay off).
  951. * Coming from any other fusion mode means we don't need to do anything.
  952. */
  953. if (priv->operation_mode == BNO055_OPR_MODE_AMG)
  954. return bno055_operation_mode_set(priv, BNO055_OPR_MODE_FUSION_FMC_OFF) ?: len;
  955. return len;
  956. }
  957. static ssize_t in_magn_calibration_fast_enable_show(struct device *dev,
  958. struct device_attribute *attr,
  959. char *buf)
  960. {
  961. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  962. return sysfs_emit(buf, "%d\n",
  963. priv->operation_mode == BNO055_OPR_MODE_FUSION);
  964. }
  965. static ssize_t in_magn_calibration_fast_enable_store(struct device *dev,
  966. struct device_attribute *attr,
  967. const char *buf, size_t len)
  968. {
  969. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  970. struct bno055_priv *priv = iio_priv(indio_dev);
  971. int ret;
  972. if (indio_dev->active_scan_mask &&
  973. !bitmap_empty(indio_dev->active_scan_mask, _BNO055_SCAN_MAX))
  974. return -EBUSY;
  975. if (sysfs_streq(buf, "0")) {
  976. if (priv->operation_mode == BNO055_OPR_MODE_FUSION) {
  977. ret = bno055_operation_mode_set(priv, BNO055_OPR_MODE_FUSION_FMC_OFF);
  978. if (ret)
  979. return ret;
  980. }
  981. } else {
  982. if (priv->operation_mode == BNO055_OPR_MODE_AMG)
  983. return -EINVAL;
  984. if (priv->operation_mode != BNO055_OPR_MODE_FUSION) {
  985. ret = bno055_operation_mode_set(priv, BNO055_OPR_MODE_FUSION);
  986. if (ret)
  987. return ret;
  988. }
  989. }
  990. return len;
  991. }
  992. static ssize_t in_accel_range_raw_show(struct device *dev,
  993. struct device_attribute *attr,
  994. char *buf)
  995. {
  996. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  997. int val;
  998. int ret;
  999. ret = bno055_get_regmask(priv, &val, NULL,
  1000. BNO055_ACC_CONFIG_REG,
  1001. BNO055_ACC_CONFIG_RANGE_MASK,
  1002. &bno055_acc_range);
  1003. if (ret < 0)
  1004. return ret;
  1005. return sysfs_emit(buf, "%d\n", val);
  1006. }
  1007. static ssize_t in_accel_range_raw_store(struct device *dev,
  1008. struct device_attribute *attr,
  1009. const char *buf, size_t len)
  1010. {
  1011. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  1012. unsigned long val;
  1013. int ret;
  1014. ret = kstrtoul(buf, 10, &val);
  1015. if (ret)
  1016. return ret;
  1017. mutex_lock(&priv->lock);
  1018. ret = bno055_set_regmask(priv, val, 0,
  1019. BNO055_ACC_CONFIG_REG,
  1020. BNO055_ACC_CONFIG_RANGE_MASK,
  1021. &bno055_acc_range);
  1022. mutex_unlock(&priv->lock);
  1023. return ret ?: len;
  1024. }
  1025. static ssize_t bno055_get_calib_status(struct device *dev, char *buf, int which)
  1026. {
  1027. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  1028. int calib;
  1029. int ret;
  1030. int val;
  1031. if (priv->operation_mode == BNO055_OPR_MODE_AMG ||
  1032. (priv->operation_mode == BNO055_OPR_MODE_FUSION_FMC_OFF &&
  1033. which == BNO055_CALIB_STAT_MAGN_SHIFT)) {
  1034. calib = 0;
  1035. } else {
  1036. mutex_lock(&priv->lock);
  1037. ret = regmap_read(priv->regmap, BNO055_CALIB_STAT_REG, &val);
  1038. mutex_unlock(&priv->lock);
  1039. if (ret)
  1040. return -EIO;
  1041. calib = ((val >> which) & GENMASK(1, 0)) + 1;
  1042. }
  1043. return sysfs_emit(buf, "%d\n", calib);
  1044. }
  1045. static ssize_t serialnumber_show(struct device *dev,
  1046. struct device_attribute *attr,
  1047. char *buf)
  1048. {
  1049. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(dev));
  1050. return sysfs_emit(buf, "%*ph\n", BNO055_UID_LEN, priv->uid);
  1051. }
  1052. static ssize_t calibration_data_read(struct file *filp, struct kobject *kobj,
  1053. struct bin_attribute *bin_attr, char *buf,
  1054. loff_t pos, size_t count)
  1055. {
  1056. struct bno055_priv *priv = iio_priv(dev_to_iio_dev(kobj_to_dev(kobj)));
  1057. u8 data[BNO055_CALDATA_LEN];
  1058. int ret;
  1059. /*
  1060. * Calibration data is volatile; reading it in chunks will possibly
  1061. * results in inconsistent data. We require the user to read the whole
  1062. * blob in a single chunk
  1063. */
  1064. if (count < BNO055_CALDATA_LEN || pos)
  1065. return -EINVAL;
  1066. mutex_lock(&priv->lock);
  1067. ret = bno055_operation_mode_do_set(priv, BNO055_OPR_MODE_CONFIG);
  1068. if (ret)
  1069. goto exit_unlock;
  1070. ret = regmap_bulk_read(priv->regmap, BNO055_CALDATA_START, data,
  1071. BNO055_CALDATA_LEN);
  1072. if (ret)
  1073. goto exit_unlock;
  1074. ret = bno055_operation_mode_do_set(priv, priv->operation_mode);
  1075. if (ret)
  1076. goto exit_unlock;
  1077. memcpy(buf, data, BNO055_CALDATA_LEN);
  1078. ret = BNO055_CALDATA_LEN;
  1079. exit_unlock:
  1080. mutex_unlock(&priv->lock);
  1081. return ret;
  1082. }
  1083. static ssize_t sys_calibration_auto_status_show(struct device *dev,
  1084. struct device_attribute *a,
  1085. char *buf)
  1086. {
  1087. return bno055_get_calib_status(dev, buf, BNO055_CALIB_STAT_SYS_SHIFT);
  1088. }
  1089. static ssize_t in_accel_calibration_auto_status_show(struct device *dev,
  1090. struct device_attribute *a,
  1091. char *buf)
  1092. {
  1093. return bno055_get_calib_status(dev, buf, BNO055_CALIB_STAT_ACCEL_SHIFT);
  1094. }
  1095. static ssize_t in_gyro_calibration_auto_status_show(struct device *dev,
  1096. struct device_attribute *a,
  1097. char *buf)
  1098. {
  1099. return bno055_get_calib_status(dev, buf, BNO055_CALIB_STAT_GYRO_SHIFT);
  1100. }
  1101. static ssize_t in_magn_calibration_auto_status_show(struct device *dev,
  1102. struct device_attribute *a,
  1103. char *buf)
  1104. {
  1105. return bno055_get_calib_status(dev, buf, BNO055_CALIB_STAT_MAGN_SHIFT);
  1106. }
  1107. static int bno055_debugfs_reg_access(struct iio_dev *iio_dev, unsigned int reg,
  1108. unsigned int writeval, unsigned int *readval)
  1109. {
  1110. struct bno055_priv *priv = iio_priv(iio_dev);
  1111. if (readval)
  1112. return regmap_read(priv->regmap, reg, readval);
  1113. else
  1114. return regmap_write(priv->regmap, reg, writeval);
  1115. }
  1116. static ssize_t bno055_show_fw_version(struct file *file, char __user *userbuf,
  1117. size_t count, loff_t *ppos)
  1118. {
  1119. struct bno055_priv *priv = file->private_data;
  1120. int rev, ver;
  1121. char *buf;
  1122. int ret;
  1123. ret = regmap_read(priv->regmap, BNO055_SW_REV_LSB_REG, &rev);
  1124. if (ret)
  1125. return ret;
  1126. ret = regmap_read(priv->regmap, BNO055_SW_REV_MSB_REG, &ver);
  1127. if (ret)
  1128. return ret;
  1129. buf = kasprintf(GFP_KERNEL, "ver: 0x%x, rev: 0x%x\n", ver, rev);
  1130. if (!buf)
  1131. return -ENOMEM;
  1132. ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  1133. kfree(buf);
  1134. return ret;
  1135. }
  1136. static const struct file_operations bno055_fw_version_ops = {
  1137. .open = simple_open,
  1138. .read = bno055_show_fw_version,
  1139. .llseek = default_llseek,
  1140. .owner = THIS_MODULE,
  1141. };
  1142. static void bno055_debugfs_remove(void *_priv)
  1143. {
  1144. struct bno055_priv *priv = _priv;
  1145. debugfs_remove(priv->debugfs);
  1146. priv->debugfs = NULL;
  1147. }
  1148. static void bno055_debugfs_init(struct iio_dev *iio_dev)
  1149. {
  1150. struct bno055_priv *priv = iio_priv(iio_dev);
  1151. priv->debugfs = debugfs_create_file("firmware_version", 0400,
  1152. iio_get_debugfs_dentry(iio_dev),
  1153. priv, &bno055_fw_version_ops);
  1154. if (!IS_ERR(priv->debugfs))
  1155. devm_add_action_or_reset(priv->dev, bno055_debugfs_remove,
  1156. priv);
  1157. if (IS_ERR_OR_NULL(priv->debugfs))
  1158. dev_warn(priv->dev, "failed to setup debugfs");
  1159. }
  1160. static IIO_DEVICE_ATTR_RW(fusion_enable, 0);
  1161. static IIO_DEVICE_ATTR_RW(in_magn_calibration_fast_enable, 0);
  1162. static IIO_DEVICE_ATTR_RW(in_accel_range_raw, 0);
  1163. static IIO_DEVICE_ATTR_RO(in_accel_range_raw_available, 0);
  1164. static IIO_DEVICE_ATTR_RO(sys_calibration_auto_status, 0);
  1165. static IIO_DEVICE_ATTR_RO(in_accel_calibration_auto_status, 0);
  1166. static IIO_DEVICE_ATTR_RO(in_gyro_calibration_auto_status, 0);
  1167. static IIO_DEVICE_ATTR_RO(in_magn_calibration_auto_status, 0);
  1168. static IIO_DEVICE_ATTR_RO(serialnumber, 0);
  1169. static struct attribute *bno055_attrs[] = {
  1170. &iio_dev_attr_in_accel_range_raw_available.dev_attr.attr,
  1171. &iio_dev_attr_in_accel_range_raw.dev_attr.attr,
  1172. &iio_dev_attr_fusion_enable.dev_attr.attr,
  1173. &iio_dev_attr_in_magn_calibration_fast_enable.dev_attr.attr,
  1174. &iio_dev_attr_sys_calibration_auto_status.dev_attr.attr,
  1175. &iio_dev_attr_in_accel_calibration_auto_status.dev_attr.attr,
  1176. &iio_dev_attr_in_gyro_calibration_auto_status.dev_attr.attr,
  1177. &iio_dev_attr_in_magn_calibration_auto_status.dev_attr.attr,
  1178. &iio_dev_attr_serialnumber.dev_attr.attr,
  1179. NULL
  1180. };
  1181. static BIN_ATTR_RO(calibration_data, BNO055_CALDATA_LEN);
  1182. static struct bin_attribute *bno055_bin_attrs[] = {
  1183. &bin_attr_calibration_data,
  1184. NULL
  1185. };
  1186. static const struct attribute_group bno055_attrs_group = {
  1187. .attrs = bno055_attrs,
  1188. .bin_attrs = bno055_bin_attrs,
  1189. };
  1190. static const struct iio_info bno055_info = {
  1191. .read_raw_multi = bno055_read_raw_multi,
  1192. .read_avail = bno055_read_avail,
  1193. .write_raw = bno055_write_raw,
  1194. .attrs = &bno055_attrs_group,
  1195. .debugfs_reg_access = bno055_debugfs_reg_access,
  1196. };
  1197. /*
  1198. * Reads len samples from the HW, stores them in buf starting from buf_idx,
  1199. * and applies mask to cull (skip) unneeded samples.
  1200. * Updates buf_idx incrementing with the number of stored samples.
  1201. * Samples from HW are transferred into buf, then in-place copy on buf is
  1202. * performed in order to cull samples that need to be skipped.
  1203. * This avoids copies of the first samples until we hit the 1st sample to skip,
  1204. * and also avoids having an extra bounce buffer.
  1205. * buf must be able to contain len elements in spite of how many samples we are
  1206. * going to cull.
  1207. */
  1208. static int bno055_scan_xfer(struct bno055_priv *priv,
  1209. int start_ch, int len, unsigned long mask,
  1210. __le16 *buf, int *buf_idx)
  1211. {
  1212. const int base = BNO055_ACC_DATA_X_LSB_REG;
  1213. bool quat_in_read = false;
  1214. int buf_base = *buf_idx;
  1215. __le16 *dst, *src;
  1216. int offs_fixup = 0;
  1217. int xfer_len = len;
  1218. int ret;
  1219. int i, n;
  1220. if (!mask)
  1221. return 0;
  1222. /*
  1223. * All channels are made up 1 16-bit sample, except for quaternion that
  1224. * is made up 4 16-bit values.
  1225. * For us the quaternion CH is just like 4 regular CHs.
  1226. * If our read starts past the quaternion make sure to adjust the
  1227. * starting offset; if the quaternion is contained in our scan then make
  1228. * sure to adjust the read len.
  1229. */
  1230. if (start_ch > BNO055_SCAN_QUATERNION) {
  1231. start_ch += 3;
  1232. } else if ((start_ch <= BNO055_SCAN_QUATERNION) &&
  1233. ((start_ch + len) > BNO055_SCAN_QUATERNION)) {
  1234. quat_in_read = true;
  1235. xfer_len += 3;
  1236. }
  1237. ret = regmap_bulk_read(priv->regmap,
  1238. base + start_ch * sizeof(__le16),
  1239. buf + buf_base,
  1240. xfer_len * sizeof(__le16));
  1241. if (ret)
  1242. return ret;
  1243. for_each_set_bit(i, &mask, len) {
  1244. if (quat_in_read && ((start_ch + i) > BNO055_SCAN_QUATERNION))
  1245. offs_fixup = 3;
  1246. dst = buf + *buf_idx;
  1247. src = buf + buf_base + offs_fixup + i;
  1248. n = (start_ch + i == BNO055_SCAN_QUATERNION) ? 4 : 1;
  1249. if (dst != src)
  1250. memcpy(dst, src, n * sizeof(__le16));
  1251. *buf_idx += n;
  1252. }
  1253. return 0;
  1254. }
  1255. static irqreturn_t bno055_trigger_handler(int irq, void *p)
  1256. {
  1257. struct iio_poll_func *pf = p;
  1258. struct iio_dev *iio_dev = pf->indio_dev;
  1259. struct bno055_priv *priv = iio_priv(iio_dev);
  1260. int xfer_start, start, end, prev_end;
  1261. unsigned long mask;
  1262. int quat_extra_len;
  1263. bool first = true;
  1264. int buf_idx = 0;
  1265. bool thr_hit;
  1266. int ret;
  1267. mutex_lock(&priv->lock);
  1268. /*
  1269. * Walk the bitmap and eventually perform several transfers.
  1270. * Bitmap ones-fields that are separated by gaps <= xfer_burst_break_thr
  1271. * will be included in same transfer.
  1272. * Every time the bitmap contains a gap wider than xfer_burst_break_thr
  1273. * then we split the transfer, skipping the gap.
  1274. */
  1275. for_each_set_bitrange(start, end, iio_dev->active_scan_mask,
  1276. iio_dev->masklength) {
  1277. /*
  1278. * First transfer will start from the beginning of the first
  1279. * ones-field in the bitmap
  1280. */
  1281. if (first) {
  1282. xfer_start = start;
  1283. } else {
  1284. /*
  1285. * We found the next ones-field; check whether to
  1286. * include it in * the current transfer or not (i.e.
  1287. * let's perform the current * transfer and prepare for
  1288. * another one).
  1289. */
  1290. /*
  1291. * In case the zeros-gap contains the quaternion bit,
  1292. * then its length is actually 4 words instead of 1
  1293. * (i.e. +3 wrt other channels).
  1294. */
  1295. quat_extra_len = ((start > BNO055_SCAN_QUATERNION) &&
  1296. (prev_end <= BNO055_SCAN_QUATERNION)) ? 3 : 0;
  1297. /* If the gap is wider than xfer_burst_break_thr then.. */
  1298. thr_hit = (start - prev_end + quat_extra_len) >
  1299. priv->xfer_burst_break_thr;
  1300. /*
  1301. * .. transfer all the data up to the gap. Then set the
  1302. * next transfer start index at right after the gap
  1303. * (i.e. at the start of this ones-field).
  1304. */
  1305. if (thr_hit) {
  1306. mask = *iio_dev->active_scan_mask >> xfer_start;
  1307. ret = bno055_scan_xfer(priv, xfer_start,
  1308. prev_end - xfer_start,
  1309. mask, priv->buf.chans, &buf_idx);
  1310. if (ret)
  1311. goto done;
  1312. xfer_start = start;
  1313. }
  1314. }
  1315. first = false;
  1316. prev_end = end;
  1317. }
  1318. /*
  1319. * We finished walking the bitmap; no more gaps to check for. Just
  1320. * perform the current transfer.
  1321. */
  1322. mask = *iio_dev->active_scan_mask >> xfer_start;
  1323. ret = bno055_scan_xfer(priv, xfer_start,
  1324. prev_end - xfer_start,
  1325. mask, priv->buf.chans, &buf_idx);
  1326. if (!ret)
  1327. iio_push_to_buffers_with_timestamp(iio_dev,
  1328. &priv->buf, pf->timestamp);
  1329. done:
  1330. mutex_unlock(&priv->lock);
  1331. iio_trigger_notify_done(iio_dev->trig);
  1332. return IRQ_HANDLED;
  1333. }
  1334. static int bno055_buffer_preenable(struct iio_dev *indio_dev)
  1335. {
  1336. struct bno055_priv *priv = iio_priv(indio_dev);
  1337. const unsigned long fusion_mask =
  1338. BIT(BNO055_SCAN_YAW) |
  1339. BIT(BNO055_SCAN_ROLL) |
  1340. BIT(BNO055_SCAN_PITCH) |
  1341. BIT(BNO055_SCAN_QUATERNION) |
  1342. BIT(BNO055_SCAN_LIA_X) |
  1343. BIT(BNO055_SCAN_LIA_Y) |
  1344. BIT(BNO055_SCAN_LIA_Z) |
  1345. BIT(BNO055_SCAN_GRAVITY_X) |
  1346. BIT(BNO055_SCAN_GRAVITY_Y) |
  1347. BIT(BNO055_SCAN_GRAVITY_Z);
  1348. if (priv->operation_mode == BNO055_OPR_MODE_AMG &&
  1349. bitmap_intersects(indio_dev->active_scan_mask, &fusion_mask,
  1350. _BNO055_SCAN_MAX))
  1351. return -EBUSY;
  1352. return 0;
  1353. }
  1354. static const struct iio_buffer_setup_ops bno055_buffer_setup_ops = {
  1355. .preenable = bno055_buffer_preenable,
  1356. };
  1357. int bno055_probe(struct device *dev, struct regmap *regmap,
  1358. int xfer_burst_break_thr, bool sw_reset)
  1359. {
  1360. const struct firmware *caldata = NULL;
  1361. struct bno055_priv *priv;
  1362. struct iio_dev *iio_dev;
  1363. char *fw_name_buf;
  1364. unsigned int val;
  1365. int rev, ver;
  1366. int ret;
  1367. iio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  1368. if (!iio_dev)
  1369. return -ENOMEM;
  1370. iio_dev->name = "bno055";
  1371. priv = iio_priv(iio_dev);
  1372. mutex_init(&priv->lock);
  1373. priv->regmap = regmap;
  1374. priv->dev = dev;
  1375. priv->xfer_burst_break_thr = xfer_burst_break_thr;
  1376. priv->sw_reset = sw_reset;
  1377. priv->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1378. if (IS_ERR(priv->reset_gpio))
  1379. return dev_err_probe(dev, PTR_ERR(priv->reset_gpio), "Failed to get reset GPIO\n");
  1380. priv->clk = devm_clk_get_optional_enabled(dev, "clk");
  1381. if (IS_ERR(priv->clk))
  1382. return dev_err_probe(dev, PTR_ERR(priv->clk), "Failed to get CLK\n");
  1383. if (priv->reset_gpio) {
  1384. usleep_range(5000, 10000);
  1385. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  1386. usleep_range(650000, 750000);
  1387. } else if (!sw_reset) {
  1388. dev_warn(dev, "No usable reset method; IMU may be unreliable\n");
  1389. }
  1390. ret = regmap_read(priv->regmap, BNO055_CHIP_ID_REG, &val);
  1391. if (ret)
  1392. return ret;
  1393. if (val != BNO055_CHIP_ID_MAGIC)
  1394. dev_warn(dev, "Unrecognized chip ID 0x%x\n", val);
  1395. /*
  1396. * In case we haven't a HW reset pin, we can still reset the chip via
  1397. * register write. This is probably nonsense in case we can't even
  1398. * communicate with the chip or the chip isn't the one we expect (i.e.
  1399. * we don't write to unknown chips), so we perform SW reset only after
  1400. * chip magic ID check
  1401. */
  1402. if (!priv->reset_gpio) {
  1403. ret = bno055_system_reset(priv);
  1404. if (ret)
  1405. return ret;
  1406. }
  1407. ret = regmap_read(priv->regmap, BNO055_SW_REV_LSB_REG, &rev);
  1408. if (ret)
  1409. return ret;
  1410. ret = regmap_read(priv->regmap, BNO055_SW_REV_MSB_REG, &ver);
  1411. if (ret)
  1412. return ret;
  1413. /*
  1414. * The stock FW version contains a bug (see comment at the beginning of
  1415. * this file) that causes the anglvel scale to be changed depending on
  1416. * the chip range setting. We workaround this, but we don't know what
  1417. * other FW versions might do.
  1418. */
  1419. if (ver != 0x3 || rev != 0x11)
  1420. dev_warn(dev, "Untested firmware version. Anglvel scale may not work as expected\n");
  1421. ret = regmap_bulk_read(priv->regmap, BNO055_UID_LOWER_REG,
  1422. priv->uid, BNO055_UID_LEN);
  1423. if (ret)
  1424. return ret;
  1425. /* Sensor calibration data */
  1426. fw_name_buf = kasprintf(GFP_KERNEL, BNO055_FW_UID_FMT,
  1427. BNO055_UID_LEN, priv->uid);
  1428. if (!fw_name_buf)
  1429. return -ENOMEM;
  1430. ret = request_firmware(&caldata, fw_name_buf, dev);
  1431. kfree(fw_name_buf);
  1432. if (ret)
  1433. ret = request_firmware(&caldata, BNO055_FW_GENERIC_NAME, dev);
  1434. if (ret) {
  1435. dev_notice(dev, "Calibration file load failed. See instruction in kernel Documentation/iio/bno055.rst\n");
  1436. ret = bno055_init(priv, NULL, 0);
  1437. } else {
  1438. ret = bno055_init(priv, caldata->data, caldata->size);
  1439. release_firmware(caldata);
  1440. }
  1441. if (ret)
  1442. return ret;
  1443. priv->operation_mode = BNO055_OPR_MODE_FUSION;
  1444. ret = bno055_operation_mode_do_set(priv, priv->operation_mode);
  1445. if (ret)
  1446. return ret;
  1447. ret = devm_add_action_or_reset(dev, bno055_uninit, priv);
  1448. if (ret)
  1449. return ret;
  1450. iio_dev->channels = bno055_channels;
  1451. iio_dev->num_channels = ARRAY_SIZE(bno055_channels);
  1452. iio_dev->info = &bno055_info;
  1453. iio_dev->modes = INDIO_DIRECT_MODE;
  1454. ret = devm_iio_triggered_buffer_setup(dev, iio_dev,
  1455. iio_pollfunc_store_time,
  1456. bno055_trigger_handler,
  1457. &bno055_buffer_setup_ops);
  1458. if (ret)
  1459. return ret;
  1460. ret = devm_iio_device_register(dev, iio_dev);
  1461. if (ret)
  1462. return ret;
  1463. bno055_debugfs_init(iio_dev);
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL_NS_GPL(bno055_probe, IIO_BNO055);
  1467. MODULE_AUTHOR("Andrea Merello <[email protected]>");
  1468. MODULE_DESCRIPTION("Bosch BNO055 driver");
  1469. MODULE_LICENSE("GPL");