hmc5843_core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Device driver for the the HMC5843 multi-chip module designed
  3. * for low field magnetic sensing.
  4. *
  5. * Copyright (C) 2010 Texas Instruments
  6. *
  7. * Author: Shubhrajyoti Datta <shubhrajyoti@ti.com>
  8. * Acknowledgment: Jonathan Cameron <jic23@kernel.org> for valuable inputs.
  9. * Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>.
  10. * Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/regmap.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/trigger_consumer.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #include <linux/delay.h>
  30. #include "hmc5843.h"
  31. /*
  32. * Range gain settings in (+-)Ga
  33. * Beware: HMC5843 and HMC5883 have different recommended sensor field
  34. * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively
  35. */
  36. #define HMC5843_RANGE_GAIN_OFFSET 0x05
  37. #define HMC5843_RANGE_GAIN_DEFAULT 0x01
  38. #define HMC5843_RANGE_GAIN_MASK 0xe0
  39. /* Device status */
  40. #define HMC5843_DATA_READY 0x01
  41. #define HMC5843_DATA_OUTPUT_LOCK 0x02
  42. /* Mode register configuration */
  43. #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00
  44. #define HMC5843_MODE_CONVERSION_SINGLE 0x01
  45. #define HMC5843_MODE_IDLE 0x02
  46. #define HMC5843_MODE_SLEEP 0x03
  47. #define HMC5843_MODE_MASK 0x03
  48. /*
  49. * HMC5843: Minimum data output rate
  50. * HMC5883: Typical data output rate
  51. */
  52. #define HMC5843_RATE_OFFSET 0x02
  53. #define HMC5843_RATE_DEFAULT 0x04
  54. #define HMC5843_RATE_MASK 0x1c
  55. /* Device measurement configuration */
  56. #define HMC5843_MEAS_CONF_NORMAL 0x00
  57. #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01
  58. #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02
  59. #define HMC5843_MEAS_CONF_MASK 0x03
  60. /*
  61. * API for setting the measurement configuration to
  62. * Normal, Positive bias and Negative bias
  63. *
  64. * From the datasheet:
  65. * 0 - Normal measurement configuration (default): In normal measurement
  66. * configuration the device follows normal measurement flow. Pins BP
  67. * and BN are left floating and high impedance.
  68. *
  69. * 1 - Positive bias configuration: In positive bias configuration, a
  70. * positive current is forced across the resistive load on pins BP
  71. * and BN.
  72. *
  73. * 2 - Negative bias configuration. In negative bias configuration, a
  74. * negative current is forced across the resistive load on pins BP
  75. * and BN.
  76. *
  77. * 3 - Only available on HMC5983. Magnetic sensor is disabled.
  78. * Temperature sensor is enabled.
  79. */
  80. static const char *const hmc5843_meas_conf_modes[] = {"normal", "positivebias",
  81. "negativebias"};
  82. static const char *const hmc5983_meas_conf_modes[] = {"normal", "positivebias",
  83. "negativebias",
  84. "disabled"};
  85. /* Scaling factors: 10000000/Gain */
  86. static const int hmc5843_regval_to_nanoscale[] = {
  87. 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714
  88. };
  89. static const int hmc5883_regval_to_nanoscale[] = {
  90. 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662
  91. };
  92. static const int hmc5883l_regval_to_nanoscale[] = {
  93. 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478
  94. };
  95. /*
  96. * From the datasheet:
  97. * Value | HMC5843 | HMC5883/HMC5883L
  98. * | Data output rate (Hz) | Data output rate (Hz)
  99. * 0 | 0.5 | 0.75
  100. * 1 | 1 | 1.5
  101. * 2 | 2 | 3
  102. * 3 | 5 | 7.5
  103. * 4 | 10 (default) | 15
  104. * 5 | 20 | 30
  105. * 6 | 50 | 75
  106. * 7 | Not used | Not used
  107. */
  108. static const int hmc5843_regval_to_samp_freq[][2] = {
  109. {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0}
  110. };
  111. static const int hmc5883_regval_to_samp_freq[][2] = {
  112. {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
  113. {75, 0}
  114. };
  115. static const int hmc5983_regval_to_samp_freq[][2] = {
  116. {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0},
  117. {75, 0}, {220, 0}
  118. };
  119. /* Describe chip variants */
  120. struct hmc5843_chip_info {
  121. const struct iio_chan_spec *channels;
  122. const int (*regval_to_samp_freq)[2];
  123. const int n_regval_to_samp_freq;
  124. const int *regval_to_nanoscale;
  125. const int n_regval_to_nanoscale;
  126. };
  127. /* The lower two bits contain the current conversion mode */
  128. static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode)
  129. {
  130. int ret;
  131. mutex_lock(&data->lock);
  132. ret = regmap_update_bits(data->regmap, HMC5843_MODE_REG,
  133. HMC5843_MODE_MASK, operating_mode);
  134. mutex_unlock(&data->lock);
  135. return ret;
  136. }
  137. static int hmc5843_wait_measurement(struct hmc5843_data *data)
  138. {
  139. int tries = 150;
  140. unsigned int val;
  141. int ret;
  142. while (tries-- > 0) {
  143. ret = regmap_read(data->regmap, HMC5843_STATUS_REG, &val);
  144. if (ret < 0)
  145. return ret;
  146. if (val & HMC5843_DATA_READY)
  147. break;
  148. msleep(20);
  149. }
  150. if (tries < 0) {
  151. dev_err(data->dev, "data not ready\n");
  152. return -EIO;
  153. }
  154. return 0;
  155. }
  156. /* Return the measurement value from the specified channel */
  157. static int hmc5843_read_measurement(struct hmc5843_data *data,
  158. int idx, int *val)
  159. {
  160. __be16 values[3];
  161. int ret;
  162. mutex_lock(&data->lock);
  163. ret = hmc5843_wait_measurement(data);
  164. if (ret < 0) {
  165. mutex_unlock(&data->lock);
  166. return ret;
  167. }
  168. ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
  169. values, sizeof(values));
  170. mutex_unlock(&data->lock);
  171. if (ret < 0)
  172. return ret;
  173. *val = sign_extend32(be16_to_cpu(values[idx]), 15);
  174. return IIO_VAL_INT;
  175. }
  176. static int hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
  177. {
  178. int ret;
  179. mutex_lock(&data->lock);
  180. ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
  181. HMC5843_MEAS_CONF_MASK, meas_conf);
  182. mutex_unlock(&data->lock);
  183. return ret;
  184. }
  185. static
  186. int hmc5843_show_measurement_configuration(struct iio_dev *indio_dev,
  187. const struct iio_chan_spec *chan)
  188. {
  189. struct hmc5843_data *data = iio_priv(indio_dev);
  190. unsigned int val;
  191. int ret;
  192. ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &val);
  193. if (ret)
  194. return ret;
  195. return val & HMC5843_MEAS_CONF_MASK;
  196. }
  197. static
  198. int hmc5843_set_measurement_configuration(struct iio_dev *indio_dev,
  199. const struct iio_chan_spec *chan,
  200. unsigned int meas_conf)
  201. {
  202. struct hmc5843_data *data = iio_priv(indio_dev);
  203. return hmc5843_set_meas_conf(data, meas_conf);
  204. }
  205. static const struct iio_enum hmc5843_meas_conf_enum = {
  206. .items = hmc5843_meas_conf_modes,
  207. .num_items = ARRAY_SIZE(hmc5843_meas_conf_modes),
  208. .get = hmc5843_show_measurement_configuration,
  209. .set = hmc5843_set_measurement_configuration,
  210. };
  211. static const struct iio_chan_spec_ext_info hmc5843_ext_info[] = {
  212. IIO_ENUM("meas_conf", true, &hmc5843_meas_conf_enum),
  213. IIO_ENUM_AVAILABLE("meas_conf", &hmc5843_meas_conf_enum),
  214. { },
  215. };
  216. static const struct iio_enum hmc5983_meas_conf_enum = {
  217. .items = hmc5983_meas_conf_modes,
  218. .num_items = ARRAY_SIZE(hmc5983_meas_conf_modes),
  219. .get = hmc5843_show_measurement_configuration,
  220. .set = hmc5843_set_measurement_configuration,
  221. };
  222. static const struct iio_chan_spec_ext_info hmc5983_ext_info[] = {
  223. IIO_ENUM("meas_conf", true, &hmc5983_meas_conf_enum),
  224. IIO_ENUM_AVAILABLE("meas_conf", &hmc5983_meas_conf_enum),
  225. { },
  226. };
  227. static
  228. ssize_t hmc5843_show_samp_freq_avail(struct device *dev,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
  232. size_t len = 0;
  233. int i;
  234. for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
  235. len += scnprintf(buf + len, PAGE_SIZE - len,
  236. "%d.%d ", data->variant->regval_to_samp_freq[i][0],
  237. data->variant->regval_to_samp_freq[i][1]);
  238. /* replace trailing space by newline */
  239. buf[len - 1] = '\n';
  240. return len;
  241. }
  242. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail);
  243. static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate)
  244. {
  245. int ret;
  246. mutex_lock(&data->lock);
  247. ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A,
  248. HMC5843_RATE_MASK,
  249. rate << HMC5843_RATE_OFFSET);
  250. mutex_unlock(&data->lock);
  251. return ret;
  252. }
  253. static int hmc5843_get_samp_freq_index(struct hmc5843_data *data,
  254. int val, int val2)
  255. {
  256. int i;
  257. for (i = 0; i < data->variant->n_regval_to_samp_freq; i++)
  258. if (val == data->variant->regval_to_samp_freq[i][0] &&
  259. val2 == data->variant->regval_to_samp_freq[i][1])
  260. return i;
  261. return -EINVAL;
  262. }
  263. static int hmc5843_set_range_gain(struct hmc5843_data *data, u8 range)
  264. {
  265. int ret;
  266. mutex_lock(&data->lock);
  267. ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_B,
  268. HMC5843_RANGE_GAIN_MASK,
  269. range << HMC5843_RANGE_GAIN_OFFSET);
  270. mutex_unlock(&data->lock);
  271. return ret;
  272. }
  273. static ssize_t hmc5843_show_scale_avail(struct device *dev,
  274. struct device_attribute *attr,
  275. char *buf)
  276. {
  277. struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
  278. size_t len = 0;
  279. int i;
  280. for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
  281. len += scnprintf(buf + len, PAGE_SIZE - len,
  282. "0.%09d ", data->variant->regval_to_nanoscale[i]);
  283. /* replace trailing space by newline */
  284. buf[len - 1] = '\n';
  285. return len;
  286. }
  287. static IIO_DEVICE_ATTR(scale_available, S_IRUGO,
  288. hmc5843_show_scale_avail, NULL, 0);
  289. static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2)
  290. {
  291. int i;
  292. if (val)
  293. return -EINVAL;
  294. for (i = 0; i < data->variant->n_regval_to_nanoscale; i++)
  295. if (val2 == data->variant->regval_to_nanoscale[i])
  296. return i;
  297. return -EINVAL;
  298. }
  299. static int hmc5843_read_raw(struct iio_dev *indio_dev,
  300. struct iio_chan_spec const *chan,
  301. int *val, int *val2, long mask)
  302. {
  303. struct hmc5843_data *data = iio_priv(indio_dev);
  304. unsigned int rval;
  305. int ret;
  306. switch (mask) {
  307. case IIO_CHAN_INFO_RAW:
  308. return hmc5843_read_measurement(data, chan->scan_index, val);
  309. case IIO_CHAN_INFO_SCALE:
  310. ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_B, &rval);
  311. if (ret < 0)
  312. return ret;
  313. rval >>= HMC5843_RANGE_GAIN_OFFSET;
  314. *val = 0;
  315. *val2 = data->variant->regval_to_nanoscale[rval];
  316. return IIO_VAL_INT_PLUS_NANO;
  317. case IIO_CHAN_INFO_SAMP_FREQ:
  318. ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &rval);
  319. if (ret < 0)
  320. return ret;
  321. rval >>= HMC5843_RATE_OFFSET;
  322. *val = data->variant->regval_to_samp_freq[rval][0];
  323. *val2 = data->variant->regval_to_samp_freq[rval][1];
  324. return IIO_VAL_INT_PLUS_MICRO;
  325. }
  326. return -EINVAL;
  327. }
  328. static int hmc5843_write_raw(struct iio_dev *indio_dev,
  329. struct iio_chan_spec const *chan,
  330. int val, int val2, long mask)
  331. {
  332. struct hmc5843_data *data = iio_priv(indio_dev);
  333. int rate, range;
  334. switch (mask) {
  335. case IIO_CHAN_INFO_SAMP_FREQ:
  336. rate = hmc5843_get_samp_freq_index(data, val, val2);
  337. if (rate < 0)
  338. return -EINVAL;
  339. return hmc5843_set_samp_freq(data, rate);
  340. case IIO_CHAN_INFO_SCALE:
  341. range = hmc5843_get_scale_index(data, val, val2);
  342. if (range < 0)
  343. return -EINVAL;
  344. return hmc5843_set_range_gain(data, range);
  345. default:
  346. return -EINVAL;
  347. }
  348. }
  349. static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev,
  350. struct iio_chan_spec const *chan,
  351. long mask)
  352. {
  353. switch (mask) {
  354. case IIO_CHAN_INFO_SAMP_FREQ:
  355. return IIO_VAL_INT_PLUS_MICRO;
  356. case IIO_CHAN_INFO_SCALE:
  357. return IIO_VAL_INT_PLUS_NANO;
  358. default:
  359. return -EINVAL;
  360. }
  361. }
  362. static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
  363. {
  364. struct iio_poll_func *pf = p;
  365. struct iio_dev *indio_dev = pf->indio_dev;
  366. struct hmc5843_data *data = iio_priv(indio_dev);
  367. int ret;
  368. mutex_lock(&data->lock);
  369. ret = hmc5843_wait_measurement(data);
  370. if (ret < 0) {
  371. mutex_unlock(&data->lock);
  372. goto done;
  373. }
  374. ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS,
  375. data->buffer, 3 * sizeof(__be16));
  376. mutex_unlock(&data->lock);
  377. if (ret < 0)
  378. goto done;
  379. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  380. iio_get_time_ns(indio_dev));
  381. done:
  382. iio_trigger_notify_done(indio_dev->trig);
  383. return IRQ_HANDLED;
  384. }
  385. #define HMC5843_CHANNEL(axis, idx) \
  386. { \
  387. .type = IIO_MAGN, \
  388. .modified = 1, \
  389. .channel2 = IIO_MOD_##axis, \
  390. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  391. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  392. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  393. .scan_index = idx, \
  394. .scan_type = { \
  395. .sign = 's', \
  396. .realbits = 16, \
  397. .storagebits = 16, \
  398. .endianness = IIO_BE, \
  399. }, \
  400. .ext_info = hmc5843_ext_info, \
  401. }
  402. #define HMC5983_CHANNEL(axis, idx) \
  403. { \
  404. .type = IIO_MAGN, \
  405. .modified = 1, \
  406. .channel2 = IIO_MOD_##axis, \
  407. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  408. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  409. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  410. .scan_index = idx, \
  411. .scan_type = { \
  412. .sign = 's', \
  413. .realbits = 16, \
  414. .storagebits = 16, \
  415. .endianness = IIO_BE, \
  416. }, \
  417. .ext_info = hmc5983_ext_info, \
  418. }
  419. static const struct iio_chan_spec hmc5843_channels[] = {
  420. HMC5843_CHANNEL(X, 0),
  421. HMC5843_CHANNEL(Y, 1),
  422. HMC5843_CHANNEL(Z, 2),
  423. IIO_CHAN_SOFT_TIMESTAMP(3),
  424. };
  425. /* Beware: Y and Z are exchanged on HMC5883 and 5983 */
  426. static const struct iio_chan_spec hmc5883_channels[] = {
  427. HMC5843_CHANNEL(X, 0),
  428. HMC5843_CHANNEL(Z, 1),
  429. HMC5843_CHANNEL(Y, 2),
  430. IIO_CHAN_SOFT_TIMESTAMP(3),
  431. };
  432. static const struct iio_chan_spec hmc5983_channels[] = {
  433. HMC5983_CHANNEL(X, 0),
  434. HMC5983_CHANNEL(Z, 1),
  435. HMC5983_CHANNEL(Y, 2),
  436. IIO_CHAN_SOFT_TIMESTAMP(3),
  437. };
  438. static struct attribute *hmc5843_attributes[] = {
  439. &iio_dev_attr_scale_available.dev_attr.attr,
  440. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  441. NULL
  442. };
  443. static const struct attribute_group hmc5843_group = {
  444. .attrs = hmc5843_attributes,
  445. };
  446. static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
  447. [HMC5843_ID] = {
  448. .channels = hmc5843_channels,
  449. .regval_to_samp_freq = hmc5843_regval_to_samp_freq,
  450. .n_regval_to_samp_freq =
  451. ARRAY_SIZE(hmc5843_regval_to_samp_freq),
  452. .regval_to_nanoscale = hmc5843_regval_to_nanoscale,
  453. .n_regval_to_nanoscale =
  454. ARRAY_SIZE(hmc5843_regval_to_nanoscale),
  455. },
  456. [HMC5883_ID] = {
  457. .channels = hmc5883_channels,
  458. .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
  459. .n_regval_to_samp_freq =
  460. ARRAY_SIZE(hmc5883_regval_to_samp_freq),
  461. .regval_to_nanoscale = hmc5883_regval_to_nanoscale,
  462. .n_regval_to_nanoscale =
  463. ARRAY_SIZE(hmc5883_regval_to_nanoscale),
  464. },
  465. [HMC5883L_ID] = {
  466. .channels = hmc5883_channels,
  467. .regval_to_samp_freq = hmc5883_regval_to_samp_freq,
  468. .n_regval_to_samp_freq =
  469. ARRAY_SIZE(hmc5883_regval_to_samp_freq),
  470. .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
  471. .n_regval_to_nanoscale =
  472. ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
  473. },
  474. [HMC5983_ID] = {
  475. .channels = hmc5983_channels,
  476. .regval_to_samp_freq = hmc5983_regval_to_samp_freq,
  477. .n_regval_to_samp_freq =
  478. ARRAY_SIZE(hmc5983_regval_to_samp_freq),
  479. .regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
  480. .n_regval_to_nanoscale =
  481. ARRAY_SIZE(hmc5883l_regval_to_nanoscale),
  482. }
  483. };
  484. static int hmc5843_init(struct hmc5843_data *data)
  485. {
  486. int ret;
  487. u8 id[3];
  488. ret = regmap_bulk_read(data->regmap, HMC5843_ID_REG,
  489. id, ARRAY_SIZE(id));
  490. if (ret < 0)
  491. return ret;
  492. if (id[0] != 'H' || id[1] != '4' || id[2] != '3') {
  493. dev_err(data->dev, "no HMC5843/5883/5883L/5983 sensor\n");
  494. return -ENODEV;
  495. }
  496. ret = hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL);
  497. if (ret < 0)
  498. return ret;
  499. ret = hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT);
  500. if (ret < 0)
  501. return ret;
  502. ret = hmc5843_set_range_gain(data, HMC5843_RANGE_GAIN_DEFAULT);
  503. if (ret < 0)
  504. return ret;
  505. return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS);
  506. }
  507. static const struct iio_info hmc5843_info = {
  508. .attrs = &hmc5843_group,
  509. .read_raw = &hmc5843_read_raw,
  510. .write_raw = &hmc5843_write_raw,
  511. .write_raw_get_fmt = &hmc5843_write_raw_get_fmt,
  512. };
  513. static const unsigned long hmc5843_scan_masks[] = {0x7, 0};
  514. int hmc5843_common_suspend(struct device *dev)
  515. {
  516. return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
  517. HMC5843_MODE_SLEEP);
  518. }
  519. EXPORT_SYMBOL(hmc5843_common_suspend);
  520. int hmc5843_common_resume(struct device *dev)
  521. {
  522. return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)),
  523. HMC5843_MODE_CONVERSION_CONTINUOUS);
  524. }
  525. EXPORT_SYMBOL(hmc5843_common_resume);
  526. int hmc5843_common_probe(struct device *dev, struct regmap *regmap,
  527. enum hmc5843_ids id, const char *name)
  528. {
  529. struct hmc5843_data *data;
  530. struct iio_dev *indio_dev;
  531. int ret;
  532. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  533. if (!indio_dev)
  534. return -ENOMEM;
  535. dev_set_drvdata(dev, indio_dev);
  536. /* default settings at probe */
  537. data = iio_priv(indio_dev);
  538. data->dev = dev;
  539. data->regmap = regmap;
  540. data->variant = &hmc5843_chip_info_tbl[id];
  541. mutex_init(&data->lock);
  542. indio_dev->dev.parent = dev;
  543. indio_dev->name = name;
  544. indio_dev->info = &hmc5843_info;
  545. indio_dev->modes = INDIO_DIRECT_MODE;
  546. indio_dev->channels = data->variant->channels;
  547. indio_dev->num_channels = 4;
  548. indio_dev->available_scan_masks = hmc5843_scan_masks;
  549. ret = hmc5843_init(data);
  550. if (ret < 0)
  551. return ret;
  552. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  553. hmc5843_trigger_handler, NULL);
  554. if (ret < 0)
  555. goto buffer_setup_err;
  556. ret = iio_device_register(indio_dev);
  557. if (ret < 0)
  558. goto buffer_cleanup;
  559. return 0;
  560. buffer_cleanup:
  561. iio_triggered_buffer_cleanup(indio_dev);
  562. buffer_setup_err:
  563. hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
  564. return ret;
  565. }
  566. EXPORT_SYMBOL(hmc5843_common_probe);
  567. int hmc5843_common_remove(struct device *dev)
  568. {
  569. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  570. iio_device_unregister(indio_dev);
  571. iio_triggered_buffer_cleanup(indio_dev);
  572. /* sleep mode to save power */
  573. hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP);
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(hmc5843_common_remove);
  577. MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>");
  578. MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver");
  579. MODULE_LICENSE("GPL");