mag3110.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * mag3110.c - Support for Freescale MAG3110 magnetometer sensor
  3. *
  4. * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * (7-bit I2C slave address 0x0e)
  11. *
  12. * TODO: irq, user offset, oversampling, continuous mode
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/trigger_consumer.h>
  19. #include <linux/iio/buffer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. #include <linux/delay.h>
  22. #define MAG3110_STATUS 0x00
  23. #define MAG3110_OUT_X 0x01 /* MSB first */
  24. #define MAG3110_OUT_Y 0x03
  25. #define MAG3110_OUT_Z 0x05
  26. #define MAG3110_WHO_AM_I 0x07
  27. #define MAG3110_SYSMOD 0x08
  28. #define MAG3110_OFF_X 0x09 /* MSB first */
  29. #define MAG3110_OFF_Y 0x0b
  30. #define MAG3110_OFF_Z 0x0d
  31. #define MAG3110_DIE_TEMP 0x0f
  32. #define MAG3110_CTRL_REG1 0x10
  33. #define MAG3110_CTRL_REG2 0x11
  34. #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
  35. #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5))
  36. #define MAG3110_CTRL_DR_SHIFT 5
  37. #define MAG3110_CTRL_DR_DEFAULT 0
  38. #define MAG3110_SYSMOD_MODE_MASK GENMASK(1, 0)
  39. #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
  40. #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
  41. #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */
  42. #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */
  43. #define MAG3110_DEVICE_ID 0xc4
  44. /* Each client has this additional data */
  45. struct mag3110_data {
  46. struct i2c_client *client;
  47. struct mutex lock;
  48. u8 ctrl_reg1;
  49. int sleep_val;
  50. };
  51. static int mag3110_request(struct mag3110_data *data)
  52. {
  53. int ret, tries = 150;
  54. if ((data->ctrl_reg1 & MAG3110_CTRL_AC) == 0) {
  55. /* trigger measurement */
  56. ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  57. data->ctrl_reg1 | MAG3110_CTRL_TM);
  58. if (ret < 0)
  59. return ret;
  60. }
  61. while (tries-- > 0) {
  62. ret = i2c_smbus_read_byte_data(data->client, MAG3110_STATUS);
  63. if (ret < 0)
  64. return ret;
  65. /* wait for data ready */
  66. if ((ret & MAG3110_STATUS_DRDY) == MAG3110_STATUS_DRDY)
  67. break;
  68. if (data->sleep_val <= 20)
  69. usleep_range(data->sleep_val * 250, data->sleep_val * 500);
  70. else
  71. msleep(20);
  72. }
  73. if (tries < 0) {
  74. dev_err(&data->client->dev, "data not ready\n");
  75. return -EIO;
  76. }
  77. return 0;
  78. }
  79. static int mag3110_read(struct mag3110_data *data, __be16 buf[3])
  80. {
  81. int ret;
  82. mutex_lock(&data->lock);
  83. ret = mag3110_request(data);
  84. if (ret < 0) {
  85. mutex_unlock(&data->lock);
  86. return ret;
  87. }
  88. ret = i2c_smbus_read_i2c_block_data(data->client,
  89. MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
  90. mutex_unlock(&data->lock);
  91. return ret;
  92. }
  93. static ssize_t mag3110_show_int_plus_micros(char *buf,
  94. const int (*vals)[2], int n)
  95. {
  96. size_t len = 0;
  97. while (n-- > 0)
  98. len += scnprintf(buf + len, PAGE_SIZE - len,
  99. "%d.%06d ", vals[n][0], vals[n][1]);
  100. /* replace trailing space by newline */
  101. buf[len - 1] = '\n';
  102. return len;
  103. }
  104. static int mag3110_get_int_plus_micros_index(const int (*vals)[2], int n,
  105. int val, int val2)
  106. {
  107. while (n-- > 0)
  108. if (val == vals[n][0] && val2 == vals[n][1])
  109. return n;
  110. return -EINVAL;
  111. }
  112. static const int mag3110_samp_freq[8][2] = {
  113. {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000},
  114. {1, 250000}, {0, 625000}
  115. };
  116. static ssize_t mag3110_show_samp_freq_avail(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. return mag3110_show_int_plus_micros(buf, mag3110_samp_freq, 8);
  120. }
  121. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail);
  122. static int mag3110_get_samp_freq_index(struct mag3110_data *data,
  123. int val, int val2)
  124. {
  125. return mag3110_get_int_plus_micros_index(mag3110_samp_freq, 8, val,
  126. val2);
  127. }
  128. static int mag3110_calculate_sleep(struct mag3110_data *data)
  129. {
  130. int ret, i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
  131. if (mag3110_samp_freq[i][0] > 0)
  132. ret = 1000 / mag3110_samp_freq[i][0];
  133. else
  134. ret = 1000;
  135. return ret == 0 ? 1 : ret;
  136. }
  137. static int mag3110_standby(struct mag3110_data *data)
  138. {
  139. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  140. data->ctrl_reg1 & ~MAG3110_CTRL_AC);
  141. }
  142. static int mag3110_wait_standby(struct mag3110_data *data)
  143. {
  144. int ret, tries = 30;
  145. /*
  146. * Takes up to 1/ODR to come out of active mode into stby
  147. * Longest expected period is 12.5seconds.
  148. * We'll sleep for 500ms between checks
  149. */
  150. while (tries-- > 0) {
  151. ret = i2c_smbus_read_byte_data(data->client, MAG3110_SYSMOD);
  152. if (ret < 0) {
  153. dev_err(&data->client->dev, "i2c error\n");
  154. return ret;
  155. }
  156. /* wait for standby */
  157. if ((ret & MAG3110_SYSMOD_MODE_MASK) == 0)
  158. break;
  159. msleep_interruptible(500);
  160. }
  161. if (tries < 0) {
  162. dev_err(&data->client->dev, "device not entering standby mode\n");
  163. return -EIO;
  164. }
  165. return 0;
  166. }
  167. static int mag3110_active(struct mag3110_data *data)
  168. {
  169. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  170. data->ctrl_reg1);
  171. }
  172. /* returns >0 if active, 0 if in standby and <0 on error */
  173. static int mag3110_is_active(struct mag3110_data *data)
  174. {
  175. int reg;
  176. reg = i2c_smbus_read_byte_data(data->client, MAG3110_CTRL_REG1);
  177. if (reg < 0)
  178. return reg;
  179. return reg & MAG3110_CTRL_AC;
  180. }
  181. static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
  182. {
  183. int ret;
  184. int is_active;
  185. mutex_lock(&data->lock);
  186. is_active = mag3110_is_active(data);
  187. if (is_active < 0) {
  188. ret = is_active;
  189. goto fail;
  190. }
  191. /* config can only be changed when in standby */
  192. if (is_active > 0) {
  193. ret = mag3110_standby(data);
  194. if (ret < 0)
  195. goto fail;
  196. }
  197. /*
  198. * After coming out of active we must wait for the part
  199. * to transition to STBY. This can take up to 1 /ODR to occur
  200. */
  201. ret = mag3110_wait_standby(data);
  202. if (ret < 0)
  203. goto fail;
  204. ret = i2c_smbus_write_byte_data(data->client, reg, val);
  205. if (ret < 0)
  206. goto fail;
  207. if (is_active > 0) {
  208. ret = mag3110_active(data);
  209. if (ret < 0)
  210. goto fail;
  211. }
  212. ret = 0;
  213. fail:
  214. mutex_unlock(&data->lock);
  215. return ret;
  216. }
  217. static int mag3110_read_raw(struct iio_dev *indio_dev,
  218. struct iio_chan_spec const *chan,
  219. int *val, int *val2, long mask)
  220. {
  221. struct mag3110_data *data = iio_priv(indio_dev);
  222. __be16 buffer[3];
  223. int i, ret;
  224. switch (mask) {
  225. case IIO_CHAN_INFO_RAW:
  226. ret = iio_device_claim_direct_mode(indio_dev);
  227. if (ret)
  228. return ret;
  229. switch (chan->type) {
  230. case IIO_MAGN: /* in 0.1 uT / LSB */
  231. ret = mag3110_read(data, buffer);
  232. if (ret < 0)
  233. goto release;
  234. *val = sign_extend32(
  235. be16_to_cpu(buffer[chan->scan_index]), 15);
  236. ret = IIO_VAL_INT;
  237. break;
  238. case IIO_TEMP: /* in 1 C / LSB */
  239. mutex_lock(&data->lock);
  240. ret = mag3110_request(data);
  241. if (ret < 0) {
  242. mutex_unlock(&data->lock);
  243. goto release;
  244. }
  245. ret = i2c_smbus_read_byte_data(data->client,
  246. MAG3110_DIE_TEMP);
  247. mutex_unlock(&data->lock);
  248. if (ret < 0)
  249. goto release;
  250. *val = sign_extend32(ret, 7);
  251. ret = IIO_VAL_INT;
  252. break;
  253. default:
  254. ret = -EINVAL;
  255. }
  256. release:
  257. iio_device_release_direct_mode(indio_dev);
  258. return ret;
  259. case IIO_CHAN_INFO_SCALE:
  260. switch (chan->type) {
  261. case IIO_MAGN:
  262. *val = 0;
  263. *val2 = 1000;
  264. return IIO_VAL_INT_PLUS_MICRO;
  265. case IIO_TEMP:
  266. *val = 1000;
  267. return IIO_VAL_INT;
  268. default:
  269. return -EINVAL;
  270. }
  271. case IIO_CHAN_INFO_SAMP_FREQ:
  272. i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
  273. *val = mag3110_samp_freq[i][0];
  274. *val2 = mag3110_samp_freq[i][1];
  275. return IIO_VAL_INT_PLUS_MICRO;
  276. case IIO_CHAN_INFO_CALIBBIAS:
  277. ret = i2c_smbus_read_word_swapped(data->client,
  278. MAG3110_OFF_X + 2 * chan->scan_index);
  279. if (ret < 0)
  280. return ret;
  281. *val = sign_extend32(ret >> 1, 14);
  282. return IIO_VAL_INT;
  283. }
  284. return -EINVAL;
  285. }
  286. static int mag3110_write_raw(struct iio_dev *indio_dev,
  287. struct iio_chan_spec const *chan,
  288. int val, int val2, long mask)
  289. {
  290. struct mag3110_data *data = iio_priv(indio_dev);
  291. int rate, ret;
  292. ret = iio_device_claim_direct_mode(indio_dev);
  293. if (ret)
  294. return ret;
  295. switch (mask) {
  296. case IIO_CHAN_INFO_SAMP_FREQ:
  297. rate = mag3110_get_samp_freq_index(data, val, val2);
  298. if (rate < 0) {
  299. ret = -EINVAL;
  300. break;
  301. }
  302. data->ctrl_reg1 &= 0xff & ~MAG3110_CTRL_DR_MASK
  303. & ~MAG3110_CTRL_AC;
  304. data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
  305. data->sleep_val = mag3110_calculate_sleep(data);
  306. if (data->sleep_val < 40)
  307. data->ctrl_reg1 |= MAG3110_CTRL_AC;
  308. ret = mag3110_change_config(data, MAG3110_CTRL_REG1,
  309. data->ctrl_reg1);
  310. break;
  311. case IIO_CHAN_INFO_CALIBBIAS:
  312. if (val < -10000 || val > 10000) {
  313. ret = -EINVAL;
  314. break;
  315. }
  316. ret = i2c_smbus_write_word_swapped(data->client,
  317. MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
  318. break;
  319. default:
  320. ret = -EINVAL;
  321. break;
  322. }
  323. iio_device_release_direct_mode(indio_dev);
  324. return ret;
  325. }
  326. static irqreturn_t mag3110_trigger_handler(int irq, void *p)
  327. {
  328. struct iio_poll_func *pf = p;
  329. struct iio_dev *indio_dev = pf->indio_dev;
  330. struct mag3110_data *data = iio_priv(indio_dev);
  331. u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */
  332. int ret;
  333. ret = mag3110_read(data, (__be16 *) buffer);
  334. if (ret < 0)
  335. goto done;
  336. if (test_bit(3, indio_dev->active_scan_mask)) {
  337. ret = i2c_smbus_read_byte_data(data->client,
  338. MAG3110_DIE_TEMP);
  339. if (ret < 0)
  340. goto done;
  341. buffer[6] = ret;
  342. }
  343. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  344. iio_get_time_ns(indio_dev));
  345. done:
  346. iio_trigger_notify_done(indio_dev->trig);
  347. return IRQ_HANDLED;
  348. }
  349. #define MAG3110_CHANNEL(axis, idx) { \
  350. .type = IIO_MAGN, \
  351. .modified = 1, \
  352. .channel2 = IIO_MOD_##axis, \
  353. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  354. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  355. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  356. BIT(IIO_CHAN_INFO_SCALE), \
  357. .scan_index = idx, \
  358. .scan_type = { \
  359. .sign = 's', \
  360. .realbits = 16, \
  361. .storagebits = 16, \
  362. .endianness = IIO_BE, \
  363. }, \
  364. }
  365. static const struct iio_chan_spec mag3110_channels[] = {
  366. MAG3110_CHANNEL(X, 0),
  367. MAG3110_CHANNEL(Y, 1),
  368. MAG3110_CHANNEL(Z, 2),
  369. {
  370. .type = IIO_TEMP,
  371. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  372. BIT(IIO_CHAN_INFO_SCALE),
  373. .scan_index = 3,
  374. .scan_type = {
  375. .sign = 's',
  376. .realbits = 8,
  377. .storagebits = 8,
  378. },
  379. },
  380. IIO_CHAN_SOFT_TIMESTAMP(4),
  381. };
  382. static struct attribute *mag3110_attributes[] = {
  383. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  384. NULL
  385. };
  386. static const struct attribute_group mag3110_group = {
  387. .attrs = mag3110_attributes,
  388. };
  389. static const struct iio_info mag3110_info = {
  390. .attrs = &mag3110_group,
  391. .read_raw = &mag3110_read_raw,
  392. .write_raw = &mag3110_write_raw,
  393. };
  394. static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
  395. static int mag3110_probe(struct i2c_client *client,
  396. const struct i2c_device_id *id)
  397. {
  398. struct mag3110_data *data;
  399. struct iio_dev *indio_dev;
  400. int ret;
  401. ret = i2c_smbus_read_byte_data(client, MAG3110_WHO_AM_I);
  402. if (ret < 0)
  403. return ret;
  404. if (ret != MAG3110_DEVICE_ID)
  405. return -ENODEV;
  406. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  407. if (!indio_dev)
  408. return -ENOMEM;
  409. data = iio_priv(indio_dev);
  410. data->client = client;
  411. mutex_init(&data->lock);
  412. i2c_set_clientdata(client, indio_dev);
  413. indio_dev->info = &mag3110_info;
  414. indio_dev->name = id->name;
  415. indio_dev->dev.parent = &client->dev;
  416. indio_dev->modes = INDIO_DIRECT_MODE;
  417. indio_dev->channels = mag3110_channels;
  418. indio_dev->num_channels = ARRAY_SIZE(mag3110_channels);
  419. indio_dev->available_scan_masks = mag3110_scan_masks;
  420. data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT << MAG3110_CTRL_DR_SHIFT;
  421. data->sleep_val = mag3110_calculate_sleep(data);
  422. if (data->sleep_val < 40)
  423. data->ctrl_reg1 |= MAG3110_CTRL_AC;
  424. ret = mag3110_change_config(data, MAG3110_CTRL_REG1, data->ctrl_reg1);
  425. if (ret < 0)
  426. return ret;
  427. ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2,
  428. MAG3110_CTRL_AUTO_MRST_EN);
  429. if (ret < 0)
  430. goto standby_on_error;
  431. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  432. mag3110_trigger_handler, NULL);
  433. if (ret < 0)
  434. goto standby_on_error;
  435. ret = iio_device_register(indio_dev);
  436. if (ret < 0)
  437. goto buffer_cleanup;
  438. return 0;
  439. buffer_cleanup:
  440. iio_triggered_buffer_cleanup(indio_dev);
  441. standby_on_error:
  442. mag3110_standby(iio_priv(indio_dev));
  443. return ret;
  444. }
  445. static int mag3110_remove(struct i2c_client *client)
  446. {
  447. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  448. iio_device_unregister(indio_dev);
  449. iio_triggered_buffer_cleanup(indio_dev);
  450. mag3110_standby(iio_priv(indio_dev));
  451. return 0;
  452. }
  453. #ifdef CONFIG_PM_SLEEP
  454. static int mag3110_suspend(struct device *dev)
  455. {
  456. return mag3110_standby(iio_priv(i2c_get_clientdata(
  457. to_i2c_client(dev))));
  458. }
  459. static int mag3110_resume(struct device *dev)
  460. {
  461. struct mag3110_data *data = iio_priv(i2c_get_clientdata(
  462. to_i2c_client(dev)));
  463. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  464. data->ctrl_reg1);
  465. }
  466. static SIMPLE_DEV_PM_OPS(mag3110_pm_ops, mag3110_suspend, mag3110_resume);
  467. #define MAG3110_PM_OPS (&mag3110_pm_ops)
  468. #else
  469. #define MAG3110_PM_OPS NULL
  470. #endif
  471. static const struct i2c_device_id mag3110_id[] = {
  472. { "mag3110", 0 },
  473. { }
  474. };
  475. MODULE_DEVICE_TABLE(i2c, mag3110_id);
  476. static const struct of_device_id mag3110_of_match[] = {
  477. { .compatible = "fsl,mag3110" },
  478. { }
  479. };
  480. MODULE_DEVICE_TABLE(of, mag3110_of_match);
  481. static struct i2c_driver mag3110_driver = {
  482. .driver = {
  483. .name = "mag3110",
  484. .of_match_table = mag3110_of_match,
  485. .pm = MAG3110_PM_OPS,
  486. },
  487. .probe = mag3110_probe,
  488. .remove = mag3110_remove,
  489. .id_table = mag3110_id,
  490. };
  491. module_i2c_driver(mag3110_driver);
  492. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  493. MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
  494. MODULE_LICENSE("GPL");