mag3110.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_OFF_X 0x09 /* MSB first */
  28. #define MAG3110_OFF_Y 0x0b
  29. #define MAG3110_OFF_Z 0x0d
  30. #define MAG3110_DIE_TEMP 0x0f
  31. #define MAG3110_CTRL_REG1 0x10
  32. #define MAG3110_CTRL_REG2 0x11
  33. #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
  34. #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5))
  35. #define MAG3110_CTRL_DR_SHIFT 5
  36. #define MAG3110_CTRL_DR_DEFAULT 0
  37. #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
  38. #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
  39. #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */
  40. #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */
  41. #define MAG3110_DEVICE_ID 0xc4
  42. /* Each client has this additional data */
  43. struct mag3110_data {
  44. struct i2c_client *client;
  45. struct mutex lock;
  46. u8 ctrl_reg1;
  47. };
  48. static int mag3110_request(struct mag3110_data *data)
  49. {
  50. int ret, tries = 150;
  51. /* trigger measurement */
  52. ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  53. data->ctrl_reg1 | MAG3110_CTRL_TM);
  54. if (ret < 0)
  55. return ret;
  56. while (tries-- > 0) {
  57. ret = i2c_smbus_read_byte_data(data->client, MAG3110_STATUS);
  58. if (ret < 0)
  59. return ret;
  60. /* wait for data ready */
  61. if ((ret & MAG3110_STATUS_DRDY) == MAG3110_STATUS_DRDY)
  62. break;
  63. msleep(20);
  64. }
  65. if (tries < 0) {
  66. dev_err(&data->client->dev, "data not ready\n");
  67. return -EIO;
  68. }
  69. return 0;
  70. }
  71. static int mag3110_read(struct mag3110_data *data, __be16 buf[3])
  72. {
  73. int ret;
  74. mutex_lock(&data->lock);
  75. ret = mag3110_request(data);
  76. if (ret < 0) {
  77. mutex_unlock(&data->lock);
  78. return ret;
  79. }
  80. ret = i2c_smbus_read_i2c_block_data(data->client,
  81. MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
  82. mutex_unlock(&data->lock);
  83. return ret;
  84. }
  85. static ssize_t mag3110_show_int_plus_micros(char *buf,
  86. const int (*vals)[2], int n)
  87. {
  88. size_t len = 0;
  89. while (n-- > 0)
  90. len += scnprintf(buf + len, PAGE_SIZE - len,
  91. "%d.%06d ", vals[n][0], vals[n][1]);
  92. /* replace trailing space by newline */
  93. buf[len - 1] = '\n';
  94. return len;
  95. }
  96. static int mag3110_get_int_plus_micros_index(const int (*vals)[2], int n,
  97. int val, int val2)
  98. {
  99. while (n-- > 0)
  100. if (val == vals[n][0] && val2 == vals[n][1])
  101. return n;
  102. return -EINVAL;
  103. }
  104. static const int mag3110_samp_freq[8][2] = {
  105. {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000},
  106. {1, 250000}, {0, 625000}
  107. };
  108. static ssize_t mag3110_show_samp_freq_avail(struct device *dev,
  109. struct device_attribute *attr, char *buf)
  110. {
  111. return mag3110_show_int_plus_micros(buf, mag3110_samp_freq, 8);
  112. }
  113. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail);
  114. static int mag3110_get_samp_freq_index(struct mag3110_data *data,
  115. int val, int val2)
  116. {
  117. return mag3110_get_int_plus_micros_index(mag3110_samp_freq, 8, val,
  118. val2);
  119. }
  120. static int mag3110_read_raw(struct iio_dev *indio_dev,
  121. struct iio_chan_spec const *chan,
  122. int *val, int *val2, long mask)
  123. {
  124. struct mag3110_data *data = iio_priv(indio_dev);
  125. __be16 buffer[3];
  126. int i, ret;
  127. switch (mask) {
  128. case IIO_CHAN_INFO_RAW:
  129. if (iio_buffer_enabled(indio_dev))
  130. return -EBUSY;
  131. switch (chan->type) {
  132. case IIO_MAGN: /* in 0.1 uT / LSB */
  133. ret = mag3110_read(data, buffer);
  134. if (ret < 0)
  135. return ret;
  136. *val = sign_extend32(
  137. be16_to_cpu(buffer[chan->scan_index]), 15);
  138. return IIO_VAL_INT;
  139. case IIO_TEMP: /* in 1 C / LSB */
  140. mutex_lock(&data->lock);
  141. ret = mag3110_request(data);
  142. if (ret < 0) {
  143. mutex_unlock(&data->lock);
  144. return ret;
  145. }
  146. ret = i2c_smbus_read_byte_data(data->client,
  147. MAG3110_DIE_TEMP);
  148. mutex_unlock(&data->lock);
  149. if (ret < 0)
  150. return ret;
  151. *val = sign_extend32(ret, 7);
  152. return IIO_VAL_INT;
  153. default:
  154. return -EINVAL;
  155. }
  156. case IIO_CHAN_INFO_SCALE:
  157. switch (chan->type) {
  158. case IIO_MAGN:
  159. *val = 0;
  160. *val2 = 1000;
  161. return IIO_VAL_INT_PLUS_MICRO;
  162. case IIO_TEMP:
  163. *val = 1000;
  164. return IIO_VAL_INT;
  165. default:
  166. return -EINVAL;
  167. }
  168. case IIO_CHAN_INFO_SAMP_FREQ:
  169. i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
  170. *val = mag3110_samp_freq[i][0];
  171. *val2 = mag3110_samp_freq[i][1];
  172. return IIO_VAL_INT_PLUS_MICRO;
  173. case IIO_CHAN_INFO_CALIBBIAS:
  174. ret = i2c_smbus_read_word_swapped(data->client,
  175. MAG3110_OFF_X + 2 * chan->scan_index);
  176. if (ret < 0)
  177. return ret;
  178. *val = sign_extend32(ret >> 1, 14);
  179. return IIO_VAL_INT;
  180. }
  181. return -EINVAL;
  182. }
  183. static int mag3110_write_raw(struct iio_dev *indio_dev,
  184. struct iio_chan_spec const *chan,
  185. int val, int val2, long mask)
  186. {
  187. struct mag3110_data *data = iio_priv(indio_dev);
  188. int rate;
  189. if (iio_buffer_enabled(indio_dev))
  190. return -EBUSY;
  191. switch (mask) {
  192. case IIO_CHAN_INFO_SAMP_FREQ:
  193. rate = mag3110_get_samp_freq_index(data, val, val2);
  194. if (rate < 0)
  195. return -EINVAL;
  196. data->ctrl_reg1 &= ~MAG3110_CTRL_DR_MASK;
  197. data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
  198. return i2c_smbus_write_byte_data(data->client,
  199. MAG3110_CTRL_REG1, data->ctrl_reg1);
  200. case IIO_CHAN_INFO_CALIBBIAS:
  201. if (val < -10000 || val > 10000)
  202. return -EINVAL;
  203. return i2c_smbus_write_word_swapped(data->client,
  204. MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
  205. default:
  206. return -EINVAL;
  207. }
  208. }
  209. static irqreturn_t mag3110_trigger_handler(int irq, void *p)
  210. {
  211. struct iio_poll_func *pf = p;
  212. struct iio_dev *indio_dev = pf->indio_dev;
  213. struct mag3110_data *data = iio_priv(indio_dev);
  214. u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */
  215. int ret;
  216. ret = mag3110_read(data, (__be16 *) buffer);
  217. if (ret < 0)
  218. goto done;
  219. if (test_bit(3, indio_dev->active_scan_mask)) {
  220. ret = i2c_smbus_read_byte_data(data->client,
  221. MAG3110_DIE_TEMP);
  222. if (ret < 0)
  223. goto done;
  224. buffer[6] = ret;
  225. }
  226. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  227. iio_get_time_ns());
  228. done:
  229. iio_trigger_notify_done(indio_dev->trig);
  230. return IRQ_HANDLED;
  231. }
  232. #define MAG3110_CHANNEL(axis, idx) { \
  233. .type = IIO_MAGN, \
  234. .modified = 1, \
  235. .channel2 = IIO_MOD_##axis, \
  236. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  237. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  238. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  239. BIT(IIO_CHAN_INFO_SCALE), \
  240. .scan_index = idx, \
  241. .scan_type = { \
  242. .sign = 's', \
  243. .realbits = 16, \
  244. .storagebits = 16, \
  245. .endianness = IIO_BE, \
  246. }, \
  247. }
  248. static const struct iio_chan_spec mag3110_channels[] = {
  249. MAG3110_CHANNEL(X, 0),
  250. MAG3110_CHANNEL(Y, 1),
  251. MAG3110_CHANNEL(Z, 2),
  252. {
  253. .type = IIO_TEMP,
  254. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  255. BIT(IIO_CHAN_INFO_SCALE),
  256. .scan_index = 3,
  257. .scan_type = {
  258. .sign = 's',
  259. .realbits = 8,
  260. .storagebits = 8,
  261. },
  262. },
  263. IIO_CHAN_SOFT_TIMESTAMP(4),
  264. };
  265. static struct attribute *mag3110_attributes[] = {
  266. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  267. NULL
  268. };
  269. static const struct attribute_group mag3110_group = {
  270. .attrs = mag3110_attributes,
  271. };
  272. static const struct iio_info mag3110_info = {
  273. .attrs = &mag3110_group,
  274. .read_raw = &mag3110_read_raw,
  275. .write_raw = &mag3110_write_raw,
  276. .driver_module = THIS_MODULE,
  277. };
  278. static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
  279. static int mag3110_standby(struct mag3110_data *data)
  280. {
  281. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  282. data->ctrl_reg1 & ~MAG3110_CTRL_AC);
  283. }
  284. static int mag3110_probe(struct i2c_client *client,
  285. const struct i2c_device_id *id)
  286. {
  287. struct mag3110_data *data;
  288. struct iio_dev *indio_dev;
  289. int ret;
  290. ret = i2c_smbus_read_byte_data(client, MAG3110_WHO_AM_I);
  291. if (ret < 0)
  292. return ret;
  293. if (ret != MAG3110_DEVICE_ID)
  294. return -ENODEV;
  295. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  296. if (!indio_dev)
  297. return -ENOMEM;
  298. data = iio_priv(indio_dev);
  299. data->client = client;
  300. mutex_init(&data->lock);
  301. i2c_set_clientdata(client, indio_dev);
  302. indio_dev->info = &mag3110_info;
  303. indio_dev->name = id->name;
  304. indio_dev->dev.parent = &client->dev;
  305. indio_dev->modes = INDIO_DIRECT_MODE;
  306. indio_dev->channels = mag3110_channels;
  307. indio_dev->num_channels = ARRAY_SIZE(mag3110_channels);
  308. indio_dev->available_scan_masks = mag3110_scan_masks;
  309. data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT << MAG3110_CTRL_DR_SHIFT;
  310. ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG1,
  311. data->ctrl_reg1);
  312. if (ret < 0)
  313. return ret;
  314. ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2,
  315. MAG3110_CTRL_AUTO_MRST_EN);
  316. if (ret < 0)
  317. goto standby_on_error;
  318. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  319. mag3110_trigger_handler, NULL);
  320. if (ret < 0)
  321. goto standby_on_error;
  322. ret = iio_device_register(indio_dev);
  323. if (ret < 0)
  324. goto buffer_cleanup;
  325. return 0;
  326. buffer_cleanup:
  327. iio_triggered_buffer_cleanup(indio_dev);
  328. standby_on_error:
  329. mag3110_standby(iio_priv(indio_dev));
  330. return ret;
  331. }
  332. static int mag3110_remove(struct i2c_client *client)
  333. {
  334. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  335. iio_device_unregister(indio_dev);
  336. iio_triggered_buffer_cleanup(indio_dev);
  337. mag3110_standby(iio_priv(indio_dev));
  338. return 0;
  339. }
  340. #ifdef CONFIG_PM_SLEEP
  341. static int mag3110_suspend(struct device *dev)
  342. {
  343. return mag3110_standby(iio_priv(i2c_get_clientdata(
  344. to_i2c_client(dev))));
  345. }
  346. static int mag3110_resume(struct device *dev)
  347. {
  348. struct mag3110_data *data = iio_priv(i2c_get_clientdata(
  349. to_i2c_client(dev)));
  350. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  351. data->ctrl_reg1);
  352. }
  353. static SIMPLE_DEV_PM_OPS(mag3110_pm_ops, mag3110_suspend, mag3110_resume);
  354. #define MAG3110_PM_OPS (&mag3110_pm_ops)
  355. #else
  356. #define MAG3110_PM_OPS NULL
  357. #endif
  358. static const struct i2c_device_id mag3110_id[] = {
  359. { "mag3110", 0 },
  360. { }
  361. };
  362. MODULE_DEVICE_TABLE(i2c, mag3110_id);
  363. static struct i2c_driver mag3110_driver = {
  364. .driver = {
  365. .name = "mag3110",
  366. .pm = MAG3110_PM_OPS,
  367. },
  368. .probe = mag3110_probe,
  369. .remove = mag3110_remove,
  370. .id_table = mag3110_id,
  371. };
  372. module_i2c_driver(mag3110_driver);
  373. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  374. MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
  375. MODULE_LICENSE("GPL");