mag3110.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * mag3110.c - Support for Freescale MAG3110 magnetometer sensor
  3. *
  4. * Copyright (c) 2013 Peter Meerwald <[email protected]>
  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. ret = iio_device_claim_direct_mode(indio_dev);
  130. if (ret)
  131. return ret;
  132. switch (chan->type) {
  133. case IIO_MAGN: /* in 0.1 uT / LSB */
  134. ret = mag3110_read(data, buffer);
  135. if (ret < 0)
  136. goto release;
  137. *val = sign_extend32(
  138. be16_to_cpu(buffer[chan->scan_index]), 15);
  139. ret = IIO_VAL_INT;
  140. break;
  141. case IIO_TEMP: /* in 1 C / LSB */
  142. mutex_lock(&data->lock);
  143. ret = mag3110_request(data);
  144. if (ret < 0) {
  145. mutex_unlock(&data->lock);
  146. goto release;
  147. }
  148. ret = i2c_smbus_read_byte_data(data->client,
  149. MAG3110_DIE_TEMP);
  150. mutex_unlock(&data->lock);
  151. if (ret < 0)
  152. goto release;
  153. *val = sign_extend32(ret, 7);
  154. ret = IIO_VAL_INT;
  155. break;
  156. default:
  157. ret = -EINVAL;
  158. }
  159. release:
  160. iio_device_release_direct_mode(indio_dev);
  161. return ret;
  162. case IIO_CHAN_INFO_SCALE:
  163. switch (chan->type) {
  164. case IIO_MAGN:
  165. *val = 0;
  166. *val2 = 1000;
  167. return IIO_VAL_INT_PLUS_MICRO;
  168. case IIO_TEMP:
  169. *val = 1000;
  170. return IIO_VAL_INT;
  171. default:
  172. return -EINVAL;
  173. }
  174. case IIO_CHAN_INFO_SAMP_FREQ:
  175. i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
  176. *val = mag3110_samp_freq[i][0];
  177. *val2 = mag3110_samp_freq[i][1];
  178. return IIO_VAL_INT_PLUS_MICRO;
  179. case IIO_CHAN_INFO_CALIBBIAS:
  180. ret = i2c_smbus_read_word_swapped(data->client,
  181. MAG3110_OFF_X + 2 * chan->scan_index);
  182. if (ret < 0)
  183. return ret;
  184. *val = sign_extend32(ret >> 1, 14);
  185. return IIO_VAL_INT;
  186. }
  187. return -EINVAL;
  188. }
  189. static int mag3110_write_raw(struct iio_dev *indio_dev,
  190. struct iio_chan_spec const *chan,
  191. int val, int val2, long mask)
  192. {
  193. struct mag3110_data *data = iio_priv(indio_dev);
  194. int rate, ret;
  195. ret = iio_device_claim_direct_mode(indio_dev);
  196. if (ret)
  197. return ret;
  198. switch (mask) {
  199. case IIO_CHAN_INFO_SAMP_FREQ:
  200. rate = mag3110_get_samp_freq_index(data, val, val2);
  201. if (rate < 0) {
  202. ret = -EINVAL;
  203. break;
  204. }
  205. data->ctrl_reg1 &= ~MAG3110_CTRL_DR_MASK;
  206. data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
  207. ret = i2c_smbus_write_byte_data(data->client,
  208. MAG3110_CTRL_REG1, data->ctrl_reg1);
  209. break;
  210. case IIO_CHAN_INFO_CALIBBIAS:
  211. if (val < -10000 || val > 10000) {
  212. ret = -EINVAL;
  213. break;
  214. }
  215. ret = i2c_smbus_write_word_swapped(data->client,
  216. MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
  217. break;
  218. default:
  219. ret = -EINVAL;
  220. break;
  221. }
  222. iio_device_release_direct_mode(indio_dev);
  223. return ret;
  224. }
  225. static irqreturn_t mag3110_trigger_handler(int irq, void *p)
  226. {
  227. struct iio_poll_func *pf = p;
  228. struct iio_dev *indio_dev = pf->indio_dev;
  229. struct mag3110_data *data = iio_priv(indio_dev);
  230. u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */
  231. int ret;
  232. ret = mag3110_read(data, (__be16 *) buffer);
  233. if (ret < 0)
  234. goto done;
  235. if (test_bit(3, indio_dev->active_scan_mask)) {
  236. ret = i2c_smbus_read_byte_data(data->client,
  237. MAG3110_DIE_TEMP);
  238. if (ret < 0)
  239. goto done;
  240. buffer[6] = ret;
  241. }
  242. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  243. iio_get_time_ns(indio_dev));
  244. done:
  245. iio_trigger_notify_done(indio_dev->trig);
  246. return IRQ_HANDLED;
  247. }
  248. #define MAG3110_CHANNEL(axis, idx) { \
  249. .type = IIO_MAGN, \
  250. .modified = 1, \
  251. .channel2 = IIO_MOD_##axis, \
  252. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  253. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  254. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  255. BIT(IIO_CHAN_INFO_SCALE), \
  256. .scan_index = idx, \
  257. .scan_type = { \
  258. .sign = 's', \
  259. .realbits = 16, \
  260. .storagebits = 16, \
  261. .endianness = IIO_BE, \
  262. }, \
  263. }
  264. static const struct iio_chan_spec mag3110_channels[] = {
  265. MAG3110_CHANNEL(X, 0),
  266. MAG3110_CHANNEL(Y, 1),
  267. MAG3110_CHANNEL(Z, 2),
  268. {
  269. .type = IIO_TEMP,
  270. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  271. BIT(IIO_CHAN_INFO_SCALE),
  272. .scan_index = 3,
  273. .scan_type = {
  274. .sign = 's',
  275. .realbits = 8,
  276. .storagebits = 8,
  277. },
  278. },
  279. IIO_CHAN_SOFT_TIMESTAMP(4),
  280. };
  281. static struct attribute *mag3110_attributes[] = {
  282. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  283. NULL
  284. };
  285. static const struct attribute_group mag3110_group = {
  286. .attrs = mag3110_attributes,
  287. };
  288. static const struct iio_info mag3110_info = {
  289. .attrs = &mag3110_group,
  290. .read_raw = &mag3110_read_raw,
  291. .write_raw = &mag3110_write_raw,
  292. .driver_module = THIS_MODULE,
  293. };
  294. static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
  295. static int mag3110_standby(struct mag3110_data *data)
  296. {
  297. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  298. data->ctrl_reg1 & ~MAG3110_CTRL_AC);
  299. }
  300. static int mag3110_probe(struct i2c_client *client,
  301. const struct i2c_device_id *id)
  302. {
  303. struct mag3110_data *data;
  304. struct iio_dev *indio_dev;
  305. int ret;
  306. ret = i2c_smbus_read_byte_data(client, MAG3110_WHO_AM_I);
  307. if (ret < 0)
  308. return ret;
  309. if (ret != MAG3110_DEVICE_ID)
  310. return -ENODEV;
  311. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  312. if (!indio_dev)
  313. return -ENOMEM;
  314. data = iio_priv(indio_dev);
  315. data->client = client;
  316. mutex_init(&data->lock);
  317. i2c_set_clientdata(client, indio_dev);
  318. indio_dev->info = &mag3110_info;
  319. indio_dev->name = id->name;
  320. indio_dev->dev.parent = &client->dev;
  321. indio_dev->modes = INDIO_DIRECT_MODE;
  322. indio_dev->channels = mag3110_channels;
  323. indio_dev->num_channels = ARRAY_SIZE(mag3110_channels);
  324. indio_dev->available_scan_masks = mag3110_scan_masks;
  325. data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT << MAG3110_CTRL_DR_SHIFT;
  326. ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG1,
  327. data->ctrl_reg1);
  328. if (ret < 0)
  329. return ret;
  330. ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2,
  331. MAG3110_CTRL_AUTO_MRST_EN);
  332. if (ret < 0)
  333. goto standby_on_error;
  334. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  335. mag3110_trigger_handler, NULL);
  336. if (ret < 0)
  337. goto standby_on_error;
  338. ret = iio_device_register(indio_dev);
  339. if (ret < 0)
  340. goto buffer_cleanup;
  341. return 0;
  342. buffer_cleanup:
  343. iio_triggered_buffer_cleanup(indio_dev);
  344. standby_on_error:
  345. mag3110_standby(iio_priv(indio_dev));
  346. return ret;
  347. }
  348. static int mag3110_remove(struct i2c_client *client)
  349. {
  350. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  351. iio_device_unregister(indio_dev);
  352. iio_triggered_buffer_cleanup(indio_dev);
  353. mag3110_standby(iio_priv(indio_dev));
  354. return 0;
  355. }
  356. #ifdef CONFIG_PM_SLEEP
  357. static int mag3110_suspend(struct device *dev)
  358. {
  359. return mag3110_standby(iio_priv(i2c_get_clientdata(
  360. to_i2c_client(dev))));
  361. }
  362. static int mag3110_resume(struct device *dev)
  363. {
  364. struct mag3110_data *data = iio_priv(i2c_get_clientdata(
  365. to_i2c_client(dev)));
  366. return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
  367. data->ctrl_reg1);
  368. }
  369. static SIMPLE_DEV_PM_OPS(mag3110_pm_ops, mag3110_suspend, mag3110_resume);
  370. #define MAG3110_PM_OPS (&mag3110_pm_ops)
  371. #else
  372. #define MAG3110_PM_OPS NULL
  373. #endif
  374. static const struct i2c_device_id mag3110_id[] = {
  375. { "mag3110", 0 },
  376. { }
  377. };
  378. MODULE_DEVICE_TABLE(i2c, mag3110_id);
  379. static struct i2c_driver mag3110_driver = {
  380. .driver = {
  381. .name = "mag3110",
  382. .pm = MAG3110_PM_OPS,
  383. },
  384. .probe = mag3110_probe,
  385. .remove = mag3110_remove,
  386. .id_table = mag3110_id,
  387. };
  388. module_i2c_driver(mag3110_driver);
  389. MODULE_AUTHOR("Peter Meerwald <[email protected]>");
  390. MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
  391. MODULE_LICENSE("GPL");