drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / magnetometer / mag3110.c
blob5295dc0100e4c12ba7dcf635fb09fa4e8478aebf
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * mag3110.c - Support for Freescale MAG3110 magnetometer sensor
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x0e)
9 * TODO: irq, user offset, oversampling, continuous mode
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/delay.h>
20 #include <linux/regulator/consumer.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
35 #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
37 #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5))
38 #define MAG3110_CTRL_DR_SHIFT 5
39 #define MAG3110_CTRL_DR_DEFAULT 0
41 #define MAG3110_SYSMOD_MODE_MASK GENMASK(1, 0)
43 #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
44 #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
46 #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */
47 #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */
49 #define MAG3110_DEVICE_ID 0xc4
51 /* Each client has this additional data */
52 struct mag3110_data {
53 struct i2c_client *client;
54 struct mutex lock;
55 u8 ctrl_reg1;
56 int sleep_val;
57 struct regulator *vdd_reg;
58 struct regulator *vddio_reg;
59 /* Ensure natural alignment of timestamp */
60 struct {
61 __be16 channels[3];
62 u8 temperature;
63 s64 ts __aligned(8);
64 } scan;
67 static int mag3110_request(struct mag3110_data *data)
69 int ret, tries = 150;
71 if ((data->ctrl_reg1 & MAG3110_CTRL_AC) == 0) {
72 /* trigger measurement */
73 ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
74 data->ctrl_reg1 | MAG3110_CTRL_TM);
75 if (ret < 0)
76 return ret;
79 while (tries-- > 0) {
80 ret = i2c_smbus_read_byte_data(data->client, MAG3110_STATUS);
81 if (ret < 0)
82 return ret;
83 /* wait for data ready */
84 if ((ret & MAG3110_STATUS_DRDY) == MAG3110_STATUS_DRDY)
85 break;
87 if (data->sleep_val <= 20)
88 usleep_range(data->sleep_val * 250, data->sleep_val * 500);
89 else
90 msleep(20);
93 if (tries < 0) {
94 dev_err(&data->client->dev, "data not ready\n");
95 return -EIO;
98 return 0;
101 static int mag3110_read(struct mag3110_data *data, __be16 buf[3])
103 int ret;
105 mutex_lock(&data->lock);
106 ret = mag3110_request(data);
107 if (ret < 0) {
108 mutex_unlock(&data->lock);
109 return ret;
111 ret = i2c_smbus_read_i2c_block_data(data->client,
112 MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
113 mutex_unlock(&data->lock);
115 return ret;
118 static ssize_t mag3110_show_int_plus_micros(char *buf,
119 const int (*vals)[2], int n)
121 size_t len = 0;
123 while (n-- > 0)
124 len += scnprintf(buf + len, PAGE_SIZE - len,
125 "%d.%06d ", vals[n][0], vals[n][1]);
127 /* replace trailing space by newline */
128 buf[len - 1] = '\n';
130 return len;
133 static int mag3110_get_int_plus_micros_index(const int (*vals)[2], int n,
134 int val, int val2)
136 while (n-- > 0)
137 if (val == vals[n][0] && val2 == vals[n][1])
138 return n;
140 return -EINVAL;
143 static const int mag3110_samp_freq[8][2] = {
144 {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000},
145 {1, 250000}, {0, 625000}
148 static ssize_t mag3110_show_samp_freq_avail(struct device *dev,
149 struct device_attribute *attr, char *buf)
151 return mag3110_show_int_plus_micros(buf, mag3110_samp_freq, 8);
154 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail);
156 static int mag3110_get_samp_freq_index(struct mag3110_data *data,
157 int val, int val2)
159 return mag3110_get_int_plus_micros_index(mag3110_samp_freq, 8, val,
160 val2);
163 static int mag3110_calculate_sleep(struct mag3110_data *data)
165 int ret, i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
167 if (mag3110_samp_freq[i][0] > 0)
168 ret = 1000 / mag3110_samp_freq[i][0];
169 else
170 ret = 1000;
172 return ret == 0 ? 1 : ret;
175 static int mag3110_standby(struct mag3110_data *data)
177 return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
178 data->ctrl_reg1 & ~MAG3110_CTRL_AC);
181 static int mag3110_wait_standby(struct mag3110_data *data)
183 int ret, tries = 30;
186 * Takes up to 1/ODR to come out of active mode into stby
187 * Longest expected period is 12.5seconds.
188 * We'll sleep for 500ms between checks
190 while (tries-- > 0) {
191 ret = i2c_smbus_read_byte_data(data->client, MAG3110_SYSMOD);
192 if (ret < 0) {
193 dev_err(&data->client->dev, "i2c error\n");
194 return ret;
196 /* wait for standby */
197 if ((ret & MAG3110_SYSMOD_MODE_MASK) == 0)
198 break;
200 msleep_interruptible(500);
203 if (tries < 0) {
204 dev_err(&data->client->dev, "device not entering standby mode\n");
205 return -EIO;
208 return 0;
211 static int mag3110_active(struct mag3110_data *data)
213 return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
214 data->ctrl_reg1);
217 /* returns >0 if active, 0 if in standby and <0 on error */
218 static int mag3110_is_active(struct mag3110_data *data)
220 int reg;
222 reg = i2c_smbus_read_byte_data(data->client, MAG3110_CTRL_REG1);
223 if (reg < 0)
224 return reg;
226 return reg & MAG3110_CTRL_AC;
229 static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
231 int ret;
232 int is_active;
234 mutex_lock(&data->lock);
236 is_active = mag3110_is_active(data);
237 if (is_active < 0) {
238 ret = is_active;
239 goto fail;
242 /* config can only be changed when in standby */
243 if (is_active > 0) {
244 ret = mag3110_standby(data);
245 if (ret < 0)
246 goto fail;
250 * After coming out of active we must wait for the part
251 * to transition to STBY. This can take up to 1 /ODR to occur
253 ret = mag3110_wait_standby(data);
254 if (ret < 0)
255 goto fail;
257 ret = i2c_smbus_write_byte_data(data->client, reg, val);
258 if (ret < 0)
259 goto fail;
261 if (is_active > 0) {
262 ret = mag3110_active(data);
263 if (ret < 0)
264 goto fail;
267 ret = 0;
268 fail:
269 mutex_unlock(&data->lock);
271 return ret;
274 static int mag3110_read_raw(struct iio_dev *indio_dev,
275 struct iio_chan_spec const *chan,
276 int *val, int *val2, long mask)
278 struct mag3110_data *data = iio_priv(indio_dev);
279 __be16 buffer[3];
280 int i, ret;
282 switch (mask) {
283 case IIO_CHAN_INFO_RAW:
284 ret = iio_device_claim_direct_mode(indio_dev);
285 if (ret)
286 return ret;
288 switch (chan->type) {
289 case IIO_MAGN: /* in 0.1 uT / LSB */
290 ret = mag3110_read(data, buffer);
291 if (ret < 0)
292 goto release;
293 *val = sign_extend32(
294 be16_to_cpu(buffer[chan->scan_index]),
295 chan->scan_type.realbits - 1);
296 ret = IIO_VAL_INT;
297 break;
298 case IIO_TEMP: /* in 1 C / LSB */
299 mutex_lock(&data->lock);
300 ret = mag3110_request(data);
301 if (ret < 0) {
302 mutex_unlock(&data->lock);
303 goto release;
305 ret = i2c_smbus_read_byte_data(data->client,
306 MAG3110_DIE_TEMP);
307 mutex_unlock(&data->lock);
308 if (ret < 0)
309 goto release;
310 *val = sign_extend32(ret,
311 chan->scan_type.realbits - 1);
312 ret = IIO_VAL_INT;
313 break;
314 default:
315 ret = -EINVAL;
317 release:
318 iio_device_release_direct_mode(indio_dev);
319 return ret;
321 case IIO_CHAN_INFO_SCALE:
322 switch (chan->type) {
323 case IIO_MAGN:
324 *val = 0;
325 *val2 = 1000;
326 return IIO_VAL_INT_PLUS_MICRO;
327 case IIO_TEMP:
328 *val = 1000;
329 return IIO_VAL_INT;
330 default:
331 return -EINVAL;
333 case IIO_CHAN_INFO_SAMP_FREQ:
334 i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT;
335 *val = mag3110_samp_freq[i][0];
336 *val2 = mag3110_samp_freq[i][1];
337 return IIO_VAL_INT_PLUS_MICRO;
338 case IIO_CHAN_INFO_CALIBBIAS:
339 ret = i2c_smbus_read_word_swapped(data->client,
340 MAG3110_OFF_X + 2 * chan->scan_index);
341 if (ret < 0)
342 return ret;
343 *val = sign_extend32(ret >> 1, 14);
344 return IIO_VAL_INT;
346 return -EINVAL;
349 static int mag3110_write_raw(struct iio_dev *indio_dev,
350 struct iio_chan_spec const *chan,
351 int val, int val2, long mask)
353 struct mag3110_data *data = iio_priv(indio_dev);
354 int rate, ret;
356 ret = iio_device_claim_direct_mode(indio_dev);
357 if (ret)
358 return ret;
360 switch (mask) {
361 case IIO_CHAN_INFO_SAMP_FREQ:
362 rate = mag3110_get_samp_freq_index(data, val, val2);
363 if (rate < 0) {
364 ret = -EINVAL;
365 break;
367 data->ctrl_reg1 &= 0xff & ~MAG3110_CTRL_DR_MASK
368 & ~MAG3110_CTRL_AC;
369 data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
370 data->sleep_val = mag3110_calculate_sleep(data);
371 if (data->sleep_val < 40)
372 data->ctrl_reg1 |= MAG3110_CTRL_AC;
374 ret = mag3110_change_config(data, MAG3110_CTRL_REG1,
375 data->ctrl_reg1);
376 break;
377 case IIO_CHAN_INFO_CALIBBIAS:
378 if (val < -10000 || val > 10000) {
379 ret = -EINVAL;
380 break;
382 ret = i2c_smbus_write_word_swapped(data->client,
383 MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
384 break;
385 default:
386 ret = -EINVAL;
387 break;
389 iio_device_release_direct_mode(indio_dev);
390 return ret;
393 static irqreturn_t mag3110_trigger_handler(int irq, void *p)
395 struct iio_poll_func *pf = p;
396 struct iio_dev *indio_dev = pf->indio_dev;
397 struct mag3110_data *data = iio_priv(indio_dev);
398 int ret;
400 ret = mag3110_read(data, data->scan.channels);
401 if (ret < 0)
402 goto done;
404 if (test_bit(3, indio_dev->active_scan_mask)) {
405 ret = i2c_smbus_read_byte_data(data->client,
406 MAG3110_DIE_TEMP);
407 if (ret < 0)
408 goto done;
409 data->scan.temperature = ret;
412 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
413 iio_get_time_ns(indio_dev));
415 done:
416 iio_trigger_notify_done(indio_dev->trig);
417 return IRQ_HANDLED;
420 #define MAG3110_CHANNEL(axis, idx) { \
421 .type = IIO_MAGN, \
422 .modified = 1, \
423 .channel2 = IIO_MOD_##axis, \
424 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
425 BIT(IIO_CHAN_INFO_CALIBBIAS), \
426 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
427 BIT(IIO_CHAN_INFO_SCALE), \
428 .scan_index = idx, \
429 .scan_type = { \
430 .sign = 's', \
431 .realbits = 16, \
432 .storagebits = 16, \
433 .endianness = IIO_BE, \
434 }, \
437 static const struct iio_chan_spec mag3110_channels[] = {
438 MAG3110_CHANNEL(X, 0),
439 MAG3110_CHANNEL(Y, 1),
440 MAG3110_CHANNEL(Z, 2),
442 .type = IIO_TEMP,
443 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
444 BIT(IIO_CHAN_INFO_SCALE),
445 .scan_index = 3,
446 .scan_type = {
447 .sign = 's',
448 .realbits = 8,
449 .storagebits = 8,
452 IIO_CHAN_SOFT_TIMESTAMP(4),
455 static struct attribute *mag3110_attributes[] = {
456 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
457 NULL
460 static const struct attribute_group mag3110_group = {
461 .attrs = mag3110_attributes,
464 static const struct iio_info mag3110_info = {
465 .attrs = &mag3110_group,
466 .read_raw = &mag3110_read_raw,
467 .write_raw = &mag3110_write_raw,
470 static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
472 static int mag3110_probe(struct i2c_client *client)
474 const struct i2c_device_id *id = i2c_client_get_device_id(client);
475 struct mag3110_data *data;
476 struct iio_dev *indio_dev;
477 int ret;
479 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
480 if (!indio_dev)
481 return -ENOMEM;
483 data = iio_priv(indio_dev);
485 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
486 if (IS_ERR(data->vdd_reg))
487 return dev_err_probe(&client->dev, PTR_ERR(data->vdd_reg),
488 "failed to get VDD regulator!\n");
490 data->vddio_reg = devm_regulator_get(&client->dev, "vddio");
491 if (IS_ERR(data->vddio_reg))
492 return dev_err_probe(&client->dev, PTR_ERR(data->vddio_reg),
493 "failed to get VDDIO regulator!\n");
495 ret = regulator_enable(data->vdd_reg);
496 if (ret) {
497 dev_err(&client->dev, "failed to enable VDD regulator!\n");
498 return ret;
501 ret = regulator_enable(data->vddio_reg);
502 if (ret) {
503 dev_err(&client->dev, "failed to enable VDDIO regulator!\n");
504 goto disable_regulator_vdd;
507 ret = i2c_smbus_read_byte_data(client, MAG3110_WHO_AM_I);
508 if (ret < 0)
509 goto disable_regulators;
510 if (ret != MAG3110_DEVICE_ID) {
511 ret = -ENODEV;
512 goto disable_regulators;
515 data->client = client;
516 mutex_init(&data->lock);
518 i2c_set_clientdata(client, indio_dev);
519 indio_dev->info = &mag3110_info;
520 indio_dev->name = id->name;
521 indio_dev->modes = INDIO_DIRECT_MODE;
522 indio_dev->channels = mag3110_channels;
523 indio_dev->num_channels = ARRAY_SIZE(mag3110_channels);
524 indio_dev->available_scan_masks = mag3110_scan_masks;
526 data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT << MAG3110_CTRL_DR_SHIFT;
527 data->sleep_val = mag3110_calculate_sleep(data);
528 if (data->sleep_val < 40)
529 data->ctrl_reg1 |= MAG3110_CTRL_AC;
531 ret = mag3110_change_config(data, MAG3110_CTRL_REG1, data->ctrl_reg1);
532 if (ret < 0)
533 goto disable_regulators;
535 ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2,
536 MAG3110_CTRL_AUTO_MRST_EN);
537 if (ret < 0)
538 goto standby_on_error;
540 ret = iio_triggered_buffer_setup(indio_dev, NULL,
541 mag3110_trigger_handler, NULL);
542 if (ret < 0)
543 goto standby_on_error;
545 ret = iio_device_register(indio_dev);
546 if (ret < 0)
547 goto buffer_cleanup;
548 return 0;
550 buffer_cleanup:
551 iio_triggered_buffer_cleanup(indio_dev);
552 standby_on_error:
553 mag3110_standby(iio_priv(indio_dev));
554 disable_regulators:
555 regulator_disable(data->vddio_reg);
556 disable_regulator_vdd:
557 regulator_disable(data->vdd_reg);
559 return ret;
562 static void mag3110_remove(struct i2c_client *client)
564 struct iio_dev *indio_dev = i2c_get_clientdata(client);
565 struct mag3110_data *data = iio_priv(indio_dev);
567 iio_device_unregister(indio_dev);
568 iio_triggered_buffer_cleanup(indio_dev);
569 mag3110_standby(iio_priv(indio_dev));
570 regulator_disable(data->vddio_reg);
571 regulator_disable(data->vdd_reg);
574 static int mag3110_suspend(struct device *dev)
576 struct mag3110_data *data = iio_priv(i2c_get_clientdata(
577 to_i2c_client(dev)));
578 int ret;
580 ret = mag3110_standby(iio_priv(i2c_get_clientdata(
581 to_i2c_client(dev))));
582 if (ret)
583 return ret;
585 ret = regulator_disable(data->vddio_reg);
586 if (ret) {
587 dev_err(dev, "failed to disable VDDIO regulator\n");
588 return ret;
591 ret = regulator_disable(data->vdd_reg);
592 if (ret) {
593 dev_err(dev, "failed to disable VDD regulator\n");
594 return ret;
597 return 0;
600 static int mag3110_resume(struct device *dev)
602 struct mag3110_data *data = iio_priv(i2c_get_clientdata(
603 to_i2c_client(dev)));
604 int ret;
606 ret = regulator_enable(data->vdd_reg);
607 if (ret) {
608 dev_err(dev, "failed to enable VDD regulator\n");
609 return ret;
612 ret = regulator_enable(data->vddio_reg);
613 if (ret) {
614 dev_err(dev, "failed to enable VDDIO regulator\n");
615 regulator_disable(data->vdd_reg);
616 return ret;
619 return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
620 data->ctrl_reg1);
623 static DEFINE_SIMPLE_DEV_PM_OPS(mag3110_pm_ops, mag3110_suspend,
624 mag3110_resume);
626 static const struct i2c_device_id mag3110_id[] = {
627 { "mag3110" },
630 MODULE_DEVICE_TABLE(i2c, mag3110_id);
632 static const struct of_device_id mag3110_of_match[] = {
633 { .compatible = "fsl,mag3110" },
636 MODULE_DEVICE_TABLE(of, mag3110_of_match);
638 static struct i2c_driver mag3110_driver = {
639 .driver = {
640 .name = "mag3110",
641 .of_match_table = mag3110_of_match,
642 .pm = pm_sleep_ptr(&mag3110_pm_ops),
644 .probe = mag3110_probe,
645 .remove = mag3110_remove,
646 .id_table = mag3110_id,
648 module_i2c_driver(mag3110_driver);
650 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
651 MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
652 MODULE_LICENSE("GPL");