1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Yamaha YAS magnetic sensors, often used in Samsung
4 * mobile phones. While all are not yet handled because of lacking
5 * hardware, expand this driver to handle the different variants:
7 * YAS530 MS-3E (2011 Samsung Galaxy S Advance)
8 * YAS532 MS-3R (2011 Samsung Galaxy S4)
9 * YAS533 MS-3F (Vivo 1633, 1707, V3, Y21L)
10 * (YAS534 is a magnetic switch, not handled)
13 * YAS537 MS-3T (2015 Samsung Galaxy S6, Note 5, Galaxy S7)
14 * YAS539 MS-3S (2018 Samsung Galaxy A7 SM-A750FN)
16 * Code functions found in the MPU3050 YAS530 and YAS532 drivers
17 * named "inv_compass" in the Tegra Android kernel tree.
18 * Copyright (C) 2012 InvenSense Corporation
20 * Code functions for YAS537 based on Yamaha Android kernel driver.
21 * Copyright (c) 2014 Yamaha Corporation
23 * Author: Linus Walleij <linus.walleij@linaro.org>
25 #include <linux/bitfield.h>
26 #include <linux/bitops.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/i2c.h>
31 #include <linux/module.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/mutex.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/property.h>
36 #include <linux/regmap.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/random.h>
39 #include <linux/units.h>
41 #include <linux/iio/buffer.h>
42 #include <linux/iio/iio.h>
43 #include <linux/iio/trigger_consumer.h>
44 #include <linux/iio/triggered_buffer.h>
46 #include <linux/unaligned.h>
48 /* Commonly used registers */
49 #define YAS5XX_DEVICE_ID 0x80
50 #define YAS5XX_MEASURE_DATA 0xB0
52 /* These registers are used by YAS530, YAS532 and YAS533 */
53 #define YAS530_ACTUATE_INIT_COIL 0x81
54 #define YAS530_MEASURE 0x82
55 #define YAS530_CONFIG 0x83
56 #define YAS530_MEASURE_INTERVAL 0x84
57 #define YAS530_OFFSET_X 0x85 /* [-31 .. 31] */
58 #define YAS530_OFFSET_Y1 0x86 /* [-31 .. 31] */
59 #define YAS530_OFFSET_Y2 0x87 /* [-31 .. 31] */
60 #define YAS530_TEST1 0x88
61 #define YAS530_TEST2 0x89
62 #define YAS530_CAL 0x90
64 /* Registers used by YAS537 */
65 #define YAS537_MEASURE 0x81 /* Originally YAS537_REG_CMDR */
66 #define YAS537_CONFIG 0x82 /* Originally YAS537_REG_CONFR */
67 #define YAS537_MEASURE_INTERVAL 0x83 /* Originally YAS537_REG_INTRVLR */
68 #define YAS537_OFFSET_X 0x84 /* Originally YAS537_REG_OXR */
69 #define YAS537_OFFSET_Y1 0x85 /* Originally YAS537_REG_OY1R */
70 #define YAS537_OFFSET_Y2 0x86 /* Originally YAS537_REG_OY2R */
71 #define YAS537_AVR 0x87
72 #define YAS537_HCK 0x88
73 #define YAS537_LCK 0x89
74 #define YAS537_SRST 0x90
75 #define YAS537_ADCCAL 0x91
76 #define YAS537_MTC 0x93
77 #define YAS537_OC 0x9E
78 #define YAS537_TRM 0x9F
79 #define YAS537_CAL 0xC0
81 /* Bits in the YAS5xx config register */
82 #define YAS5XX_CONFIG_INTON BIT(0) /* Interrupt on? */
83 #define YAS5XX_CONFIG_INTHACT BIT(1) /* Interrupt active high? */
84 #define YAS5XX_CONFIG_CCK_MASK GENMASK(4, 2)
85 #define YAS5XX_CONFIG_CCK_SHIFT 2
87 /* Bits in the measure command register */
88 #define YAS5XX_MEASURE_START BIT(0)
89 #define YAS5XX_MEASURE_LDTC BIT(1)
90 #define YAS5XX_MEASURE_FORS BIT(2)
91 #define YAS5XX_MEASURE_DLYMES BIT(4)
92 #define YAS5XX_MEASURE_CONT BIT(5)
94 /* Bits in the measure data register */
95 #define YAS5XX_MEASURE_DATA_BUSY BIT(7)
97 #define YAS530_DEVICE_ID 0x01 /* YAS530 (MS-3E) */
98 #define YAS530_VERSION_A 0 /* YAS530 (MS-3E A) */
99 #define YAS530_VERSION_B 1 /* YAS530B (MS-3E B) */
100 #define YAS530_VERSION_A_COEF 380
101 #define YAS530_VERSION_B_COEF 550
102 #define YAS530_DATA_BITS 12
103 #define YAS530_DATA_CENTER BIT(YAS530_DATA_BITS - 1)
104 #define YAS530_DATA_OVERFLOW (BIT(YAS530_DATA_BITS) - 1)
106 #define YAS532_DEVICE_ID 0x02 /* YAS532/YAS533 (MS-3R/F) */
107 #define YAS532_VERSION_AB 0 /* YAS532/533 AB (MS-3R/F AB) */
108 #define YAS532_VERSION_AC 1 /* YAS532/533 AC (MS-3R/F AC) */
109 #define YAS532_VERSION_AB_COEF 1800
110 #define YAS532_VERSION_AC_COEF_X 850
111 #define YAS532_VERSION_AC_COEF_Y1 750
112 #define YAS532_VERSION_AC_COEF_Y2 750
113 #define YAS532_DATA_BITS 13
114 #define YAS532_DATA_CENTER BIT(YAS532_DATA_BITS - 1)
115 #define YAS532_DATA_OVERFLOW (BIT(YAS532_DATA_BITS) - 1)
117 #define YAS537_DEVICE_ID 0x07 /* YAS537 (MS-3T) */
118 #define YAS537_VERSION_0 0 /* Version naming unknown */
119 #define YAS537_VERSION_1 1 /* Version naming unknown */
120 #define YAS537_MAG_AVERAGE_32_MASK GENMASK(6, 4)
121 #define YAS537_MEASURE_TIME_WORST_US 1500
122 #define YAS537_DEFAULT_SENSOR_DELAY_MS 50
123 #define YAS537_MAG_RCOIL_TIME_US 65
124 #define YAS537_MTC3_MASK_PREP GENMASK(7, 0)
125 #define YAS537_MTC3_MASK_GET GENMASK(7, 5)
126 #define YAS537_MTC3_ADD_BIT BIT(4)
127 #define YAS537_HCK_MASK_PREP GENMASK(4, 0)
128 #define YAS537_HCK_MASK_GET GENMASK(7, 4)
129 #define YAS537_LCK_MASK_PREP GENMASK(4, 0)
130 #define YAS537_LCK_MASK_GET GENMASK(3, 0)
131 #define YAS537_OC_MASK_GET GENMASK(5, 0)
133 /* Turn off device regulators etc after 5 seconds of inactivity */
134 #define YAS5XX_AUTOSUSPEND_DELAY_MS 5000
143 static const int yas530_volatile_reg
[] = {
144 YAS530_ACTUATE_INIT_COIL
,
148 static const int yas537_volatile_reg
[] = {
152 struct yas5xx_calibration
{
153 /* Linearization calibration x, y1, y2 */
156 /* Temperature compensation calibration */
158 /* Misc calibration coefficients */
159 s8 a2
, a3
, a4
, a6
, a7
, a8
;
169 * struct yas5xx_chip_info - device-specific data and function pointers
170 * @devid: device ID number
171 * @product_name: product name of the YAS variant
172 * @version_names: version letters or namings
173 * @volatile_reg: device-specific volatile registers
174 * @volatile_reg_qty: quantity of device-specific volatile registers
175 * @scaling_val2: scaling value for IIO_CHAN_INFO_SCALE
176 * @t_ref: number of counts at reference temperature 20 °C
177 * @min_temp_x10: starting point of temperature counting in 1/10:s degrees Celsius
178 * @get_measure: function pointer to get a measurement
179 * @get_calibration_data: function pointer to get calibration data
180 * @dump_calibration: function pointer to dump calibration for debugging
181 * @measure_offsets: function pointer to measure the offsets
182 * @power_on: function pointer to power-on procedure
184 * The "t_ref" value for YAS532/533 is known from the Android driver.
185 * For YAS530 and YAS537 it was approximately measured.
187 * The temperatures "min_temp_x10" are derived from the temperature resolutions
188 * given in the data sheets.
190 struct yas5xx_chip_info
{
192 const char *product_name
;
193 const char *version_names
[2];
194 const int *volatile_reg
;
195 int volatile_reg_qty
;
199 int (*get_measure
)(struct yas5xx
*yas5xx
, s32
*to
, s32
*xo
, s32
*yo
, s32
*zo
);
200 int (*get_calibration_data
)(struct yas5xx
*yas5xx
);
201 void (*dump_calibration
)(struct yas5xx
*yas5xx
);
202 int (*measure_offsets
)(struct yas5xx
*yas5xx
);
203 int (*power_on
)(struct yas5xx
*yas5xx
);
207 * struct yas5xx - state container for the YAS5xx driver
208 * @dev: parent device pointer
209 * @chip_info: device-specific data and function pointers
210 * @version: device version
211 * @calibration: calibration settings from the OTP storage
212 * @hard_offsets: offsets for each axis measured with initcoil actuated
213 * @orientation: mounting matrix, flipped axis etc
214 * @map: regmap to access the YAX5xx registers over I2C
215 * @regs: the vdd and vddio power regulators
216 * @reset: optional GPIO line used for handling RESET
217 * @lock: locks the magnetometer for exclusive use during a measurement (which
218 * involves several register transactions so the regmap lock is not enough)
219 * so that measurements get serialized in a first-come-first serve manner
220 * @scan: naturally aligned measurements
224 const struct yas5xx_chip_info
*chip_info
;
225 unsigned int version
;
226 struct yas5xx_calibration calibration
;
228 struct iio_mount_matrix orientation
;
230 struct regulator_bulk_data regs
[2];
231 struct gpio_desc
*reset
;
234 * The scanout is 4 x 32 bits in CPU endianness.
235 * Ensure timestamp is naturally aligned
243 /* On YAS530 the x, y1 and y2 values are 12 bits */
244 static u16
yas530_extract_axis(u8
*data
)
249 * These are the bits used in a 16bit word:
250 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
251 * x x x x x x x x x x x x
253 val
= get_unaligned_be16(&data
[0]);
254 val
= FIELD_GET(GENMASK(14, 3), val
);
258 /* On YAS532 the x, y1 and y2 values are 13 bits */
259 static u16
yas532_extract_axis(u8
*data
)
264 * These are the bits used in a 16bit word:
265 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
266 * x x x x x x x x x x x x x
268 val
= get_unaligned_be16(&data
[0]);
269 val
= FIELD_GET(GENMASK(14, 2), val
);
274 * yas530_measure() - Make a measure from the hardware
275 * @yas5xx: The device state
276 * @t: the raw temperature measurement
277 * @x: the raw x axis measurement
278 * @y1: the y1 axis measurement
279 * @y2: the y2 axis measurement
280 * @return: 0 on success or error code
282 * Used by YAS530, YAS532 and YAS533.
284 static int yas530_measure(struct yas5xx
*yas5xx
, u16
*t
, u16
*x
, u16
*y1
, u16
*y2
)
286 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
292 mutex_lock(&yas5xx
->lock
);
293 ret
= regmap_write(yas5xx
->map
, YAS530_MEASURE
, YAS5XX_MEASURE_START
);
298 * Typical time to measure 1500 us, max 2000 us so wait min 500 us
299 * and at most 20000 us (one magnitude more than the datsheet max)
302 ret
= regmap_read_poll_timeout(yas5xx
->map
, YAS5XX_MEASURE_DATA
, busy
,
303 !(busy
& YAS5XX_MEASURE_DATA_BUSY
),
306 dev_err(yas5xx
->dev
, "timeout waiting for measurement\n");
310 ret
= regmap_bulk_read(yas5xx
->map
, YAS5XX_MEASURE_DATA
,
315 mutex_unlock(&yas5xx
->lock
);
318 case YAS530_DEVICE_ID
:
320 * The t value is 9 bits in big endian format
321 * These are the bits used in a 16bit word:
322 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
325 val
= get_unaligned_be16(&data
[0]);
326 val
= FIELD_GET(GENMASK(14, 6), val
);
328 *x
= yas530_extract_axis(&data
[2]);
329 *y1
= yas530_extract_axis(&data
[4]);
330 *y2
= yas530_extract_axis(&data
[6]);
332 case YAS532_DEVICE_ID
:
334 * The t value is 10 bits in big endian format
335 * These are the bits used in a 16bit word:
336 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
337 * x x x x x x x x x x
339 val
= get_unaligned_be16(&data
[0]);
340 val
= FIELD_GET(GENMASK(14, 5), val
);
342 *x
= yas532_extract_axis(&data
[2]);
343 *y1
= yas532_extract_axis(&data
[4]);
344 *y2
= yas532_extract_axis(&data
[6]);
347 dev_err(yas5xx
->dev
, "unknown data format\n");
355 mutex_unlock(&yas5xx
->lock
);
360 * yas537_measure() - Make a measure from the hardware
361 * @yas5xx: The device state
362 * @t: the raw temperature measurement
363 * @x: the raw x axis measurement
364 * @y1: the y1 axis measurement
365 * @y2: the y2 axis measurement
366 * @return: 0 on success or error code
368 static int yas537_measure(struct yas5xx
*yas5xx
, u16
*t
, u16
*x
, u16
*y1
, u16
*y2
)
370 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
377 mutex_lock(&yas5xx
->lock
);
379 /* Contrary to YAS530/532, also a "cont" bit is set, meaning unknown */
380 ret
= regmap_write(yas5xx
->map
, YAS537_MEASURE
, YAS5XX_MEASURE_START
|
381 YAS5XX_MEASURE_CONT
);
385 /* Use same timeout like YAS530/532 but the bit is in data row 2 */
386 ret
= regmap_read_poll_timeout(yas5xx
->map
, YAS5XX_MEASURE_DATA
+ 2, busy
,
387 !(busy
& YAS5XX_MEASURE_DATA_BUSY
),
390 dev_err(yas5xx
->dev
, "timeout waiting for measurement\n");
394 ret
= regmap_bulk_read(yas5xx
->map
, YAS5XX_MEASURE_DATA
,
399 mutex_unlock(&yas5xx
->lock
);
401 *t
= get_unaligned_be16(&data
[0]);
402 xy1y2
[0] = FIELD_GET(GENMASK(13, 0), get_unaligned_be16(&data
[2]));
403 xy1y2
[1] = get_unaligned_be16(&data
[4]);
404 xy1y2
[2] = get_unaligned_be16(&data
[6]);
406 /* The second version of YAS537 needs to include calibration coefficients */
407 if (yas5xx
->version
== YAS537_VERSION_1
) {
408 for (i
= 0; i
< 3; i
++)
409 s
[i
] = xy1y2
[i
] - BIT(13);
410 h
[0] = (c
->k
* (128 * s
[0] + c
->a2
* s
[1] + c
->a3
* s
[2])) / BIT(13);
411 h
[1] = (c
->k
* (c
->a4
* s
[0] + c
->a5
* s
[1] + c
->a6
* s
[2])) / BIT(13);
412 h
[2] = (c
->k
* (c
->a7
* s
[0] + c
->a8
* s
[1] + c
->a9
* s
[2])) / BIT(13);
413 for (i
= 0; i
< 3; i
++) {
414 clamp_val(h
[i
], -BIT(13), BIT(13) - 1);
415 xy1y2
[i
] = h
[i
] + BIT(13);
426 mutex_unlock(&yas5xx
->lock
);
430 /* Used by YAS530, YAS532 and YAS533 */
431 static s32
yas530_linearize(struct yas5xx
*yas5xx
, u16 val
, int axis
)
433 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
434 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
435 static const s32 yas532ac_coef
[] = {
436 YAS532_VERSION_AC_COEF_X
,
437 YAS532_VERSION_AC_COEF_Y1
,
438 YAS532_VERSION_AC_COEF_Y2
,
442 /* Select coefficients */
444 case YAS530_DEVICE_ID
:
445 if (yas5xx
->version
== YAS530_VERSION_A
)
446 coef
= YAS530_VERSION_A_COEF
;
448 coef
= YAS530_VERSION_B_COEF
;
450 case YAS532_DEVICE_ID
:
451 if (yas5xx
->version
== YAS532_VERSION_AB
)
452 coef
= YAS532_VERSION_AB_COEF
;
454 /* Elaborate coefficients */
455 coef
= yas532ac_coef
[axis
];
458 dev_err(yas5xx
->dev
, "unknown device type\n");
462 * Linearization formula:
464 * x' = x - (3721 + 50 * f) + (xoffset - r) * c
466 * Where f and r are calibration values, c is a per-device
467 * and sometimes per-axis coefficient.
469 return val
- (3721 + 50 * c
->f
[axis
]) +
470 (yas5xx
->hard_offsets
[axis
] - c
->r
[axis
]) * coef
;
473 static s32
yas5xx_calc_temperature(struct yas5xx
*yas5xx
, u16 t
)
475 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
482 min_temp_x10
= ci
->min_temp_x10
;
485 to
= (min_temp_x10
+ ((ref_temp_x10
- min_temp_x10
) * t
/ t_ref
)) * 100;
490 * yas530_get_measure() - Measure a sample of all axis and process
491 * @yas5xx: The device state
492 * @to: Temperature out
496 * @return: 0 on success or error code
498 * Used by YAS530, YAS532 and YAS533.
500 static int yas530_get_measure(struct yas5xx
*yas5xx
, s32
*to
, s32
*xo
, s32
*yo
, s32
*zo
)
502 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
503 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
504 u16 t_ref
, t_comp
, t
, x
, y1
, y2
;
505 /* These are signed x, signed y1 etc */
506 s32 sx
, sy1
, sy2
, sy
, sz
;
509 /* We first get raw data that needs to be translated to [x,y,z] */
510 ret
= yas530_measure(yas5xx
, &t
, &x
, &y1
, &y2
);
514 /* Do some linearization if available */
515 sx
= yas530_linearize(yas5xx
, x
, 0);
516 sy1
= yas530_linearize(yas5xx
, y1
, 1);
517 sy2
= yas530_linearize(yas5xx
, y2
, 2);
520 * Set the temperature for compensation (unit: counts):
521 * YAS532/YAS533 version AC uses the temperature deviation as a
522 * multiplier. YAS530 and YAS532 version AB use solely the t value.
525 if (ci
->devid
== YAS532_DEVICE_ID
&&
526 yas5xx
->version
== YAS532_VERSION_AC
) {
533 * Temperature compensation for x, y1, y2 respectively:
536 * x' = x - -----------
539 sx
= sx
- (c
->Cx
* t_comp
) / 100;
540 sy1
= sy1
- (c
->Cy1
* t_comp
) / 100;
541 sy2
= sy2
- (c
->Cy2
* t_comp
) / 100;
544 * Break y1 and y2 into y and z, y1 and y2 are apparently encoding
550 /* Calculate temperature readout */
551 *to
= yas5xx_calc_temperature(yas5xx
, t
);
554 * Calibrate [x,y,z] with some formulas like this:
556 * 100 * x + a_2 * y + a_3 * z
557 * x' = k * ---------------------------
560 * a_4 * x + a_5 * y + a_6 * z
561 * y' = k * ---------------------------
564 * a_7 * x + a_8 * y + a_9 * z
565 * z' = k * ---------------------------
568 *xo
= c
->k
* ((100 * sx
+ c
->a2
* sy
+ c
->a3
* sz
) / 10);
569 *yo
= c
->k
* ((c
->a4
* sx
+ c
->a5
* sy
+ c
->a6
* sz
) / 10);
570 *zo
= c
->k
* ((c
->a7
* sx
+ c
->a8
* sy
+ c
->a9
* sz
) / 10);
576 * yas537_get_measure() - Measure a sample of all axis and process
577 * @yas5xx: The device state
578 * @to: Temperature out
582 * @return: 0 on success or error code
584 static int yas537_get_measure(struct yas5xx
*yas5xx
, s32
*to
, s32
*xo
, s32
*yo
, s32
*zo
)
589 /* We first get raw data that needs to be translated to [x,y,z] */
590 ret
= yas537_measure(yas5xx
, &t
, &x
, &y1
, &y2
);
594 /* Calculate temperature readout */
595 *to
= yas5xx_calc_temperature(yas5xx
, t
);
598 * Unfortunately, no linearization or temperature compensation formulas
599 * are known for YAS537.
602 /* Calculate x, y, z from x, y1, y2 */
603 *xo
= (x
- BIT(13)) * 300;
604 *yo
= (y1
- y2
) * 1732 / 10;
605 *zo
= (-y1
- y2
+ BIT(14)) * 300;
610 static int yas5xx_read_raw(struct iio_dev
*indio_dev
,
611 struct iio_chan_spec
const *chan
,
615 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
616 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
621 case IIO_CHAN_INFO_PROCESSED
:
622 case IIO_CHAN_INFO_RAW
:
623 pm_runtime_get_sync(yas5xx
->dev
);
624 ret
= ci
->get_measure(yas5xx
, &t
, &x
, &y
, &z
);
625 pm_runtime_mark_last_busy(yas5xx
->dev
);
626 pm_runtime_put_autosuspend(yas5xx
->dev
);
629 switch (chan
->address
) {
643 dev_err(yas5xx
->dev
, "unknown channel\n");
647 case IIO_CHAN_INFO_SCALE
:
649 *val2
= ci
->scaling_val2
;
650 return IIO_VAL_FRACTIONAL
;
652 /* Unknown request */
657 static void yas5xx_fill_buffer(struct iio_dev
*indio_dev
)
659 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
660 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
664 pm_runtime_get_sync(yas5xx
->dev
);
665 ret
= ci
->get_measure(yas5xx
, &t
, &x
, &y
, &z
);
666 pm_runtime_mark_last_busy(yas5xx
->dev
);
667 pm_runtime_put_autosuspend(yas5xx
->dev
);
669 dev_err(yas5xx
->dev
, "error refilling buffer\n");
672 yas5xx
->scan
.channels
[0] = t
;
673 yas5xx
->scan
.channels
[1] = x
;
674 yas5xx
->scan
.channels
[2] = y
;
675 yas5xx
->scan
.channels
[3] = z
;
676 iio_push_to_buffers_with_timestamp(indio_dev
, &yas5xx
->scan
,
677 iio_get_time_ns(indio_dev
));
680 static irqreturn_t
yas5xx_handle_trigger(int irq
, void *p
)
682 const struct iio_poll_func
*pf
= p
;
683 struct iio_dev
*indio_dev
= pf
->indio_dev
;
685 yas5xx_fill_buffer(indio_dev
);
686 iio_trigger_notify_done(indio_dev
->trig
);
692 static const struct iio_mount_matrix
*
693 yas5xx_get_mount_matrix(const struct iio_dev
*indio_dev
,
694 const struct iio_chan_spec
*chan
)
696 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
698 return &yas5xx
->orientation
;
701 static const struct iio_chan_spec_ext_info yas5xx_ext_info
[] = {
702 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, yas5xx_get_mount_matrix
),
706 #define YAS5XX_AXIS_CHANNEL(axis, index) \
710 .channel2 = IIO_MOD_##axis, \
711 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
712 BIT(IIO_CHAN_INFO_SCALE), \
713 .ext_info = yas5xx_ext_info, \
715 .scan_index = index, \
720 .endianness = IIO_CPU, \
724 static const struct iio_chan_spec yas5xx_channels
[] = {
727 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
734 .endianness
= IIO_CPU
,
737 YAS5XX_AXIS_CHANNEL(X
, 1),
738 YAS5XX_AXIS_CHANNEL(Y
, 2),
739 YAS5XX_AXIS_CHANNEL(Z
, 3),
740 IIO_CHAN_SOFT_TIMESTAMP(4),
743 static const unsigned long yas5xx_scan_masks
[] = { GENMASK(3, 0), 0 };
745 static const struct iio_info yas5xx_info
= {
746 .read_raw
= &yas5xx_read_raw
,
749 static bool yas5xx_volatile_reg(struct device
*dev
, unsigned int reg
)
751 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
752 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
753 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
757 if (reg
>= YAS5XX_MEASURE_DATA
&& reg
< YAS5XX_MEASURE_DATA
+ 8)
761 * YAS versions share different registers on the same address,
762 * need to differentiate.
764 reg_qty
= ci
->volatile_reg_qty
;
765 for (i
= 0; i
< reg_qty
; i
++) {
766 if (reg
== ci
->volatile_reg
[i
])
773 /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
774 static const struct regmap_config yas5xx_regmap_config
= {
777 .max_register
= 0xff,
778 .volatile_reg
= yas5xx_volatile_reg
,
782 * yas530_extract_calibration() - extracts the a2-a9 and k calibration
783 * @data: the bitfield to use
784 * @c: the calibration to populate
786 * Used by YAS530, YAS532 and YAS533.
788 static void yas530_extract_calibration(u8
*data
, struct yas5xx_calibration
*c
)
790 u64 val
= get_unaligned_be64(data
);
793 * Bitfield layout for the axis calibration data, for factor
794 * a2 = 2 etc, k = k, c = clock divider
797 * 0 [ 2 2 2 2 2 2 3 3 ] bits 63 .. 56
798 * 1 [ 3 3 4 4 4 4 4 4 ] bits 55 .. 48
799 * 2 [ 5 5 5 5 5 5 6 6 ] bits 47 .. 40
800 * 3 [ 6 6 6 6 7 7 7 7 ] bits 39 .. 32
801 * 4 [ 7 7 7 8 8 8 8 8 ] bits 31 .. 24
802 * 5 [ 8 9 9 9 9 9 9 9 ] bits 23 .. 16
803 * 6 [ 9 k k k k k c c ] bits 15 .. 8
804 * 7 [ c x x x x x x x ] bits 7 .. 0
806 c
->a2
= FIELD_GET(GENMASK_ULL(63, 58), val
) - 32;
807 c
->a3
= FIELD_GET(GENMASK_ULL(57, 54), val
) - 8;
808 c
->a4
= FIELD_GET(GENMASK_ULL(53, 48), val
) - 32;
809 c
->a5
= FIELD_GET(GENMASK_ULL(47, 42), val
) + 38;
810 c
->a6
= FIELD_GET(GENMASK_ULL(41, 36), val
) - 32;
811 c
->a7
= FIELD_GET(GENMASK_ULL(35, 29), val
) - 64;
812 c
->a8
= FIELD_GET(GENMASK_ULL(28, 23), val
) - 32;
813 c
->a9
= FIELD_GET(GENMASK_ULL(22, 15), val
);
814 c
->k
= FIELD_GET(GENMASK_ULL(14, 10), val
) + 10;
815 c
->dck
= FIELD_GET(GENMASK_ULL(9, 7), val
);
818 static int yas530_get_calibration_data(struct yas5xx
*yas5xx
)
820 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
825 /* Dummy read, first read is ALWAYS wrong */
826 ret
= regmap_bulk_read(yas5xx
->map
, YAS530_CAL
, data
, sizeof(data
));
830 /* Actual calibration readout */
831 ret
= regmap_bulk_read(yas5xx
->map
, YAS530_CAL
, data
, sizeof(data
));
834 dev_dbg(yas5xx
->dev
, "calibration data: %16ph\n", data
);
836 /* Contribute calibration data to the input pool for kernel entropy */
837 add_device_randomness(data
, sizeof(data
));
839 /* Extract version */
840 yas5xx
->version
= data
[15] & GENMASK(1, 0);
842 /* Extract the calibration from the bitfield */
843 c
->Cx
= data
[0] * 6 - 768;
844 c
->Cy1
= data
[1] * 6 - 768;
845 c
->Cy2
= data
[2] * 6 - 768;
846 yas530_extract_calibration(&data
[3], c
);
849 * Extract linearization:
850 * Linearization layout in the 32 bits at byte 11:
851 * The r factors are 6 bit values where bit 5 is the sign
854 * 0 [ xx xx xx r0 r0 r0 r0 r0 ] bits 31 .. 24
855 * 1 [ r0 f0 f0 r1 r1 r1 r1 r1 ] bits 23 .. 16
856 * 2 [ r1 f1 f1 r2 r2 r2 r2 r2 ] bits 15 .. 8
857 * 3 [ r2 f2 f2 xx xx xx xx xx ] bits 7 .. 0
859 val
= get_unaligned_be32(&data
[11]);
860 c
->f
[0] = FIELD_GET(GENMASK(22, 21), val
);
861 c
->f
[1] = FIELD_GET(GENMASK(14, 13), val
);
862 c
->f
[2] = FIELD_GET(GENMASK(6, 5), val
);
863 c
->r
[0] = sign_extend32(FIELD_GET(GENMASK(28, 23), val
), 5);
864 c
->r
[1] = sign_extend32(FIELD_GET(GENMASK(20, 15), val
), 5);
865 c
->r
[2] = sign_extend32(FIELD_GET(GENMASK(12, 7), val
), 5);
870 static int yas532_get_calibration_data(struct yas5xx
*yas5xx
)
872 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
877 /* Dummy read, first read is ALWAYS wrong */
878 ret
= regmap_bulk_read(yas5xx
->map
, YAS530_CAL
, data
, sizeof(data
));
881 /* Actual calibration readout */
882 ret
= regmap_bulk_read(yas5xx
->map
, YAS530_CAL
, data
, sizeof(data
));
885 dev_dbg(yas5xx
->dev
, "calibration data: %14ph\n", data
);
887 /* Sanity check, is this all zeroes? */
888 if (!memchr_inv(data
, 0x00, 13) && !(data
[13] & BIT(7)))
889 dev_warn(yas5xx
->dev
, "calibration is blank!\n");
891 /* Contribute calibration data to the input pool for kernel entropy */
892 add_device_randomness(data
, sizeof(data
));
894 /* Only one bit of version info reserved here as far as we know */
895 yas5xx
->version
= data
[13] & BIT(0);
897 /* Extract calibration from the bitfield */
898 c
->Cx
= data
[0] * 10 - 1280;
899 c
->Cy1
= data
[1] * 10 - 1280;
900 c
->Cy2
= data
[2] * 10 - 1280;
901 yas530_extract_calibration(&data
[3], c
);
904 * Extract linearization:
905 * Linearization layout in the 32 bits at byte 10:
906 * The r factors are 6 bit values where bit 5 is the sign
909 * 0 [ xx r0 r0 r0 r0 r0 r0 f0 ] bits 31 .. 24
910 * 1 [ f0 r1 r1 r1 r1 r1 r1 f1 ] bits 23 .. 16
911 * 2 [ f1 r2 r2 r2 r2 r2 r2 f2 ] bits 15 .. 8
912 * 3 [ f2 xx xx xx xx xx xx xx ] bits 7 .. 0
914 val
= get_unaligned_be32(&data
[10]);
915 c
->f
[0] = FIELD_GET(GENMASK(24, 23), val
);
916 c
->f
[1] = FIELD_GET(GENMASK(16, 15), val
);
917 c
->f
[2] = FIELD_GET(GENMASK(8, 7), val
);
918 c
->r
[0] = sign_extend32(FIELD_GET(GENMASK(30, 25), val
), 5);
919 c
->r
[1] = sign_extend32(FIELD_GET(GENMASK(22, 17), val
), 5);
920 c
->r
[2] = sign_extend32(FIELD_GET(GENMASK(14, 7), val
), 5);
925 static int yas537_get_calibration_data(struct yas5xx
*yas5xx
)
927 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
929 u32 val1
, val2
, val3
, val4
;
932 /* Writing SRST register */
933 ret
= regmap_write(yas5xx
->map
, YAS537_SRST
, BIT(1));
937 /* Calibration readout, YAS537 needs one readout only */
938 ret
= regmap_bulk_read(yas5xx
->map
, YAS537_CAL
, data
, sizeof(data
));
941 dev_dbg(yas5xx
->dev
, "calibration data: %17ph\n", data
);
943 /* Sanity check, is this all zeroes? */
944 if (!memchr_inv(data
, 0x00, 16) && !FIELD_GET(GENMASK(5, 0), data
[16]))
945 dev_warn(yas5xx
->dev
, "calibration is blank!\n");
947 /* Contribute calibration data to the input pool for kernel entropy */
948 add_device_randomness(data
, sizeof(data
));
950 /* Extract version information */
951 yas5xx
->version
= FIELD_GET(GENMASK(7, 6), data
[16]);
953 /* There are two versions of YAS537 behaving differently */
954 switch (yas5xx
->version
) {
955 case YAS537_VERSION_0
:
957 * The first version simply writes data back into registers:
959 * data[0] YAS537_MTC 0x93
970 * data[11] YAS537_OC 0x9e
972 * data[12] YAS537_OFFSET_X 0x84
973 * data[13] YAS537_OFFSET_Y1 0x85
974 * data[14] YAS537_OFFSET_Y2 0x86
976 * data[15] YAS537_HCK 0x88
977 * data[16] YAS537_LCK 0x89
979 for (i
= 0; i
< 12; i
++) {
980 ret
= regmap_write(yas5xx
->map
, YAS537_MTC
+ i
,
985 for (i
= 0; i
< 3; i
++) {
986 ret
= regmap_write(yas5xx
->map
, YAS537_OFFSET_X
+ i
,
990 yas5xx
->hard_offsets
[i
] = data
[i
+ 12];
992 for (i
= 0; i
< 2; i
++) {
993 ret
= regmap_write(yas5xx
->map
, YAS537_HCK
+ i
,
999 case YAS537_VERSION_1
:
1001 * The second version writes some data into registers but also
1002 * extracts calibration coefficients.
1004 * Registers being written:
1006 * data[0] YAS537_MTC 0x93
1007 * data[1] YAS537_MTC+1 0x94
1008 * data[2] YAS537_MTC+2 0x95
1009 * data[3] YAS537_MTC+3 (partially) 0x96
1011 * data[12] YAS537_OFFSET_X 0x84
1012 * data[13] YAS537_OFFSET_Y1 0x85
1013 * data[14] YAS537_OFFSET_Y2 0x86
1015 * data[15] YAS537_HCK (partially) 0x88
1016 * YAS537_LCK (partially) 0x89
1017 * data[16] YAS537_OC (partially) 0x9e
1019 for (i
= 0; i
< 3; i
++) {
1020 ret
= regmap_write(yas5xx
->map
, YAS537_MTC
+ i
,
1025 for (i
= 0; i
< 3; i
++) {
1026 ret
= regmap_write(yas5xx
->map
, YAS537_OFFSET_X
+ i
,
1030 yas5xx
->hard_offsets
[i
] = data
[i
+ 12];
1033 * Visualization of partially taken data:
1035 * data[3] n 7 6 5 4 3 2 1 0
1036 * YAS537_MTC+3 x x x 1 0 0 0 0
1038 * data[15] n 7 6 5 4 3 2 1 0
1039 * YAS537_HCK x x x x 0
1041 * data[15] n 7 6 5 4 3 2 1 0
1042 * YAS537_LCK x x x x 0
1044 * data[16] n 7 6 5 4 3 2 1 0
1045 * YAS537_OC x x x x x x
1047 ret
= regmap_write(yas5xx
->map
, YAS537_MTC
+ 3,
1048 FIELD_PREP(YAS537_MTC3_MASK_PREP
,
1049 FIELD_GET(YAS537_MTC3_MASK_GET
, data
[3])) |
1050 YAS537_MTC3_ADD_BIT
);
1053 ret
= regmap_write(yas5xx
->map
, YAS537_HCK
,
1054 FIELD_PREP(YAS537_HCK_MASK_PREP
,
1055 FIELD_GET(YAS537_HCK_MASK_GET
, data
[15])));
1058 ret
= regmap_write(yas5xx
->map
, YAS537_LCK
,
1059 FIELD_PREP(YAS537_LCK_MASK_PREP
,
1060 FIELD_GET(YAS537_LCK_MASK_GET
, data
[15])));
1063 ret
= regmap_write(yas5xx
->map
, YAS537_OC
,
1064 FIELD_GET(YAS537_OC_MASK_GET
, data
[16]));
1068 * For data extraction, build some blocks. Four 32-bit blocks
1072 * data[0] 0 [ Cx Cx Cx Cx Cx Cx Cx Cx ] bits 31 .. 24
1073 * data[1] 1 [ Cx C1 C1 C1 C1 C1 C1 C1 ] bits 23 .. 16
1074 * data[2] 2 [ C1 C1 C2 C2 C2 C2 C2 C2 ] bits 15 .. 8
1075 * data[3] 3 [ C2 C2 C2 ] bits 7 .. 0
1078 * data[3] 0 [ a2 a2 a2 a2 a2 ] bits 31 .. 24
1079 * data[4] 1 [ a2 a2 a3 a3 a3 a3 a3 a3 ] bits 23 .. 16
1080 * data[5] 2 [ a3 a4 a4 a4 a4 a4 a4 a4 ] bits 15 .. 8
1081 * data[6] 3 [ a4 ] bits 7 .. 0
1084 * data[6] 0 [ a5 a5 a5 a5 a5 a5 a5 ] bits 31 .. 24
1085 * data[7] 1 [ a5 a5 a6 a6 a6 a6 a6 a6 ] bits 23 .. 16
1086 * data[8] 2 [ a6 a7 a7 a7 a7 a7 a7 a7 ] bits 15 .. 8
1087 * data[9] 3 [ a7 ] bits 7 .. 0
1090 * data[9] 0 [ a8 a8 a8 a8 a8 a8 a8 ] bits 31 .. 24
1091 * data[10] 1 [ a9 a9 a9 a9 a9 a9 a9 a9 ] bits 23 .. 16
1092 * data[11] 2 [ a9 k k k k k k k ] bits 15 .. 8
1093 * data[12] 3 [ ] bits 7 .. 0
1095 val1
= get_unaligned_be32(&data
[0]);
1096 val2
= get_unaligned_be32(&data
[3]);
1097 val3
= get_unaligned_be32(&data
[6]);
1098 val4
= get_unaligned_be32(&data
[9]);
1099 /* Extract calibration coefficients and modify */
1100 c
->Cx
= FIELD_GET(GENMASK(31, 23), val1
) - 256;
1101 c
->Cy1
= FIELD_GET(GENMASK(22, 14), val1
) - 256;
1102 c
->Cy2
= FIELD_GET(GENMASK(13, 5), val1
) - 256;
1103 c
->a2
= FIELD_GET(GENMASK(28, 22), val2
) - 64;
1104 c
->a3
= FIELD_GET(GENMASK(21, 15), val2
) - 64;
1105 c
->a4
= FIELD_GET(GENMASK(14, 7), val2
) - 128;
1106 c
->a5
= FIELD_GET(GENMASK(30, 22), val3
) - 112;
1107 c
->a6
= FIELD_GET(GENMASK(21, 15), val3
) - 64;
1108 c
->a7
= FIELD_GET(GENMASK(14, 7), val3
) - 128;
1109 c
->a8
= FIELD_GET(GENMASK(30, 24), val4
) - 64;
1110 c
->a9
= FIELD_GET(GENMASK(23, 15), val4
) - 112;
1111 c
->k
= FIELD_GET(GENMASK(14, 8), val4
);
1114 dev_err(yas5xx
->dev
, "unknown version of YAS537\n");
1121 /* Used by YAS530, YAS532 and YAS533 */
1122 static void yas530_dump_calibration(struct yas5xx
*yas5xx
)
1124 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
1126 dev_dbg(yas5xx
->dev
, "f[] = [%d, %d, %d]\n",
1127 c
->f
[0], c
->f
[1], c
->f
[2]);
1128 dev_dbg(yas5xx
->dev
, "r[] = [%d, %d, %d]\n",
1129 c
->r
[0], c
->r
[1], c
->r
[2]);
1130 dev_dbg(yas5xx
->dev
, "Cx = %d\n", c
->Cx
);
1131 dev_dbg(yas5xx
->dev
, "Cy1 = %d\n", c
->Cy1
);
1132 dev_dbg(yas5xx
->dev
, "Cy2 = %d\n", c
->Cy2
);
1133 dev_dbg(yas5xx
->dev
, "a2 = %d\n", c
->a2
);
1134 dev_dbg(yas5xx
->dev
, "a3 = %d\n", c
->a3
);
1135 dev_dbg(yas5xx
->dev
, "a4 = %d\n", c
->a4
);
1136 dev_dbg(yas5xx
->dev
, "a5 = %d\n", c
->a5
);
1137 dev_dbg(yas5xx
->dev
, "a6 = %d\n", c
->a6
);
1138 dev_dbg(yas5xx
->dev
, "a7 = %d\n", c
->a7
);
1139 dev_dbg(yas5xx
->dev
, "a8 = %d\n", c
->a8
);
1140 dev_dbg(yas5xx
->dev
, "a9 = %d\n", c
->a9
);
1141 dev_dbg(yas5xx
->dev
, "k = %d\n", c
->k
);
1142 dev_dbg(yas5xx
->dev
, "dck = %d\n", c
->dck
);
1145 static void yas537_dump_calibration(struct yas5xx
*yas5xx
)
1147 struct yas5xx_calibration
*c
= &yas5xx
->calibration
;
1149 if (yas5xx
->version
== YAS537_VERSION_1
) {
1150 dev_dbg(yas5xx
->dev
, "Cx = %d\n", c
->Cx
);
1151 dev_dbg(yas5xx
->dev
, "Cy1 = %d\n", c
->Cy1
);
1152 dev_dbg(yas5xx
->dev
, "Cy2 = %d\n", c
->Cy2
);
1153 dev_dbg(yas5xx
->dev
, "a2 = %d\n", c
->a2
);
1154 dev_dbg(yas5xx
->dev
, "a3 = %d\n", c
->a3
);
1155 dev_dbg(yas5xx
->dev
, "a4 = %d\n", c
->a4
);
1156 dev_dbg(yas5xx
->dev
, "a5 = %d\n", c
->a5
);
1157 dev_dbg(yas5xx
->dev
, "a6 = %d\n", c
->a6
);
1158 dev_dbg(yas5xx
->dev
, "a7 = %d\n", c
->a7
);
1159 dev_dbg(yas5xx
->dev
, "a8 = %d\n", c
->a8
);
1160 dev_dbg(yas5xx
->dev
, "a9 = %d\n", c
->a9
);
1161 dev_dbg(yas5xx
->dev
, "k = %d\n", c
->k
);
1165 /* Used by YAS530, YAS532 and YAS533 */
1166 static int yas530_set_offsets(struct yas5xx
*yas5xx
, s8 ox
, s8 oy1
, s8 oy2
)
1170 ret
= regmap_write(yas5xx
->map
, YAS530_OFFSET_X
, ox
);
1173 ret
= regmap_write(yas5xx
->map
, YAS530_OFFSET_Y1
, oy1
);
1176 return regmap_write(yas5xx
->map
, YAS530_OFFSET_Y2
, oy2
);
1179 /* Used by YAS530, YAS532 and YAS533 */
1180 static s8
yas530_adjust_offset(s8 old
, int bit
, u16 center
, u16 measure
)
1182 if (measure
> center
)
1183 return old
+ BIT(bit
);
1184 if (measure
< center
)
1185 return old
- BIT(bit
);
1189 /* Used by YAS530, YAS532 and YAS533 */
1190 static int yas530_measure_offsets(struct yas5xx
*yas5xx
)
1192 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
1199 /* Actuate the init coil and measure offsets */
1200 ret
= regmap_write(yas5xx
->map
, YAS530_ACTUATE_INIT_COIL
, 0);
1204 /* When the initcoil is active this should be around the center */
1205 switch (ci
->devid
) {
1206 case YAS530_DEVICE_ID
:
1207 center
= YAS530_DATA_CENTER
;
1209 case YAS532_DEVICE_ID
:
1210 center
= YAS532_DATA_CENTER
;
1213 dev_err(yas5xx
->dev
, "unknown device type\n");
1218 * We set offsets in the interval +-31 by iterating
1219 * +-16, +-8, +-4, +-2, +-1 adjusting the offsets each
1220 * time, then writing the final offsets into the
1223 * NOTE: these offsets are NOT in the same unit or magnitude
1224 * as the values for [x, y1, y2]. The value is +/-31
1225 * but the effect on the raw values is much larger.
1226 * The effect of the offset is to bring the measure
1227 * rougly to the center.
1233 for (i
= 4; i
>= 0; i
--) {
1234 ret
= yas530_set_offsets(yas5xx
, ox
, oy1
, oy2
);
1238 ret
= yas530_measure(yas5xx
, &t
, &x
, &y1
, &y2
);
1241 dev_dbg(yas5xx
->dev
, "measurement %d: x=%d, y1=%d, y2=%d\n",
1244 ox
= yas530_adjust_offset(ox
, i
, center
, x
);
1245 oy1
= yas530_adjust_offset(oy1
, i
, center
, y1
);
1246 oy2
= yas530_adjust_offset(oy2
, i
, center
, y2
);
1249 /* Needed for calibration algorithm */
1250 yas5xx
->hard_offsets
[0] = ox
;
1251 yas5xx
->hard_offsets
[1] = oy1
;
1252 yas5xx
->hard_offsets
[2] = oy2
;
1253 ret
= yas530_set_offsets(yas5xx
, ox
, oy1
, oy2
);
1257 dev_info(yas5xx
->dev
, "discovered hard offsets: x=%d, y1=%d, y2=%d\n",
1262 /* Used by YAS530, YAS532 and YAS533 */
1263 static int yas530_power_on(struct yas5xx
*yas5xx
)
1268 /* Zero the test registers */
1269 ret
= regmap_write(yas5xx
->map
, YAS530_TEST1
, 0);
1272 ret
= regmap_write(yas5xx
->map
, YAS530_TEST2
, 0);
1276 /* Set up for no interrupts, calibrated clock divider */
1277 val
= FIELD_PREP(YAS5XX_CONFIG_CCK_MASK
, yas5xx
->calibration
.dck
);
1278 ret
= regmap_write(yas5xx
->map
, YAS530_CONFIG
, val
);
1282 /* Measure interval 0 (back-to-back?) */
1283 return regmap_write(yas5xx
->map
, YAS530_MEASURE_INTERVAL
, 0);
1286 static int yas537_power_on(struct yas5xx
*yas5xx
)
1292 /* Writing ADCCAL and TRM registers */
1293 buf
= cpu_to_be16(GENMASK(9, 3));
1294 ret
= regmap_bulk_write(yas5xx
->map
, YAS537_ADCCAL
, &buf
, sizeof(buf
));
1297 ret
= regmap_write(yas5xx
->map
, YAS537_TRM
, GENMASK(7, 0));
1301 /* The interval value is static in regular operation */
1302 intrvl
= (YAS537_DEFAULT_SENSOR_DELAY_MS
* MILLI
1303 - YAS537_MEASURE_TIME_WORST_US
) / 4100;
1304 ret
= regmap_write(yas5xx
->map
, YAS537_MEASURE_INTERVAL
, intrvl
);
1308 /* The average value is also static in regular operation */
1309 ret
= regmap_write(yas5xx
->map
, YAS537_AVR
, YAS537_MAG_AVERAGE_32_MASK
);
1313 /* Perform the "rcoil" part but skip the "last_after_rcoil" read */
1314 ret
= regmap_write(yas5xx
->map
, YAS537_CONFIG
, BIT(3));
1318 /* Wait until the coil has ramped up */
1319 usleep_range(YAS537_MAG_RCOIL_TIME_US
, YAS537_MAG_RCOIL_TIME_US
+ 100);
1324 static const struct yas5xx_chip_info yas5xx_chip_info_tbl
[] = {
1326 .devid
= YAS530_DEVICE_ID
,
1327 .product_name
= "YAS530 MS-3E",
1328 .version_names
= { "A", "B" },
1329 .volatile_reg
= yas530_volatile_reg
,
1330 .volatile_reg_qty
= ARRAY_SIZE(yas530_volatile_reg
),
1331 .scaling_val2
= 100000000, /* picotesla to Gauss */
1332 .t_ref
= 182, /* counts */
1333 .min_temp_x10
= -620, /* 1/10:s degrees Celsius */
1334 .get_measure
= yas530_get_measure
,
1335 .get_calibration_data
= yas530_get_calibration_data
,
1336 .dump_calibration
= yas530_dump_calibration
,
1337 .measure_offsets
= yas530_measure_offsets
,
1338 .power_on
= yas530_power_on
,
1341 .devid
= YAS532_DEVICE_ID
,
1342 .product_name
= "YAS532 MS-3R",
1343 .version_names
= { "AB", "AC" },
1344 .volatile_reg
= yas530_volatile_reg
,
1345 .volatile_reg_qty
= ARRAY_SIZE(yas530_volatile_reg
),
1346 .scaling_val2
= 100000, /* nanotesla to Gauss */
1347 .t_ref
= 390, /* counts */
1348 .min_temp_x10
= -500, /* 1/10:s degrees Celsius */
1349 .get_measure
= yas530_get_measure
,
1350 .get_calibration_data
= yas532_get_calibration_data
,
1351 .dump_calibration
= yas530_dump_calibration
,
1352 .measure_offsets
= yas530_measure_offsets
,
1353 .power_on
= yas530_power_on
,
1356 .devid
= YAS532_DEVICE_ID
,
1357 .product_name
= "YAS533 MS-3F",
1358 .version_names
= { "AB", "AC" },
1359 .volatile_reg
= yas530_volatile_reg
,
1360 .volatile_reg_qty
= ARRAY_SIZE(yas530_volatile_reg
),
1361 .scaling_val2
= 100000, /* nanotesla to Gauss */
1362 .t_ref
= 390, /* counts */
1363 .min_temp_x10
= -500, /* 1/10:s degrees Celsius */
1364 .get_measure
= yas530_get_measure
,
1365 .get_calibration_data
= yas532_get_calibration_data
,
1366 .dump_calibration
= yas530_dump_calibration
,
1367 .measure_offsets
= yas530_measure_offsets
,
1368 .power_on
= yas530_power_on
,
1371 .devid
= YAS537_DEVICE_ID
,
1372 .product_name
= "YAS537 MS-3T",
1373 .version_names
= { "v0", "v1" }, /* version naming unknown */
1374 .volatile_reg
= yas537_volatile_reg
,
1375 .volatile_reg_qty
= ARRAY_SIZE(yas537_volatile_reg
),
1376 .scaling_val2
= 100000, /* nanotesla to Gauss */
1377 .t_ref
= 8120, /* counts */
1378 .min_temp_x10
= -3860, /* 1/10:s degrees Celsius */
1379 .get_measure
= yas537_get_measure
,
1380 .get_calibration_data
= yas537_get_calibration_data
,
1381 .dump_calibration
= yas537_dump_calibration
,
1382 /* .measure_offets is not needed for yas537 */
1383 .power_on
= yas537_power_on
,
1387 static int yas5xx_probe(struct i2c_client
*i2c
)
1389 const struct i2c_device_id
*id
= i2c_client_get_device_id(i2c
);
1390 struct iio_dev
*indio_dev
;
1391 struct device
*dev
= &i2c
->dev
;
1392 struct yas5xx
*yas5xx
;
1393 const struct yas5xx_chip_info
*ci
;
1397 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*yas5xx
));
1401 yas5xx
= iio_priv(indio_dev
);
1402 i2c_set_clientdata(i2c
, indio_dev
);
1404 mutex_init(&yas5xx
->lock
);
1406 ret
= iio_read_mount_matrix(dev
, &yas5xx
->orientation
);
1410 yas5xx
->regs
[0].supply
= "vdd";
1411 yas5xx
->regs
[1].supply
= "iovdd";
1412 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(yas5xx
->regs
),
1415 return dev_err_probe(dev
, ret
, "cannot get regulators\n");
1417 ret
= regulator_bulk_enable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1419 return dev_err_probe(dev
, ret
, "cannot enable regulators\n");
1421 /* See comment in runtime resume callback */
1422 usleep_range(31000, 40000);
1424 /* This will take the device out of reset if need be */
1425 yas5xx
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
1426 if (IS_ERR(yas5xx
->reset
)) {
1427 ret
= dev_err_probe(dev
, PTR_ERR(yas5xx
->reset
), "failed to get reset line\n");
1431 yas5xx
->map
= devm_regmap_init_i2c(i2c
, &yas5xx_regmap_config
);
1432 if (IS_ERR(yas5xx
->map
)) {
1433 ret
= dev_err_probe(dev
, PTR_ERR(yas5xx
->map
), "failed to allocate register map\n");
1437 ci
= i2c_get_match_data(i2c
);
1438 yas5xx
->chip_info
= ci
;
1440 ret
= regmap_read(yas5xx
->map
, YAS5XX_DEVICE_ID
, &id_check
);
1444 if (id_check
!= ci
->devid
) {
1445 ret
= dev_err_probe(dev
, -ENODEV
,
1446 "device ID %02x doesn't match %s\n",
1447 id_check
, id
->name
);
1451 ret
= ci
->get_calibration_data(yas5xx
);
1455 dev_info(dev
, "detected %s %s\n", ci
->product_name
,
1456 ci
->version_names
[yas5xx
->version
]);
1458 ci
->dump_calibration(yas5xx
);
1460 ret
= ci
->power_on(yas5xx
);
1464 if (ci
->measure_offsets
) {
1465 ret
= ci
->measure_offsets(yas5xx
);
1470 indio_dev
->info
= &yas5xx_info
;
1471 indio_dev
->available_scan_masks
= yas5xx_scan_masks
;
1472 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1473 indio_dev
->name
= id
->name
;
1474 indio_dev
->channels
= yas5xx_channels
;
1475 indio_dev
->num_channels
= ARRAY_SIZE(yas5xx_channels
);
1477 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
1478 yas5xx_handle_trigger
,
1481 dev_err_probe(dev
, ret
, "triggered buffer setup failed\n");
1485 ret
= iio_device_register(indio_dev
);
1487 dev_err_probe(dev
, ret
, "device register failed\n");
1488 goto cleanup_buffer
;
1491 /* Take runtime PM online */
1492 pm_runtime_get_noresume(dev
);
1493 pm_runtime_set_active(dev
);
1494 pm_runtime_enable(dev
);
1496 pm_runtime_set_autosuspend_delay(dev
, YAS5XX_AUTOSUSPEND_DELAY_MS
);
1497 pm_runtime_use_autosuspend(dev
);
1498 pm_runtime_put(dev
);
1503 iio_triggered_buffer_cleanup(indio_dev
);
1505 gpiod_set_value_cansleep(yas5xx
->reset
, 1);
1507 regulator_bulk_disable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1512 static void yas5xx_remove(struct i2c_client
*i2c
)
1514 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
1515 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
1516 struct device
*dev
= &i2c
->dev
;
1518 iio_device_unregister(indio_dev
);
1519 iio_triggered_buffer_cleanup(indio_dev
);
1521 * Now we can't get any more reads from the device, which would
1522 * also call pm_runtime* functions and race with our disable
1523 * code. Disable PM runtime in orderly fashion and power down.
1525 pm_runtime_get_sync(dev
);
1526 pm_runtime_put_noidle(dev
);
1527 pm_runtime_disable(dev
);
1528 gpiod_set_value_cansleep(yas5xx
->reset
, 1);
1529 regulator_bulk_disable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1532 static int yas5xx_runtime_suspend(struct device
*dev
)
1534 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1535 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
1537 gpiod_set_value_cansleep(yas5xx
->reset
, 1);
1538 regulator_bulk_disable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1543 static int yas5xx_runtime_resume(struct device
*dev
)
1545 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1546 struct yas5xx
*yas5xx
= iio_priv(indio_dev
);
1547 const struct yas5xx_chip_info
*ci
= yas5xx
->chip_info
;
1550 ret
= regulator_bulk_enable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1552 dev_err(dev
, "cannot enable regulators\n");
1557 * The YAS530 datasheet says TVSKW is up to 30 ms, after that 1 ms
1558 * for all voltages to settle. The YAS532 is 10ms then 4ms for the
1559 * I2C to come online. Let's keep it safe and put this at 31ms.
1561 usleep_range(31000, 40000);
1562 gpiod_set_value_cansleep(yas5xx
->reset
, 0);
1564 ret
= ci
->power_on(yas5xx
);
1566 dev_err(dev
, "cannot power on\n");
1573 gpiod_set_value_cansleep(yas5xx
->reset
, 1);
1574 regulator_bulk_disable(ARRAY_SIZE(yas5xx
->regs
), yas5xx
->regs
);
1579 static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops
, yas5xx_runtime_suspend
,
1580 yas5xx_runtime_resume
, NULL
);
1582 static const struct i2c_device_id yas5xx_id
[] = {
1583 {"yas530", (kernel_ulong_t
)&yas5xx_chip_info_tbl
[yas530
] },
1584 {"yas532", (kernel_ulong_t
)&yas5xx_chip_info_tbl
[yas532
] },
1585 {"yas533", (kernel_ulong_t
)&yas5xx_chip_info_tbl
[yas533
] },
1586 {"yas537", (kernel_ulong_t
)&yas5xx_chip_info_tbl
[yas537
] },
1589 MODULE_DEVICE_TABLE(i2c
, yas5xx_id
);
1591 static const struct of_device_id yas5xx_of_match
[] = {
1592 { .compatible
= "yamaha,yas530", &yas5xx_chip_info_tbl
[yas530
] },
1593 { .compatible
= "yamaha,yas532", &yas5xx_chip_info_tbl
[yas532
] },
1594 { .compatible
= "yamaha,yas533", &yas5xx_chip_info_tbl
[yas533
] },
1595 { .compatible
= "yamaha,yas537", &yas5xx_chip_info_tbl
[yas537
] },
1598 MODULE_DEVICE_TABLE(of
, yas5xx_of_match
);
1600 static struct i2c_driver yas5xx_driver
= {
1603 .of_match_table
= yas5xx_of_match
,
1604 .pm
= pm_ptr(&yas5xx_dev_pm_ops
),
1606 .probe
= yas5xx_probe
,
1607 .remove
= yas5xx_remove
,
1608 .id_table
= yas5xx_id
,
1610 module_i2c_driver(yas5xx_driver
);
1612 MODULE_DESCRIPTION("Yamaha YAS53x 3-axis magnetometer driver");
1613 MODULE_AUTHOR("Linus Walleij");
1614 MODULE_LICENSE("GPL v2");