bmc150_magn.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * Bosch BMC150 three-axis magnetic field sensor driver
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  5. *
  6. * This code is based on bmm050_api.c authored by [email protected]:
  7. *
  8. * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/acpi.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/events.h>
  31. #include <linux/iio/trigger.h>
  32. #include <linux/iio/trigger_consumer.h>
  33. #include <linux/iio/triggered_buffer.h>
  34. #include <linux/regmap.h>
  35. #include "bmc150_magn.h"
  36. #define BMC150_MAGN_DRV_NAME "bmc150_magn"
  37. #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
  38. #define BMC150_MAGN_REG_CHIP_ID 0x40
  39. #define BMC150_MAGN_CHIP_ID_VAL 0x32
  40. #define BMC150_MAGN_REG_X_L 0x42
  41. #define BMC150_MAGN_REG_X_M 0x43
  42. #define BMC150_MAGN_REG_Y_L 0x44
  43. #define BMC150_MAGN_REG_Y_M 0x45
  44. #define BMC150_MAGN_SHIFT_XY_L 3
  45. #define BMC150_MAGN_REG_Z_L 0x46
  46. #define BMC150_MAGN_REG_Z_M 0x47
  47. #define BMC150_MAGN_SHIFT_Z_L 1
  48. #define BMC150_MAGN_REG_RHALL_L 0x48
  49. #define BMC150_MAGN_REG_RHALL_M 0x49
  50. #define BMC150_MAGN_SHIFT_RHALL_L 2
  51. #define BMC150_MAGN_REG_INT_STATUS 0x4A
  52. #define BMC150_MAGN_REG_POWER 0x4B
  53. #define BMC150_MAGN_MASK_POWER_CTL BIT(0)
  54. #define BMC150_MAGN_REG_OPMODE_ODR 0x4C
  55. #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1)
  56. #define BMC150_MAGN_SHIFT_OPMODE 1
  57. #define BMC150_MAGN_MODE_NORMAL 0x00
  58. #define BMC150_MAGN_MODE_FORCED 0x01
  59. #define BMC150_MAGN_MODE_SLEEP 0x03
  60. #define BMC150_MAGN_MASK_ODR GENMASK(5, 3)
  61. #define BMC150_MAGN_SHIFT_ODR 3
  62. #define BMC150_MAGN_REG_INT 0x4D
  63. #define BMC150_MAGN_REG_INT_DRDY 0x4E
  64. #define BMC150_MAGN_MASK_DRDY_EN BIT(7)
  65. #define BMC150_MAGN_SHIFT_DRDY_EN 7
  66. #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6)
  67. #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5)
  68. #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4)
  69. #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3)
  70. #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2)
  71. #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1)
  72. #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0)
  73. #define BMC150_MAGN_REG_LOW_THRESH 0x4F
  74. #define BMC150_MAGN_REG_HIGH_THRESH 0x50
  75. #define BMC150_MAGN_REG_REP_XY 0x51
  76. #define BMC150_MAGN_REG_REP_Z 0x52
  77. #define BMC150_MAGN_REG_REP_DATAMASK GENMASK(7, 0)
  78. #define BMC150_MAGN_REG_TRIM_START 0x5D
  79. #define BMC150_MAGN_REG_TRIM_END 0x71
  80. #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
  81. #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
  82. /* Time from SUSPEND to SLEEP */
  83. #define BMC150_MAGN_START_UP_TIME_MS 3
  84. #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000
  85. #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1)
  86. #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1)
  87. #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
  88. #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
  89. enum bmc150_magn_axis {
  90. AXIS_X,
  91. AXIS_Y,
  92. AXIS_Z,
  93. RHALL,
  94. AXIS_XYZ_MAX = RHALL,
  95. AXIS_XYZR_MAX,
  96. };
  97. enum bmc150_magn_power_modes {
  98. BMC150_MAGN_POWER_MODE_SUSPEND,
  99. BMC150_MAGN_POWER_MODE_SLEEP,
  100. BMC150_MAGN_POWER_MODE_NORMAL,
  101. };
  102. struct bmc150_magn_trim_regs {
  103. s8 x1;
  104. s8 y1;
  105. __le16 reserved1;
  106. u8 reserved2;
  107. __le16 z4;
  108. s8 x2;
  109. s8 y2;
  110. __le16 reserved3;
  111. __le16 z2;
  112. __le16 z1;
  113. __le16 xyz1;
  114. __le16 z3;
  115. s8 xy2;
  116. u8 xy1;
  117. } __packed;
  118. struct bmc150_magn_data {
  119. struct device *dev;
  120. /*
  121. * 1. Protect this structure.
  122. * 2. Serialize sequences that power on/off the device and access HW.
  123. */
  124. struct mutex mutex;
  125. struct regmap *regmap;
  126. /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
  127. s32 buffer[6];
  128. struct iio_trigger *dready_trig;
  129. bool dready_trigger_on;
  130. int max_odr;
  131. int irq;
  132. };
  133. static const struct {
  134. int freq;
  135. u8 reg_val;
  136. } bmc150_magn_samp_freq_table[] = { {2, 0x01},
  137. {6, 0x02},
  138. {8, 0x03},
  139. {10, 0x00},
  140. {15, 0x04},
  141. {20, 0x05},
  142. {25, 0x06},
  143. {30, 0x07} };
  144. enum bmc150_magn_presets {
  145. LOW_POWER_PRESET,
  146. REGULAR_PRESET,
  147. ENHANCED_REGULAR_PRESET,
  148. HIGH_ACCURACY_PRESET
  149. };
  150. static const struct bmc150_magn_preset {
  151. u8 rep_xy;
  152. u8 rep_z;
  153. u8 odr;
  154. } bmc150_magn_presets_table[] = {
  155. [LOW_POWER_PRESET] = {3, 3, 10},
  156. [REGULAR_PRESET] = {9, 15, 10},
  157. [ENHANCED_REGULAR_PRESET] = {15, 27, 10},
  158. [HIGH_ACCURACY_PRESET] = {47, 83, 20},
  159. };
  160. #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
  161. static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg)
  162. {
  163. switch (reg) {
  164. case BMC150_MAGN_REG_POWER:
  165. case BMC150_MAGN_REG_OPMODE_ODR:
  166. case BMC150_MAGN_REG_INT:
  167. case BMC150_MAGN_REG_INT_DRDY:
  168. case BMC150_MAGN_REG_LOW_THRESH:
  169. case BMC150_MAGN_REG_HIGH_THRESH:
  170. case BMC150_MAGN_REG_REP_XY:
  171. case BMC150_MAGN_REG_REP_Z:
  172. return true;
  173. default:
  174. return false;
  175. };
  176. }
  177. static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
  178. {
  179. switch (reg) {
  180. case BMC150_MAGN_REG_X_L:
  181. case BMC150_MAGN_REG_X_M:
  182. case BMC150_MAGN_REG_Y_L:
  183. case BMC150_MAGN_REG_Y_M:
  184. case BMC150_MAGN_REG_Z_L:
  185. case BMC150_MAGN_REG_Z_M:
  186. case BMC150_MAGN_REG_RHALL_L:
  187. case BMC150_MAGN_REG_RHALL_M:
  188. case BMC150_MAGN_REG_INT_STATUS:
  189. return true;
  190. default:
  191. return false;
  192. }
  193. }
  194. const struct regmap_config bmc150_magn_regmap_config = {
  195. .reg_bits = 8,
  196. .val_bits = 8,
  197. .max_register = BMC150_MAGN_REG_TRIM_END,
  198. .cache_type = REGCACHE_RBTREE,
  199. .writeable_reg = bmc150_magn_is_writeable_reg,
  200. .volatile_reg = bmc150_magn_is_volatile_reg,
  201. };
  202. EXPORT_SYMBOL(bmc150_magn_regmap_config);
  203. static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
  204. enum bmc150_magn_power_modes mode,
  205. bool state)
  206. {
  207. int ret;
  208. switch (mode) {
  209. case BMC150_MAGN_POWER_MODE_SUSPEND:
  210. ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER,
  211. BMC150_MAGN_MASK_POWER_CTL, !state);
  212. if (ret < 0)
  213. return ret;
  214. usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000);
  215. return 0;
  216. case BMC150_MAGN_POWER_MODE_SLEEP:
  217. return regmap_update_bits(data->regmap,
  218. BMC150_MAGN_REG_OPMODE_ODR,
  219. BMC150_MAGN_MASK_OPMODE,
  220. BMC150_MAGN_MODE_SLEEP <<
  221. BMC150_MAGN_SHIFT_OPMODE);
  222. case BMC150_MAGN_POWER_MODE_NORMAL:
  223. return regmap_update_bits(data->regmap,
  224. BMC150_MAGN_REG_OPMODE_ODR,
  225. BMC150_MAGN_MASK_OPMODE,
  226. BMC150_MAGN_MODE_NORMAL <<
  227. BMC150_MAGN_SHIFT_OPMODE);
  228. }
  229. return -EINVAL;
  230. }
  231. static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
  232. {
  233. #ifdef CONFIG_PM
  234. int ret;
  235. if (on) {
  236. ret = pm_runtime_get_sync(data->dev);
  237. } else {
  238. pm_runtime_mark_last_busy(data->dev);
  239. ret = pm_runtime_put_autosuspend(data->dev);
  240. }
  241. if (ret < 0) {
  242. dev_err(data->dev,
  243. "failed to change power state to %d\n", on);
  244. if (on)
  245. pm_runtime_put_noidle(data->dev);
  246. return ret;
  247. }
  248. #endif
  249. return 0;
  250. }
  251. static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
  252. {
  253. int ret, reg_val;
  254. u8 i, odr_val;
  255. ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val);
  256. if (ret < 0)
  257. return ret;
  258. odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
  259. for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
  260. if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) {
  261. *val = bmc150_magn_samp_freq_table[i].freq;
  262. return 0;
  263. }
  264. return -EINVAL;
  265. }
  266. static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
  267. {
  268. int ret;
  269. u8 i;
  270. for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
  271. if (bmc150_magn_samp_freq_table[i].freq == val) {
  272. ret = regmap_update_bits(data->regmap,
  273. BMC150_MAGN_REG_OPMODE_ODR,
  274. BMC150_MAGN_MASK_ODR,
  275. bmc150_magn_samp_freq_table[i].
  276. reg_val <<
  277. BMC150_MAGN_SHIFT_ODR);
  278. if (ret < 0)
  279. return ret;
  280. return 0;
  281. }
  282. }
  283. return -EINVAL;
  284. }
  285. static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
  286. int rep_z, int odr)
  287. {
  288. int ret, reg_val, max_odr;
  289. if (rep_xy <= 0) {
  290. ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
  291. &reg_val);
  292. if (ret < 0)
  293. return ret;
  294. rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val);
  295. }
  296. if (rep_z <= 0) {
  297. ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
  298. &reg_val);
  299. if (ret < 0)
  300. return ret;
  301. rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val);
  302. }
  303. if (odr <= 0) {
  304. ret = bmc150_magn_get_odr(data, &odr);
  305. if (ret < 0)
  306. return ret;
  307. }
  308. /* the maximum selectable read-out frequency from datasheet */
  309. max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
  310. if (odr > max_odr) {
  311. dev_err(data->dev,
  312. "Can't set oversampling with sampling freq %d\n",
  313. odr);
  314. return -EINVAL;
  315. }
  316. data->max_odr = max_odr;
  317. return 0;
  318. }
  319. static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x,
  320. u16 rhall)
  321. {
  322. s16 val;
  323. u16 xyz1 = le16_to_cpu(tregs->xyz1);
  324. if (x == BMC150_MAGN_XY_OVERFLOW_VAL)
  325. return S32_MIN;
  326. if (!rhall)
  327. rhall = xyz1;
  328. val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
  329. val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) *
  330. ((s32)val)) >> 7)) + (((s32)val) *
  331. ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
  332. ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) +
  333. (((s16)tregs->x1) << 3);
  334. return (s32)val;
  335. }
  336. static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y,
  337. u16 rhall)
  338. {
  339. s16 val;
  340. u16 xyz1 = le16_to_cpu(tregs->xyz1);
  341. if (y == BMC150_MAGN_XY_OVERFLOW_VAL)
  342. return S32_MIN;
  343. if (!rhall)
  344. rhall = xyz1;
  345. val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000)));
  346. val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) *
  347. ((s32)val)) >> 7)) + (((s32)val) *
  348. ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) *
  349. ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) +
  350. (((s16)tregs->y1) << 3);
  351. return (s32)val;
  352. }
  353. static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z,
  354. u16 rhall)
  355. {
  356. s32 val;
  357. u16 xyz1 = le16_to_cpu(tregs->xyz1);
  358. u16 z1 = le16_to_cpu(tregs->z1);
  359. s16 z2 = le16_to_cpu(tregs->z2);
  360. s16 z3 = le16_to_cpu(tregs->z3);
  361. s16 z4 = le16_to_cpu(tregs->z4);
  362. if (z == BMC150_MAGN_Z_OVERFLOW_VAL)
  363. return S32_MIN;
  364. val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) -
  365. ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) *
  366. ((((s16)rhall) << 1))) + (1 << 15)) >> 16))));
  367. return val;
  368. }
  369. static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer)
  370. {
  371. int ret;
  372. __le16 values[AXIS_XYZR_MAX];
  373. s16 raw_x, raw_y, raw_z;
  374. u16 rhall;
  375. struct bmc150_magn_trim_regs tregs;
  376. ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L,
  377. values, sizeof(values));
  378. if (ret < 0)
  379. return ret;
  380. raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L;
  381. raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L;
  382. raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L;
  383. rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L;
  384. ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START,
  385. &tregs, sizeof(tregs));
  386. if (ret < 0)
  387. return ret;
  388. buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall);
  389. buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall);
  390. buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall);
  391. return 0;
  392. }
  393. static int bmc150_magn_read_raw(struct iio_dev *indio_dev,
  394. struct iio_chan_spec const *chan,
  395. int *val, int *val2, long mask)
  396. {
  397. struct bmc150_magn_data *data = iio_priv(indio_dev);
  398. int ret, tmp;
  399. s32 values[AXIS_XYZ_MAX];
  400. switch (mask) {
  401. case IIO_CHAN_INFO_RAW:
  402. if (iio_buffer_enabled(indio_dev))
  403. return -EBUSY;
  404. mutex_lock(&data->mutex);
  405. ret = bmc150_magn_set_power_state(data, true);
  406. if (ret < 0) {
  407. mutex_unlock(&data->mutex);
  408. return ret;
  409. }
  410. ret = bmc150_magn_read_xyz(data, values);
  411. if (ret < 0) {
  412. bmc150_magn_set_power_state(data, false);
  413. mutex_unlock(&data->mutex);
  414. return ret;
  415. }
  416. *val = values[chan->scan_index];
  417. ret = bmc150_magn_set_power_state(data, false);
  418. if (ret < 0) {
  419. mutex_unlock(&data->mutex);
  420. return ret;
  421. }
  422. mutex_unlock(&data->mutex);
  423. return IIO_VAL_INT;
  424. case IIO_CHAN_INFO_SCALE:
  425. /*
  426. * The API/driver performs an off-chip temperature
  427. * compensation and outputs x/y/z magnetic field data in
  428. * 16 LSB/uT to the upper application layer.
  429. */
  430. *val = 0;
  431. *val2 = 625;
  432. return IIO_VAL_INT_PLUS_MICRO;
  433. case IIO_CHAN_INFO_SAMP_FREQ:
  434. ret = bmc150_magn_get_odr(data, val);
  435. if (ret < 0)
  436. return ret;
  437. return IIO_VAL_INT;
  438. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  439. switch (chan->channel2) {
  440. case IIO_MOD_X:
  441. case IIO_MOD_Y:
  442. ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY,
  443. &tmp);
  444. if (ret < 0)
  445. return ret;
  446. *val = BMC150_MAGN_REGVAL_TO_REPXY(tmp);
  447. return IIO_VAL_INT;
  448. case IIO_MOD_Z:
  449. ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z,
  450. &tmp);
  451. if (ret < 0)
  452. return ret;
  453. *val = BMC150_MAGN_REGVAL_TO_REPZ(tmp);
  454. return IIO_VAL_INT;
  455. default:
  456. return -EINVAL;
  457. }
  458. default:
  459. return -EINVAL;
  460. }
  461. }
  462. static int bmc150_magn_write_raw(struct iio_dev *indio_dev,
  463. struct iio_chan_spec const *chan,
  464. int val, int val2, long mask)
  465. {
  466. struct bmc150_magn_data *data = iio_priv(indio_dev);
  467. int ret;
  468. switch (mask) {
  469. case IIO_CHAN_INFO_SAMP_FREQ:
  470. if (val > data->max_odr)
  471. return -EINVAL;
  472. mutex_lock(&data->mutex);
  473. ret = bmc150_magn_set_odr(data, val);
  474. mutex_unlock(&data->mutex);
  475. return ret;
  476. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  477. switch (chan->channel2) {
  478. case IIO_MOD_X:
  479. case IIO_MOD_Y:
  480. if (val < 1 || val > 511)
  481. return -EINVAL;
  482. mutex_lock(&data->mutex);
  483. ret = bmc150_magn_set_max_odr(data, val, 0, 0);
  484. if (ret < 0) {
  485. mutex_unlock(&data->mutex);
  486. return ret;
  487. }
  488. ret = regmap_update_bits(data->regmap,
  489. BMC150_MAGN_REG_REP_XY,
  490. BMC150_MAGN_REG_REP_DATAMASK,
  491. BMC150_MAGN_REPXY_TO_REGVAL
  492. (val));
  493. mutex_unlock(&data->mutex);
  494. return ret;
  495. case IIO_MOD_Z:
  496. if (val < 1 || val > 256)
  497. return -EINVAL;
  498. mutex_lock(&data->mutex);
  499. ret = bmc150_magn_set_max_odr(data, 0, val, 0);
  500. if (ret < 0) {
  501. mutex_unlock(&data->mutex);
  502. return ret;
  503. }
  504. ret = regmap_update_bits(data->regmap,
  505. BMC150_MAGN_REG_REP_Z,
  506. BMC150_MAGN_REG_REP_DATAMASK,
  507. BMC150_MAGN_REPZ_TO_REGVAL
  508. (val));
  509. mutex_unlock(&data->mutex);
  510. return ret;
  511. default:
  512. return -EINVAL;
  513. }
  514. default:
  515. return -EINVAL;
  516. }
  517. }
  518. static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev,
  519. struct device_attribute *attr,
  520. char *buf)
  521. {
  522. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  523. struct bmc150_magn_data *data = iio_priv(indio_dev);
  524. size_t len = 0;
  525. u8 i;
  526. for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
  527. if (bmc150_magn_samp_freq_table[i].freq > data->max_odr)
  528. break;
  529. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
  530. bmc150_magn_samp_freq_table[i].freq);
  531. }
  532. /* replace last space with a newline */
  533. buf[len - 1] = '\n';
  534. return len;
  535. }
  536. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
  537. static struct attribute *bmc150_magn_attributes[] = {
  538. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  539. NULL,
  540. };
  541. static const struct attribute_group bmc150_magn_attrs_group = {
  542. .attrs = bmc150_magn_attributes,
  543. };
  544. #define BMC150_MAGN_CHANNEL(_axis) { \
  545. .type = IIO_MAGN, \
  546. .modified = 1, \
  547. .channel2 = IIO_MOD_##_axis, \
  548. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  549. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  550. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  551. BIT(IIO_CHAN_INFO_SCALE), \
  552. .scan_index = AXIS_##_axis, \
  553. .scan_type = { \
  554. .sign = 's', \
  555. .realbits = 32, \
  556. .storagebits = 32, \
  557. .endianness = IIO_LE \
  558. }, \
  559. }
  560. static const struct iio_chan_spec bmc150_magn_channels[] = {
  561. BMC150_MAGN_CHANNEL(X),
  562. BMC150_MAGN_CHANNEL(Y),
  563. BMC150_MAGN_CHANNEL(Z),
  564. IIO_CHAN_SOFT_TIMESTAMP(3),
  565. };
  566. static const struct iio_info bmc150_magn_info = {
  567. .attrs = &bmc150_magn_attrs_group,
  568. .read_raw = bmc150_magn_read_raw,
  569. .write_raw = bmc150_magn_write_raw,
  570. .driver_module = THIS_MODULE,
  571. };
  572. static const unsigned long bmc150_magn_scan_masks[] = {
  573. BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  574. 0};
  575. static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p)
  576. {
  577. struct iio_poll_func *pf = p;
  578. struct iio_dev *indio_dev = pf->indio_dev;
  579. struct bmc150_magn_data *data = iio_priv(indio_dev);
  580. int ret;
  581. mutex_lock(&data->mutex);
  582. ret = bmc150_magn_read_xyz(data, data->buffer);
  583. if (ret < 0)
  584. goto err;
  585. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  586. pf->timestamp);
  587. err:
  588. mutex_unlock(&data->mutex);
  589. iio_trigger_notify_done(indio_dev->trig);
  590. return IRQ_HANDLED;
  591. }
  592. static int bmc150_magn_init(struct bmc150_magn_data *data)
  593. {
  594. int ret, chip_id;
  595. struct bmc150_magn_preset preset;
  596. ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
  597. false);
  598. if (ret < 0) {
  599. dev_err(data->dev,
  600. "Failed to bring up device from suspend mode\n");
  601. return ret;
  602. }
  603. ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
  604. if (ret < 0) {
  605. dev_err(data->dev, "Failed reading chip id\n");
  606. goto err_poweroff;
  607. }
  608. if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
  609. dev_err(data->dev, "Invalid chip id 0x%x\n", chip_id);
  610. ret = -ENODEV;
  611. goto err_poweroff;
  612. }
  613. dev_dbg(data->dev, "Chip id %x\n", chip_id);
  614. preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
  615. ret = bmc150_magn_set_odr(data, preset.odr);
  616. if (ret < 0) {
  617. dev_err(data->dev, "Failed to set ODR to %d\n",
  618. preset.odr);
  619. goto err_poweroff;
  620. }
  621. ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
  622. BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
  623. if (ret < 0) {
  624. dev_err(data->dev, "Failed to set REP XY to %d\n",
  625. preset.rep_xy);
  626. goto err_poweroff;
  627. }
  628. ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
  629. BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
  630. if (ret < 0) {
  631. dev_err(data->dev, "Failed to set REP Z to %d\n",
  632. preset.rep_z);
  633. goto err_poweroff;
  634. }
  635. ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z,
  636. preset.odr);
  637. if (ret < 0)
  638. goto err_poweroff;
  639. ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
  640. true);
  641. if (ret < 0) {
  642. dev_err(data->dev, "Failed to power on device\n");
  643. goto err_poweroff;
  644. }
  645. return 0;
  646. err_poweroff:
  647. bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
  648. return ret;
  649. }
  650. static int bmc150_magn_reset_intr(struct bmc150_magn_data *data)
  651. {
  652. int tmp;
  653. /*
  654. * Data Ready (DRDY) is always cleared after
  655. * readout of data registers ends.
  656. */
  657. return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp);
  658. }
  659. static int bmc150_magn_trig_try_reen(struct iio_trigger *trig)
  660. {
  661. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  662. struct bmc150_magn_data *data = iio_priv(indio_dev);
  663. int ret;
  664. if (!data->dready_trigger_on)
  665. return 0;
  666. mutex_lock(&data->mutex);
  667. ret = bmc150_magn_reset_intr(data);
  668. mutex_unlock(&data->mutex);
  669. return ret;
  670. }
  671. static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
  672. bool state)
  673. {
  674. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  675. struct bmc150_magn_data *data = iio_priv(indio_dev);
  676. int ret = 0;
  677. mutex_lock(&data->mutex);
  678. if (state == data->dready_trigger_on)
  679. goto err_unlock;
  680. ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
  681. BMC150_MAGN_MASK_DRDY_EN,
  682. state << BMC150_MAGN_SHIFT_DRDY_EN);
  683. if (ret < 0)
  684. goto err_unlock;
  685. data->dready_trigger_on = state;
  686. if (state) {
  687. ret = bmc150_magn_reset_intr(data);
  688. if (ret < 0)
  689. goto err_unlock;
  690. }
  691. mutex_unlock(&data->mutex);
  692. return 0;
  693. err_unlock:
  694. mutex_unlock(&data->mutex);
  695. return ret;
  696. }
  697. static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
  698. .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state,
  699. .try_reenable = bmc150_magn_trig_try_reen,
  700. .owner = THIS_MODULE,
  701. };
  702. static int bmc150_magn_buffer_preenable(struct iio_dev *indio_dev)
  703. {
  704. struct bmc150_magn_data *data = iio_priv(indio_dev);
  705. return bmc150_magn_set_power_state(data, true);
  706. }
  707. static int bmc150_magn_buffer_postdisable(struct iio_dev *indio_dev)
  708. {
  709. struct bmc150_magn_data *data = iio_priv(indio_dev);
  710. return bmc150_magn_set_power_state(data, false);
  711. }
  712. static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = {
  713. .preenable = bmc150_magn_buffer_preenable,
  714. .postenable = iio_triggered_buffer_postenable,
  715. .predisable = iio_triggered_buffer_predisable,
  716. .postdisable = bmc150_magn_buffer_postdisable,
  717. };
  718. static const char *bmc150_magn_match_acpi_device(struct device *dev)
  719. {
  720. const struct acpi_device_id *id;
  721. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  722. if (!id)
  723. return NULL;
  724. return dev_name(dev);
  725. }
  726. int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
  727. int irq, const char *name)
  728. {
  729. struct bmc150_magn_data *data;
  730. struct iio_dev *indio_dev;
  731. int ret;
  732. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  733. if (!indio_dev)
  734. return -ENOMEM;
  735. data = iio_priv(indio_dev);
  736. dev_set_drvdata(dev, indio_dev);
  737. data->regmap = regmap;
  738. data->irq = irq;
  739. data->dev = dev;
  740. if (!name && ACPI_HANDLE(dev))
  741. name = bmc150_magn_match_acpi_device(dev);
  742. mutex_init(&data->mutex);
  743. ret = bmc150_magn_init(data);
  744. if (ret < 0)
  745. return ret;
  746. indio_dev->dev.parent = dev;
  747. indio_dev->channels = bmc150_magn_channels;
  748. indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
  749. indio_dev->available_scan_masks = bmc150_magn_scan_masks;
  750. indio_dev->name = name;
  751. indio_dev->modes = INDIO_DIRECT_MODE;
  752. indio_dev->info = &bmc150_magn_info;
  753. if (irq > 0) {
  754. data->dready_trig = devm_iio_trigger_alloc(dev,
  755. "%s-dev%d",
  756. indio_dev->name,
  757. indio_dev->id);
  758. if (!data->dready_trig) {
  759. ret = -ENOMEM;
  760. dev_err(dev, "iio trigger alloc failed\n");
  761. goto err_poweroff;
  762. }
  763. data->dready_trig->dev.parent = dev;
  764. data->dready_trig->ops = &bmc150_magn_trigger_ops;
  765. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  766. ret = iio_trigger_register(data->dready_trig);
  767. if (ret) {
  768. dev_err(dev, "iio trigger register failed\n");
  769. goto err_poweroff;
  770. }
  771. ret = request_threaded_irq(irq,
  772. iio_trigger_generic_data_rdy_poll,
  773. NULL,
  774. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  775. BMC150_MAGN_IRQ_NAME,
  776. data->dready_trig);
  777. if (ret < 0) {
  778. dev_err(dev, "request irq %d failed\n", irq);
  779. goto err_trigger_unregister;
  780. }
  781. }
  782. ret = iio_triggered_buffer_setup(indio_dev,
  783. iio_pollfunc_store_time,
  784. bmc150_magn_trigger_handler,
  785. &bmc150_magn_buffer_setup_ops);
  786. if (ret < 0) {
  787. dev_err(dev, "iio triggered buffer setup failed\n");
  788. goto err_free_irq;
  789. }
  790. ret = pm_runtime_set_active(dev);
  791. if (ret)
  792. goto err_buffer_cleanup;
  793. pm_runtime_enable(dev);
  794. pm_runtime_set_autosuspend_delay(dev,
  795. BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
  796. pm_runtime_use_autosuspend(dev);
  797. ret = iio_device_register(indio_dev);
  798. if (ret < 0) {
  799. dev_err(dev, "unable to register iio device\n");
  800. goto err_buffer_cleanup;
  801. }
  802. dev_dbg(dev, "Registered device %s\n", name);
  803. return 0;
  804. err_buffer_cleanup:
  805. iio_triggered_buffer_cleanup(indio_dev);
  806. err_free_irq:
  807. if (irq > 0)
  808. free_irq(irq, data->dready_trig);
  809. err_trigger_unregister:
  810. if (data->dready_trig)
  811. iio_trigger_unregister(data->dready_trig);
  812. err_poweroff:
  813. bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
  814. return ret;
  815. }
  816. EXPORT_SYMBOL(bmc150_magn_probe);
  817. int bmc150_magn_remove(struct device *dev)
  818. {
  819. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  820. struct bmc150_magn_data *data = iio_priv(indio_dev);
  821. iio_device_unregister(indio_dev);
  822. pm_runtime_disable(dev);
  823. pm_runtime_set_suspended(dev);
  824. pm_runtime_put_noidle(dev);
  825. iio_triggered_buffer_cleanup(indio_dev);
  826. if (data->irq > 0)
  827. free_irq(data->irq, data->dready_trig);
  828. if (data->dready_trig)
  829. iio_trigger_unregister(data->dready_trig);
  830. mutex_lock(&data->mutex);
  831. bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
  832. mutex_unlock(&data->mutex);
  833. return 0;
  834. }
  835. EXPORT_SYMBOL(bmc150_magn_remove);
  836. #ifdef CONFIG_PM
  837. static int bmc150_magn_runtime_suspend(struct device *dev)
  838. {
  839. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  840. struct bmc150_magn_data *data = iio_priv(indio_dev);
  841. int ret;
  842. mutex_lock(&data->mutex);
  843. ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
  844. true);
  845. mutex_unlock(&data->mutex);
  846. if (ret < 0) {
  847. dev_err(dev, "powering off device failed\n");
  848. return ret;
  849. }
  850. return 0;
  851. }
  852. /*
  853. * Should be called with data->mutex held.
  854. */
  855. static int bmc150_magn_runtime_resume(struct device *dev)
  856. {
  857. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  858. struct bmc150_magn_data *data = iio_priv(indio_dev);
  859. return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
  860. true);
  861. }
  862. #endif
  863. #ifdef CONFIG_PM_SLEEP
  864. static int bmc150_magn_suspend(struct device *dev)
  865. {
  866. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  867. struct bmc150_magn_data *data = iio_priv(indio_dev);
  868. int ret;
  869. mutex_lock(&data->mutex);
  870. ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP,
  871. true);
  872. mutex_unlock(&data->mutex);
  873. return ret;
  874. }
  875. static int bmc150_magn_resume(struct device *dev)
  876. {
  877. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  878. struct bmc150_magn_data *data = iio_priv(indio_dev);
  879. int ret;
  880. mutex_lock(&data->mutex);
  881. ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
  882. true);
  883. mutex_unlock(&data->mutex);
  884. return ret;
  885. }
  886. #endif
  887. const struct dev_pm_ops bmc150_magn_pm_ops = {
  888. SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
  889. SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
  890. bmc150_magn_runtime_resume, NULL)
  891. };
  892. EXPORT_SYMBOL(bmc150_magn_pm_ops);
  893. MODULE_AUTHOR("Irina Tirdea <[email protected]>");
  894. MODULE_LICENSE("GPL v2");
  895. MODULE_DESCRIPTION("BMC150 magnetometer core driver");