bmc150_magn.c 27 KB

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