drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / magnetometer / yamaha-yas530.c
blobc55a38650c0d47d4292742d52fe322aa7d758352
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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)
11 * YAS535 MS-6C
12 * YAS536 MS-3W
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
136 enum chip_ids {
137 yas530,
138 yas532,
139 yas533,
140 yas537,
143 static const int yas530_volatile_reg[] = {
144 YAS530_ACTUATE_INIT_COIL,
145 YAS530_MEASURE,
148 static const int yas537_volatile_reg[] = {
149 YAS537_MEASURE,
152 struct yas5xx_calibration {
153 /* Linearization calibration x, y1, y2 */
154 s32 r[3];
155 u32 f[3];
156 /* Temperature compensation calibration */
157 s16 Cx, Cy1, Cy2;
158 /* Misc calibration coefficients */
159 s8 a2, a3, a4, a6, a7, a8;
160 s16 a5, a9;
161 u8 k;
162 /* clock divider */
163 u8 dck;
166 struct yas5xx;
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 {
191 unsigned int devid;
192 const char *product_name;
193 const char *version_names[2];
194 const int *volatile_reg;
195 int volatile_reg_qty;
196 u32 scaling_val2;
197 u16 t_ref;
198 s16 min_temp_x10;
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
222 struct yas5xx {
223 struct device *dev;
224 const struct yas5xx_chip_info *chip_info;
225 unsigned int version;
226 struct yas5xx_calibration calibration;
227 s8 hard_offsets[3];
228 struct iio_mount_matrix orientation;
229 struct regmap *map;
230 struct regulator_bulk_data regs[2];
231 struct gpio_desc *reset;
232 struct mutex lock;
234 * The scanout is 4 x 32 bits in CPU endianness.
235 * Ensure timestamp is naturally aligned
237 struct {
238 s32 channels[4];
239 s64 ts __aligned(8);
240 } scan;
243 /* On YAS530 the x, y1 and y2 values are 12 bits */
244 static u16 yas530_extract_axis(u8 *data)
246 u16 val;
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);
255 return val;
258 /* On YAS532 the x, y1 and y2 values are 13 bits */
259 static u16 yas532_extract_axis(u8 *data)
261 u16 val;
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);
270 return 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;
287 unsigned int busy;
288 u8 data[8];
289 int ret;
290 u16 val;
292 mutex_lock(&yas5xx->lock);
293 ret = regmap_write(yas5xx->map, YAS530_MEASURE, YAS5XX_MEASURE_START);
294 if (ret < 0)
295 goto out_unlock;
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)
300 * before timeout.
302 ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA, busy,
303 !(busy & YAS5XX_MEASURE_DATA_BUSY),
304 500, 20000);
305 if (ret) {
306 dev_err(yas5xx->dev, "timeout waiting for measurement\n");
307 goto out_unlock;
310 ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
311 data, sizeof(data));
312 if (ret)
313 goto out_unlock;
315 mutex_unlock(&yas5xx->lock);
317 switch (ci->devid) {
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
323 * x x x x x x x x x
325 val = get_unaligned_be16(&data[0]);
326 val = FIELD_GET(GENMASK(14, 6), val);
327 *t = val;
328 *x = yas530_extract_axis(&data[2]);
329 *y1 = yas530_extract_axis(&data[4]);
330 *y2 = yas530_extract_axis(&data[6]);
331 break;
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);
341 *t = val;
342 *x = yas532_extract_axis(&data[2]);
343 *y1 = yas532_extract_axis(&data[4]);
344 *y2 = yas532_extract_axis(&data[6]);
345 break;
346 default:
347 dev_err(yas5xx->dev, "unknown data format\n");
348 ret = -EINVAL;
349 break;
352 return ret;
354 out_unlock:
355 mutex_unlock(&yas5xx->lock);
356 return ret;
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;
371 unsigned int busy;
372 u8 data[8];
373 u16 xy1y2[3];
374 s32 h[3], s[3];
375 int half_range = BIT(13);
376 int i, ret;
378 mutex_lock(&yas5xx->lock);
380 /* Contrary to YAS530/532, also a "cont" bit is set, meaning unknown */
381 ret = regmap_write(yas5xx->map, YAS537_MEASURE, YAS5XX_MEASURE_START |
382 YAS5XX_MEASURE_CONT);
383 if (ret < 0)
384 goto out_unlock;
386 /* Use same timeout like YAS530/532 but the bit is in data row 2 */
387 ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA + 2, busy,
388 !(busy & YAS5XX_MEASURE_DATA_BUSY),
389 500, 20000);
390 if (ret) {
391 dev_err(yas5xx->dev, "timeout waiting for measurement\n");
392 goto out_unlock;
395 ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
396 data, sizeof(data));
397 if (ret)
398 goto out_unlock;
400 mutex_unlock(&yas5xx->lock);
402 *t = get_unaligned_be16(&data[0]);
403 xy1y2[0] = FIELD_GET(GENMASK(13, 0), get_unaligned_be16(&data[2]));
404 xy1y2[1] = get_unaligned_be16(&data[4]);
405 xy1y2[2] = get_unaligned_be16(&data[6]);
407 /* The second version of YAS537 needs to include calibration coefficients */
408 if (yas5xx->version == YAS537_VERSION_1) {
409 for (i = 0; i < 3; i++)
410 s[i] = xy1y2[i] - half_range;
411 h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / half_range;
412 h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / half_range;
413 h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / half_range;
414 for (i = 0; i < 3; i++) {
415 h[i] = clamp(h[i], -half_range, half_range - 1);
416 xy1y2[i] = h[i] + half_range;
420 *x = xy1y2[0];
421 *y1 = xy1y2[1];
422 *y2 = xy1y2[2];
424 return 0;
426 out_unlock:
427 mutex_unlock(&yas5xx->lock);
428 return ret;
431 /* Used by YAS530, YAS532 and YAS533 */
432 static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis)
434 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
435 struct yas5xx_calibration *c = &yas5xx->calibration;
436 static const s32 yas532ac_coef[] = {
437 YAS532_VERSION_AC_COEF_X,
438 YAS532_VERSION_AC_COEF_Y1,
439 YAS532_VERSION_AC_COEF_Y2,
441 s32 coef;
443 /* Select coefficients */
444 switch (ci->devid) {
445 case YAS530_DEVICE_ID:
446 if (yas5xx->version == YAS530_VERSION_A)
447 coef = YAS530_VERSION_A_COEF;
448 else
449 coef = YAS530_VERSION_B_COEF;
450 break;
451 case YAS532_DEVICE_ID:
452 if (yas5xx->version == YAS532_VERSION_AB)
453 coef = YAS532_VERSION_AB_COEF;
454 else
455 /* Elaborate coefficients */
456 coef = yas532ac_coef[axis];
457 break;
458 default:
459 dev_err(yas5xx->dev, "unknown device type\n");
460 return val;
463 * Linearization formula:
465 * x' = x - (3721 + 50 * f) + (xoffset - r) * c
467 * Where f and r are calibration values, c is a per-device
468 * and sometimes per-axis coefficient.
470 return val - (3721 + 50 * c->f[axis]) +
471 (yas5xx->hard_offsets[axis] - c->r[axis]) * coef;
474 static s32 yas5xx_calc_temperature(struct yas5xx *yas5xx, u16 t)
476 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
477 s32 to;
478 u16 t_ref;
479 s16 min_temp_x10;
480 int ref_temp_x10;
482 t_ref = ci->t_ref;
483 min_temp_x10 = ci->min_temp_x10;
484 ref_temp_x10 = 200;
486 to = (min_temp_x10 + ((ref_temp_x10 - min_temp_x10) * t / t_ref)) * 100;
487 return to;
491 * yas530_get_measure() - Measure a sample of all axis and process
492 * @yas5xx: The device state
493 * @to: Temperature out
494 * @xo: X axis out
495 * @yo: Y axis out
496 * @zo: Z axis out
497 * @return: 0 on success or error code
499 * Used by YAS530, YAS532 and YAS533.
501 static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
503 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
504 struct yas5xx_calibration *c = &yas5xx->calibration;
505 u16 t_ref, t_comp, t, x, y1, y2;
506 /* These are signed x, signed y1 etc */
507 s32 sx, sy1, sy2, sy, sz;
508 int ret;
510 /* We first get raw data that needs to be translated to [x,y,z] */
511 ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
512 if (ret)
513 return ret;
515 /* Do some linearization if available */
516 sx = yas530_linearize(yas5xx, x, 0);
517 sy1 = yas530_linearize(yas5xx, y1, 1);
518 sy2 = yas530_linearize(yas5xx, y2, 2);
521 * Set the temperature for compensation (unit: counts):
522 * YAS532/YAS533 version AC uses the temperature deviation as a
523 * multiplier. YAS530 and YAS532 version AB use solely the t value.
525 t_ref = ci->t_ref;
526 if (ci->devid == YAS532_DEVICE_ID &&
527 yas5xx->version == YAS532_VERSION_AC) {
528 t_comp = t - t_ref;
529 } else {
530 t_comp = t;
534 * Temperature compensation for x, y1, y2 respectively:
536 * Cx * t_comp
537 * x' = x - -----------
538 * 100
540 sx = sx - (c->Cx * t_comp) / 100;
541 sy1 = sy1 - (c->Cy1 * t_comp) / 100;
542 sy2 = sy2 - (c->Cy2 * t_comp) / 100;
545 * Break y1 and y2 into y and z, y1 and y2 are apparently encoding
546 * y and z.
548 sy = sy1 - sy2;
549 sz = -sy1 - sy2;
551 /* Calculate temperature readout */
552 *to = yas5xx_calc_temperature(yas5xx, t);
555 * Calibrate [x,y,z] with some formulas like this:
557 * 100 * x + a_2 * y + a_3 * z
558 * x' = k * ---------------------------
559 * 10
561 * a_4 * x + a_5 * y + a_6 * z
562 * y' = k * ---------------------------
563 * 10
565 * a_7 * x + a_8 * y + a_9 * z
566 * z' = k * ---------------------------
567 * 10
569 *xo = c->k * ((100 * sx + c->a2 * sy + c->a3 * sz) / 10);
570 *yo = c->k * ((c->a4 * sx + c->a5 * sy + c->a6 * sz) / 10);
571 *zo = c->k * ((c->a7 * sx + c->a8 * sy + c->a9 * sz) / 10);
573 return 0;
577 * yas537_get_measure() - Measure a sample of all axis and process
578 * @yas5xx: The device state
579 * @to: Temperature out
580 * @xo: X axis out
581 * @yo: Y axis out
582 * @zo: Z axis out
583 * @return: 0 on success or error code
585 static int yas537_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
587 u16 t, x, y1, y2;
588 int ret;
590 /* We first get raw data that needs to be translated to [x,y,z] */
591 ret = yas537_measure(yas5xx, &t, &x, &y1, &y2);
592 if (ret)
593 return ret;
595 /* Calculate temperature readout */
596 *to = yas5xx_calc_temperature(yas5xx, t);
599 * Unfortunately, no linearization or temperature compensation formulas
600 * are known for YAS537.
603 /* Calculate x, y, z from x, y1, y2 */
604 *xo = (x - BIT(13)) * 300;
605 *yo = (y1 - y2) * 1732 / 10;
606 *zo = (-y1 - y2 + BIT(14)) * 300;
608 return 0;
611 static int yas5xx_read_raw(struct iio_dev *indio_dev,
612 struct iio_chan_spec const *chan,
613 int *val, int *val2,
614 long mask)
616 struct yas5xx *yas5xx = iio_priv(indio_dev);
617 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
618 s32 t, x, y, z;
619 int ret;
621 switch (mask) {
622 case IIO_CHAN_INFO_PROCESSED:
623 case IIO_CHAN_INFO_RAW:
624 pm_runtime_get_sync(yas5xx->dev);
625 ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
626 pm_runtime_mark_last_busy(yas5xx->dev);
627 pm_runtime_put_autosuspend(yas5xx->dev);
628 if (ret)
629 return ret;
630 switch (chan->address) {
631 case 0:
632 *val = t;
633 break;
634 case 1:
635 *val = x;
636 break;
637 case 2:
638 *val = y;
639 break;
640 case 3:
641 *val = z;
642 break;
643 default:
644 dev_err(yas5xx->dev, "unknown channel\n");
645 return -EINVAL;
647 return IIO_VAL_INT;
648 case IIO_CHAN_INFO_SCALE:
649 *val = 1;
650 *val2 = ci->scaling_val2;
651 return IIO_VAL_FRACTIONAL;
652 default:
653 /* Unknown request */
654 return -EINVAL;
658 static void yas5xx_fill_buffer(struct iio_dev *indio_dev)
660 struct yas5xx *yas5xx = iio_priv(indio_dev);
661 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
662 s32 t, x, y, z;
663 int ret;
665 pm_runtime_get_sync(yas5xx->dev);
666 ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
667 pm_runtime_mark_last_busy(yas5xx->dev);
668 pm_runtime_put_autosuspend(yas5xx->dev);
669 if (ret) {
670 dev_err(yas5xx->dev, "error refilling buffer\n");
671 return;
673 yas5xx->scan.channels[0] = t;
674 yas5xx->scan.channels[1] = x;
675 yas5xx->scan.channels[2] = y;
676 yas5xx->scan.channels[3] = z;
677 iio_push_to_buffers_with_timestamp(indio_dev, &yas5xx->scan,
678 iio_get_time_ns(indio_dev));
681 static irqreturn_t yas5xx_handle_trigger(int irq, void *p)
683 const struct iio_poll_func *pf = p;
684 struct iio_dev *indio_dev = pf->indio_dev;
686 yas5xx_fill_buffer(indio_dev);
687 iio_trigger_notify_done(indio_dev->trig);
689 return IRQ_HANDLED;
693 static const struct iio_mount_matrix *
694 yas5xx_get_mount_matrix(const struct iio_dev *indio_dev,
695 const struct iio_chan_spec *chan)
697 struct yas5xx *yas5xx = iio_priv(indio_dev);
699 return &yas5xx->orientation;
702 static const struct iio_chan_spec_ext_info yas5xx_ext_info[] = {
703 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, yas5xx_get_mount_matrix),
707 #define YAS5XX_AXIS_CHANNEL(axis, index) \
709 .type = IIO_MAGN, \
710 .modified = 1, \
711 .channel2 = IIO_MOD_##axis, \
712 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
713 BIT(IIO_CHAN_INFO_SCALE), \
714 .ext_info = yas5xx_ext_info, \
715 .address = index, \
716 .scan_index = index, \
717 .scan_type = { \
718 .sign = 's', \
719 .realbits = 32, \
720 .storagebits = 32, \
721 .endianness = IIO_CPU, \
722 }, \
725 static const struct iio_chan_spec yas5xx_channels[] = {
727 .type = IIO_TEMP,
728 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
729 .address = 0,
730 .scan_index = 0,
731 .scan_type = {
732 .sign = 's',
733 .realbits = 32,
734 .storagebits = 32,
735 .endianness = IIO_CPU,
738 YAS5XX_AXIS_CHANNEL(X, 1),
739 YAS5XX_AXIS_CHANNEL(Y, 2),
740 YAS5XX_AXIS_CHANNEL(Z, 3),
741 IIO_CHAN_SOFT_TIMESTAMP(4),
744 static const unsigned long yas5xx_scan_masks[] = { GENMASK(3, 0), 0 };
746 static const struct iio_info yas5xx_info = {
747 .read_raw = &yas5xx_read_raw,
750 static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg)
752 struct iio_dev *indio_dev = dev_get_drvdata(dev);
753 struct yas5xx *yas5xx = iio_priv(indio_dev);
754 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
755 int reg_qty;
756 int i;
758 if (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8)
759 return true;
762 * YAS versions share different registers on the same address,
763 * need to differentiate.
765 reg_qty = ci->volatile_reg_qty;
766 for (i = 0; i < reg_qty; i++) {
767 if (reg == ci->volatile_reg[i])
768 return true;
771 return false;
774 /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
775 static const struct regmap_config yas5xx_regmap_config = {
776 .reg_bits = 8,
777 .val_bits = 8,
778 .max_register = 0xff,
779 .volatile_reg = yas5xx_volatile_reg,
783 * yas530_extract_calibration() - extracts the a2-a9 and k calibration
784 * @data: the bitfield to use
785 * @c: the calibration to populate
787 * Used by YAS530, YAS532 and YAS533.
789 static void yas530_extract_calibration(u8 *data, struct yas5xx_calibration *c)
791 u64 val = get_unaligned_be64(data);
794 * Bitfield layout for the axis calibration data, for factor
795 * a2 = 2 etc, k = k, c = clock divider
797 * n 7 6 5 4 3 2 1 0
798 * 0 [ 2 2 2 2 2 2 3 3 ] bits 63 .. 56
799 * 1 [ 3 3 4 4 4 4 4 4 ] bits 55 .. 48
800 * 2 [ 5 5 5 5 5 5 6 6 ] bits 47 .. 40
801 * 3 [ 6 6 6 6 7 7 7 7 ] bits 39 .. 32
802 * 4 [ 7 7 7 8 8 8 8 8 ] bits 31 .. 24
803 * 5 [ 8 9 9 9 9 9 9 9 ] bits 23 .. 16
804 * 6 [ 9 k k k k k c c ] bits 15 .. 8
805 * 7 [ c x x x x x x x ] bits 7 .. 0
807 c->a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32;
808 c->a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8;
809 c->a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32;
810 c->a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38;
811 c->a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32;
812 c->a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64;
813 c->a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32;
814 c->a9 = FIELD_GET(GENMASK_ULL(22, 15), val);
815 c->k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10;
816 c->dck = FIELD_GET(GENMASK_ULL(9, 7), val);
819 static int yas530_get_calibration_data(struct yas5xx *yas5xx)
821 struct yas5xx_calibration *c = &yas5xx->calibration;
822 u8 data[16];
823 u32 val;
824 int ret;
826 /* Dummy read, first read is ALWAYS wrong */
827 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
828 if (ret)
829 return ret;
831 /* Actual calibration readout */
832 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
833 if (ret)
834 return ret;
835 dev_dbg(yas5xx->dev, "calibration data: %16ph\n", data);
837 /* Contribute calibration data to the input pool for kernel entropy */
838 add_device_randomness(data, sizeof(data));
840 /* Extract version */
841 yas5xx->version = data[15] & GENMASK(1, 0);
843 /* Extract the calibration from the bitfield */
844 c->Cx = data[0] * 6 - 768;
845 c->Cy1 = data[1] * 6 - 768;
846 c->Cy2 = data[2] * 6 - 768;
847 yas530_extract_calibration(&data[3], c);
850 * Extract linearization:
851 * Linearization layout in the 32 bits at byte 11:
852 * The r factors are 6 bit values where bit 5 is the sign
854 * n 7 6 5 4 3 2 1 0
855 * 0 [ xx xx xx r0 r0 r0 r0 r0 ] bits 31 .. 24
856 * 1 [ r0 f0 f0 r1 r1 r1 r1 r1 ] bits 23 .. 16
857 * 2 [ r1 f1 f1 r2 r2 r2 r2 r2 ] bits 15 .. 8
858 * 3 [ r2 f2 f2 xx xx xx xx xx ] bits 7 .. 0
860 val = get_unaligned_be32(&data[11]);
861 c->f[0] = FIELD_GET(GENMASK(22, 21), val);
862 c->f[1] = FIELD_GET(GENMASK(14, 13), val);
863 c->f[2] = FIELD_GET(GENMASK(6, 5), val);
864 c->r[0] = sign_extend32(FIELD_GET(GENMASK(28, 23), val), 5);
865 c->r[1] = sign_extend32(FIELD_GET(GENMASK(20, 15), val), 5);
866 c->r[2] = sign_extend32(FIELD_GET(GENMASK(12, 7), val), 5);
868 return 0;
871 static int yas532_get_calibration_data(struct yas5xx *yas5xx)
873 struct yas5xx_calibration *c = &yas5xx->calibration;
874 u8 data[14];
875 u32 val;
876 int ret;
878 /* Dummy read, first read is ALWAYS wrong */
879 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
880 if (ret)
881 return ret;
882 /* Actual calibration readout */
883 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
884 if (ret)
885 return ret;
886 dev_dbg(yas5xx->dev, "calibration data: %14ph\n", data);
888 /* Sanity check, is this all zeroes? */
889 if (!memchr_inv(data, 0x00, 13) && !(data[13] & BIT(7)))
890 dev_warn(yas5xx->dev, "calibration is blank!\n");
892 /* Contribute calibration data to the input pool for kernel entropy */
893 add_device_randomness(data, sizeof(data));
895 /* Only one bit of version info reserved here as far as we know */
896 yas5xx->version = data[13] & BIT(0);
898 /* Extract calibration from the bitfield */
899 c->Cx = data[0] * 10 - 1280;
900 c->Cy1 = data[1] * 10 - 1280;
901 c->Cy2 = data[2] * 10 - 1280;
902 yas530_extract_calibration(&data[3], c);
905 * Extract linearization:
906 * Linearization layout in the 32 bits at byte 10:
907 * The r factors are 6 bit values where bit 5 is the sign
909 * n 7 6 5 4 3 2 1 0
910 * 0 [ xx r0 r0 r0 r0 r0 r0 f0 ] bits 31 .. 24
911 * 1 [ f0 r1 r1 r1 r1 r1 r1 f1 ] bits 23 .. 16
912 * 2 [ f1 r2 r2 r2 r2 r2 r2 f2 ] bits 15 .. 8
913 * 3 [ f2 xx xx xx xx xx xx xx ] bits 7 .. 0
915 val = get_unaligned_be32(&data[10]);
916 c->f[0] = FIELD_GET(GENMASK(24, 23), val);
917 c->f[1] = FIELD_GET(GENMASK(16, 15), val);
918 c->f[2] = FIELD_GET(GENMASK(8, 7), val);
919 c->r[0] = sign_extend32(FIELD_GET(GENMASK(30, 25), val), 5);
920 c->r[1] = sign_extend32(FIELD_GET(GENMASK(22, 17), val), 5);
921 c->r[2] = sign_extend32(FIELD_GET(GENMASK(14, 7), val), 5);
923 return 0;
926 static int yas537_get_calibration_data(struct yas5xx *yas5xx)
928 struct yas5xx_calibration *c = &yas5xx->calibration;
929 u8 data[17];
930 u32 val1, val2, val3, val4;
931 int i, ret;
933 /* Writing SRST register */
934 ret = regmap_write(yas5xx->map, YAS537_SRST, BIT(1));
935 if (ret)
936 return ret;
938 /* Calibration readout, YAS537 needs one readout only */
939 ret = regmap_bulk_read(yas5xx->map, YAS537_CAL, data, sizeof(data));
940 if (ret)
941 return ret;
942 dev_dbg(yas5xx->dev, "calibration data: %17ph\n", data);
944 /* Sanity check, is this all zeroes? */
945 if (!memchr_inv(data, 0x00, 16) && !FIELD_GET(GENMASK(5, 0), data[16]))
946 dev_warn(yas5xx->dev, "calibration is blank!\n");
948 /* Contribute calibration data to the input pool for kernel entropy */
949 add_device_randomness(data, sizeof(data));
951 /* Extract version information */
952 yas5xx->version = FIELD_GET(GENMASK(7, 6), data[16]);
954 /* There are two versions of YAS537 behaving differently */
955 switch (yas5xx->version) {
956 case YAS537_VERSION_0:
958 * The first version simply writes data back into registers:
960 * data[0] YAS537_MTC 0x93
961 * data[1] 0x94
962 * data[2] 0x95
963 * data[3] 0x96
964 * data[4] 0x97
965 * data[5] 0x98
966 * data[6] 0x99
967 * data[7] 0x9a
968 * data[8] 0x9b
969 * data[9] 0x9c
970 * data[10] 0x9d
971 * data[11] YAS537_OC 0x9e
973 * data[12] YAS537_OFFSET_X 0x84
974 * data[13] YAS537_OFFSET_Y1 0x85
975 * data[14] YAS537_OFFSET_Y2 0x86
977 * data[15] YAS537_HCK 0x88
978 * data[16] YAS537_LCK 0x89
980 for (i = 0; i < 12; i++) {
981 ret = regmap_write(yas5xx->map, YAS537_MTC + i,
982 data[i]);
983 if (ret)
984 return ret;
986 for (i = 0; i < 3; i++) {
987 ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
988 data[i + 12]);
989 if (ret)
990 return ret;
991 yas5xx->hard_offsets[i] = data[i + 12];
993 for (i = 0; i < 2; i++) {
994 ret = regmap_write(yas5xx->map, YAS537_HCK + i,
995 data[i + 15]);
996 if (ret)
997 return ret;
999 break;
1000 case YAS537_VERSION_1:
1002 * The second version writes some data into registers but also
1003 * extracts calibration coefficients.
1005 * Registers being written:
1007 * data[0] YAS537_MTC 0x93
1008 * data[1] YAS537_MTC+1 0x94
1009 * data[2] YAS537_MTC+2 0x95
1010 * data[3] YAS537_MTC+3 (partially) 0x96
1012 * data[12] YAS537_OFFSET_X 0x84
1013 * data[13] YAS537_OFFSET_Y1 0x85
1014 * data[14] YAS537_OFFSET_Y2 0x86
1016 * data[15] YAS537_HCK (partially) 0x88
1017 * YAS537_LCK (partially) 0x89
1018 * data[16] YAS537_OC (partially) 0x9e
1020 for (i = 0; i < 3; i++) {
1021 ret = regmap_write(yas5xx->map, YAS537_MTC + i,
1022 data[i]);
1023 if (ret)
1024 return ret;
1026 for (i = 0; i < 3; i++) {
1027 ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
1028 data[i + 12]);
1029 if (ret)
1030 return ret;
1031 yas5xx->hard_offsets[i] = data[i + 12];
1034 * Visualization of partially taken data:
1036 * data[3] n 7 6 5 4 3 2 1 0
1037 * YAS537_MTC+3 x x x 1 0 0 0 0
1039 * data[15] n 7 6 5 4 3 2 1 0
1040 * YAS537_HCK x x x x 0
1042 * data[15] n 7 6 5 4 3 2 1 0
1043 * YAS537_LCK x x x x 0
1045 * data[16] n 7 6 5 4 3 2 1 0
1046 * YAS537_OC x x x x x x
1048 ret = regmap_write(yas5xx->map, YAS537_MTC + 3,
1049 FIELD_PREP(YAS537_MTC3_MASK_PREP,
1050 FIELD_GET(YAS537_MTC3_MASK_GET, data[3])) |
1051 YAS537_MTC3_ADD_BIT);
1052 if (ret)
1053 return ret;
1054 ret = regmap_write(yas5xx->map, YAS537_HCK,
1055 FIELD_PREP(YAS537_HCK_MASK_PREP,
1056 FIELD_GET(YAS537_HCK_MASK_GET, data[15])));
1057 if (ret)
1058 return ret;
1059 ret = regmap_write(yas5xx->map, YAS537_LCK,
1060 FIELD_PREP(YAS537_LCK_MASK_PREP,
1061 FIELD_GET(YAS537_LCK_MASK_GET, data[15])));
1062 if (ret)
1063 return ret;
1064 ret = regmap_write(yas5xx->map, YAS537_OC,
1065 FIELD_GET(YAS537_OC_MASK_GET, data[16]));
1066 if (ret)
1067 return ret;
1069 * For data extraction, build some blocks. Four 32-bit blocks
1070 * look appropriate.
1072 * n 7 6 5 4 3 2 1 0
1073 * data[0] 0 [ Cx Cx Cx Cx Cx Cx Cx Cx ] bits 31 .. 24
1074 * data[1] 1 [ Cx C1 C1 C1 C1 C1 C1 C1 ] bits 23 .. 16
1075 * data[2] 2 [ C1 C1 C2 C2 C2 C2 C2 C2 ] bits 15 .. 8
1076 * data[3] 3 [ C2 C2 C2 ] bits 7 .. 0
1078 * n 7 6 5 4 3 2 1 0
1079 * data[3] 0 [ a2 a2 a2 a2 a2 ] bits 31 .. 24
1080 * data[4] 1 [ a2 a2 a3 a3 a3 a3 a3 a3 ] bits 23 .. 16
1081 * data[5] 2 [ a3 a4 a4 a4 a4 a4 a4 a4 ] bits 15 .. 8
1082 * data[6] 3 [ a4 ] bits 7 .. 0
1084 * n 7 6 5 4 3 2 1 0
1085 * data[6] 0 [ a5 a5 a5 a5 a5 a5 a5 ] bits 31 .. 24
1086 * data[7] 1 [ a5 a5 a6 a6 a6 a6 a6 a6 ] bits 23 .. 16
1087 * data[8] 2 [ a6 a7 a7 a7 a7 a7 a7 a7 ] bits 15 .. 8
1088 * data[9] 3 [ a7 ] bits 7 .. 0
1090 * n 7 6 5 4 3 2 1 0
1091 * data[9] 0 [ a8 a8 a8 a8 a8 a8 a8 ] bits 31 .. 24
1092 * data[10] 1 [ a9 a9 a9 a9 a9 a9 a9 a9 ] bits 23 .. 16
1093 * data[11] 2 [ a9 k k k k k k k ] bits 15 .. 8
1094 * data[12] 3 [ ] bits 7 .. 0
1096 val1 = get_unaligned_be32(&data[0]);
1097 val2 = get_unaligned_be32(&data[3]);
1098 val3 = get_unaligned_be32(&data[6]);
1099 val4 = get_unaligned_be32(&data[9]);
1100 /* Extract calibration coefficients and modify */
1101 c->Cx = FIELD_GET(GENMASK(31, 23), val1) - 256;
1102 c->Cy1 = FIELD_GET(GENMASK(22, 14), val1) - 256;
1103 c->Cy2 = FIELD_GET(GENMASK(13, 5), val1) - 256;
1104 c->a2 = FIELD_GET(GENMASK(28, 22), val2) - 64;
1105 c->a3 = FIELD_GET(GENMASK(21, 15), val2) - 64;
1106 c->a4 = FIELD_GET(GENMASK(14, 7), val2) - 128;
1107 c->a5 = FIELD_GET(GENMASK(30, 22), val3) - 112;
1108 c->a6 = FIELD_GET(GENMASK(21, 15), val3) - 64;
1109 c->a7 = FIELD_GET(GENMASK(14, 7), val3) - 128;
1110 c->a8 = FIELD_GET(GENMASK(30, 24), val4) - 64;
1111 c->a9 = FIELD_GET(GENMASK(23, 15), val4) - 112;
1112 c->k = FIELD_GET(GENMASK(14, 8), val4);
1113 break;
1114 default:
1115 dev_err(yas5xx->dev, "unknown version of YAS537\n");
1116 return -EINVAL;
1119 return 0;
1122 /* Used by YAS530, YAS532 and YAS533 */
1123 static void yas530_dump_calibration(struct yas5xx *yas5xx)
1125 struct yas5xx_calibration *c = &yas5xx->calibration;
1127 dev_dbg(yas5xx->dev, "f[] = [%d, %d, %d]\n",
1128 c->f[0], c->f[1], c->f[2]);
1129 dev_dbg(yas5xx->dev, "r[] = [%d, %d, %d]\n",
1130 c->r[0], c->r[1], c->r[2]);
1131 dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx);
1132 dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1);
1133 dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2);
1134 dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2);
1135 dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3);
1136 dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4);
1137 dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5);
1138 dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6);
1139 dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7);
1140 dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8);
1141 dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9);
1142 dev_dbg(yas5xx->dev, "k = %d\n", c->k);
1143 dev_dbg(yas5xx->dev, "dck = %d\n", c->dck);
1146 static void yas537_dump_calibration(struct yas5xx *yas5xx)
1148 struct yas5xx_calibration *c = &yas5xx->calibration;
1150 if (yas5xx->version == YAS537_VERSION_1) {
1151 dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx);
1152 dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1);
1153 dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2);
1154 dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2);
1155 dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3);
1156 dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4);
1157 dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5);
1158 dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6);
1159 dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7);
1160 dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8);
1161 dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9);
1162 dev_dbg(yas5xx->dev, "k = %d\n", c->k);
1166 /* Used by YAS530, YAS532 and YAS533 */
1167 static int yas530_set_offsets(struct yas5xx *yas5xx, s8 ox, s8 oy1, s8 oy2)
1169 int ret;
1171 ret = regmap_write(yas5xx->map, YAS530_OFFSET_X, ox);
1172 if (ret)
1173 return ret;
1174 ret = regmap_write(yas5xx->map, YAS530_OFFSET_Y1, oy1);
1175 if (ret)
1176 return ret;
1177 return regmap_write(yas5xx->map, YAS530_OFFSET_Y2, oy2);
1180 /* Used by YAS530, YAS532 and YAS533 */
1181 static s8 yas530_adjust_offset(s8 old, int bit, u16 center, u16 measure)
1183 if (measure > center)
1184 return old + BIT(bit);
1185 if (measure < center)
1186 return old - BIT(bit);
1187 return old;
1190 /* Used by YAS530, YAS532 and YAS533 */
1191 static int yas530_measure_offsets(struct yas5xx *yas5xx)
1193 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
1194 int ret;
1195 u16 center;
1196 u16 t, x, y1, y2;
1197 s8 ox, oy1, oy2;
1198 int i;
1200 /* Actuate the init coil and measure offsets */
1201 ret = regmap_write(yas5xx->map, YAS530_ACTUATE_INIT_COIL, 0);
1202 if (ret)
1203 return ret;
1205 /* When the initcoil is active this should be around the center */
1206 switch (ci->devid) {
1207 case YAS530_DEVICE_ID:
1208 center = YAS530_DATA_CENTER;
1209 break;
1210 case YAS532_DEVICE_ID:
1211 center = YAS532_DATA_CENTER;
1212 break;
1213 default:
1214 dev_err(yas5xx->dev, "unknown device type\n");
1215 return -EINVAL;
1219 * We set offsets in the interval +-31 by iterating
1220 * +-16, +-8, +-4, +-2, +-1 adjusting the offsets each
1221 * time, then writing the final offsets into the
1222 * registers.
1224 * NOTE: these offsets are NOT in the same unit or magnitude
1225 * as the values for [x, y1, y2]. The value is +/-31
1226 * but the effect on the raw values is much larger.
1227 * The effect of the offset is to bring the measure
1228 * rougly to the center.
1230 ox = 0;
1231 oy1 = 0;
1232 oy2 = 0;
1234 for (i = 4; i >= 0; i--) {
1235 ret = yas530_set_offsets(yas5xx, ox, oy1, oy2);
1236 if (ret)
1237 return ret;
1239 ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
1240 if (ret)
1241 return ret;
1242 dev_dbg(yas5xx->dev, "measurement %d: x=%d, y1=%d, y2=%d\n",
1243 5-i, x, y1, y2);
1245 ox = yas530_adjust_offset(ox, i, center, x);
1246 oy1 = yas530_adjust_offset(oy1, i, center, y1);
1247 oy2 = yas530_adjust_offset(oy2, i, center, y2);
1250 /* Needed for calibration algorithm */
1251 yas5xx->hard_offsets[0] = ox;
1252 yas5xx->hard_offsets[1] = oy1;
1253 yas5xx->hard_offsets[2] = oy2;
1254 ret = yas530_set_offsets(yas5xx, ox, oy1, oy2);
1255 if (ret)
1256 return ret;
1258 dev_info(yas5xx->dev, "discovered hard offsets: x=%d, y1=%d, y2=%d\n",
1259 ox, oy1, oy2);
1260 return 0;
1263 /* Used by YAS530, YAS532 and YAS533 */
1264 static int yas530_power_on(struct yas5xx *yas5xx)
1266 unsigned int val;
1267 int ret;
1269 /* Zero the test registers */
1270 ret = regmap_write(yas5xx->map, YAS530_TEST1, 0);
1271 if (ret)
1272 return ret;
1273 ret = regmap_write(yas5xx->map, YAS530_TEST2, 0);
1274 if (ret)
1275 return ret;
1277 /* Set up for no interrupts, calibrated clock divider */
1278 val = FIELD_PREP(YAS5XX_CONFIG_CCK_MASK, yas5xx->calibration.dck);
1279 ret = regmap_write(yas5xx->map, YAS530_CONFIG, val);
1280 if (ret)
1281 return ret;
1283 /* Measure interval 0 (back-to-back?) */
1284 return regmap_write(yas5xx->map, YAS530_MEASURE_INTERVAL, 0);
1287 static int yas537_power_on(struct yas5xx *yas5xx)
1289 __be16 buf;
1290 int ret;
1291 u8 intrvl;
1293 /* Writing ADCCAL and TRM registers */
1294 buf = cpu_to_be16(GENMASK(9, 3));
1295 ret = regmap_bulk_write(yas5xx->map, YAS537_ADCCAL, &buf, sizeof(buf));
1296 if (ret)
1297 return ret;
1298 ret = regmap_write(yas5xx->map, YAS537_TRM, GENMASK(7, 0));
1299 if (ret)
1300 return ret;
1302 /* The interval value is static in regular operation */
1303 intrvl = (YAS537_DEFAULT_SENSOR_DELAY_MS * MILLI
1304 - YAS537_MEASURE_TIME_WORST_US) / 4100;
1305 ret = regmap_write(yas5xx->map, YAS537_MEASURE_INTERVAL, intrvl);
1306 if (ret)
1307 return ret;
1309 /* The average value is also static in regular operation */
1310 ret = regmap_write(yas5xx->map, YAS537_AVR, YAS537_MAG_AVERAGE_32_MASK);
1311 if (ret)
1312 return ret;
1314 /* Perform the "rcoil" part but skip the "last_after_rcoil" read */
1315 ret = regmap_write(yas5xx->map, YAS537_CONFIG, BIT(3));
1316 if (ret)
1317 return ret;
1319 /* Wait until the coil has ramped up */
1320 usleep_range(YAS537_MAG_RCOIL_TIME_US, YAS537_MAG_RCOIL_TIME_US + 100);
1322 return 0;
1325 static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
1326 [yas530] = {
1327 .devid = YAS530_DEVICE_ID,
1328 .product_name = "YAS530 MS-3E",
1329 .version_names = { "A", "B" },
1330 .volatile_reg = yas530_volatile_reg,
1331 .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
1332 .scaling_val2 = 100000000, /* picotesla to Gauss */
1333 .t_ref = 182, /* counts */
1334 .min_temp_x10 = -620, /* 1/10:s degrees Celsius */
1335 .get_measure = yas530_get_measure,
1336 .get_calibration_data = yas530_get_calibration_data,
1337 .dump_calibration = yas530_dump_calibration,
1338 .measure_offsets = yas530_measure_offsets,
1339 .power_on = yas530_power_on,
1341 [yas532] = {
1342 .devid = YAS532_DEVICE_ID,
1343 .product_name = "YAS532 MS-3R",
1344 .version_names = { "AB", "AC" },
1345 .volatile_reg = yas530_volatile_reg,
1346 .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
1347 .scaling_val2 = 100000, /* nanotesla to Gauss */
1348 .t_ref = 390, /* counts */
1349 .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
1350 .get_measure = yas530_get_measure,
1351 .get_calibration_data = yas532_get_calibration_data,
1352 .dump_calibration = yas530_dump_calibration,
1353 .measure_offsets = yas530_measure_offsets,
1354 .power_on = yas530_power_on,
1356 [yas533] = {
1357 .devid = YAS532_DEVICE_ID,
1358 .product_name = "YAS533 MS-3F",
1359 .version_names = { "AB", "AC" },
1360 .volatile_reg = yas530_volatile_reg,
1361 .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
1362 .scaling_val2 = 100000, /* nanotesla to Gauss */
1363 .t_ref = 390, /* counts */
1364 .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
1365 .get_measure = yas530_get_measure,
1366 .get_calibration_data = yas532_get_calibration_data,
1367 .dump_calibration = yas530_dump_calibration,
1368 .measure_offsets = yas530_measure_offsets,
1369 .power_on = yas530_power_on,
1371 [yas537] = {
1372 .devid = YAS537_DEVICE_ID,
1373 .product_name = "YAS537 MS-3T",
1374 .version_names = { "v0", "v1" }, /* version naming unknown */
1375 .volatile_reg = yas537_volatile_reg,
1376 .volatile_reg_qty = ARRAY_SIZE(yas537_volatile_reg),
1377 .scaling_val2 = 100000, /* nanotesla to Gauss */
1378 .t_ref = 8120, /* counts */
1379 .min_temp_x10 = -3860, /* 1/10:s degrees Celsius */
1380 .get_measure = yas537_get_measure,
1381 .get_calibration_data = yas537_get_calibration_data,
1382 .dump_calibration = yas537_dump_calibration,
1383 /* .measure_offets is not needed for yas537 */
1384 .power_on = yas537_power_on,
1388 static int yas5xx_probe(struct i2c_client *i2c)
1390 const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
1391 struct iio_dev *indio_dev;
1392 struct device *dev = &i2c->dev;
1393 struct yas5xx *yas5xx;
1394 const struct yas5xx_chip_info *ci;
1395 int id_check;
1396 int ret;
1398 indio_dev = devm_iio_device_alloc(dev, sizeof(*yas5xx));
1399 if (!indio_dev)
1400 return -ENOMEM;
1402 yas5xx = iio_priv(indio_dev);
1403 i2c_set_clientdata(i2c, indio_dev);
1404 yas5xx->dev = dev;
1405 mutex_init(&yas5xx->lock);
1407 ret = iio_read_mount_matrix(dev, &yas5xx->orientation);
1408 if (ret)
1409 return ret;
1411 yas5xx->regs[0].supply = "vdd";
1412 yas5xx->regs[1].supply = "iovdd";
1413 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(yas5xx->regs),
1414 yas5xx->regs);
1415 if (ret)
1416 return dev_err_probe(dev, ret, "cannot get regulators\n");
1418 ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1419 if (ret)
1420 return dev_err_probe(dev, ret, "cannot enable regulators\n");
1422 /* See comment in runtime resume callback */
1423 usleep_range(31000, 40000);
1425 /* This will take the device out of reset if need be */
1426 yas5xx->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1427 if (IS_ERR(yas5xx->reset)) {
1428 ret = dev_err_probe(dev, PTR_ERR(yas5xx->reset), "failed to get reset line\n");
1429 goto reg_off;
1432 yas5xx->map = devm_regmap_init_i2c(i2c, &yas5xx_regmap_config);
1433 if (IS_ERR(yas5xx->map)) {
1434 ret = dev_err_probe(dev, PTR_ERR(yas5xx->map), "failed to allocate register map\n");
1435 goto assert_reset;
1438 ci = i2c_get_match_data(i2c);
1439 yas5xx->chip_info = ci;
1441 ret = regmap_read(yas5xx->map, YAS5XX_DEVICE_ID, &id_check);
1442 if (ret)
1443 goto assert_reset;
1445 if (id_check != ci->devid) {
1446 ret = dev_err_probe(dev, -ENODEV,
1447 "device ID %02x doesn't match %s\n",
1448 id_check, id->name);
1449 goto assert_reset;
1452 ret = ci->get_calibration_data(yas5xx);
1453 if (ret)
1454 goto assert_reset;
1456 dev_info(dev, "detected %s %s\n", ci->product_name,
1457 ci->version_names[yas5xx->version]);
1459 ci->dump_calibration(yas5xx);
1461 ret = ci->power_on(yas5xx);
1462 if (ret)
1463 goto assert_reset;
1465 if (ci->measure_offsets) {
1466 ret = ci->measure_offsets(yas5xx);
1467 if (ret)
1468 goto assert_reset;
1471 indio_dev->info = &yas5xx_info;
1472 indio_dev->available_scan_masks = yas5xx_scan_masks;
1473 indio_dev->modes = INDIO_DIRECT_MODE;
1474 indio_dev->name = id->name;
1475 indio_dev->channels = yas5xx_channels;
1476 indio_dev->num_channels = ARRAY_SIZE(yas5xx_channels);
1478 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1479 yas5xx_handle_trigger,
1480 NULL);
1481 if (ret) {
1482 dev_err_probe(dev, ret, "triggered buffer setup failed\n");
1483 goto assert_reset;
1486 ret = iio_device_register(indio_dev);
1487 if (ret) {
1488 dev_err_probe(dev, ret, "device register failed\n");
1489 goto cleanup_buffer;
1492 /* Take runtime PM online */
1493 pm_runtime_get_noresume(dev);
1494 pm_runtime_set_active(dev);
1495 pm_runtime_enable(dev);
1497 pm_runtime_set_autosuspend_delay(dev, YAS5XX_AUTOSUSPEND_DELAY_MS);
1498 pm_runtime_use_autosuspend(dev);
1499 pm_runtime_put(dev);
1501 return 0;
1503 cleanup_buffer:
1504 iio_triggered_buffer_cleanup(indio_dev);
1505 assert_reset:
1506 gpiod_set_value_cansleep(yas5xx->reset, 1);
1507 reg_off:
1508 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1510 return ret;
1513 static void yas5xx_remove(struct i2c_client *i2c)
1515 struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
1516 struct yas5xx *yas5xx = iio_priv(indio_dev);
1517 struct device *dev = &i2c->dev;
1519 iio_device_unregister(indio_dev);
1520 iio_triggered_buffer_cleanup(indio_dev);
1522 * Now we can't get any more reads from the device, which would
1523 * also call pm_runtime* functions and race with our disable
1524 * code. Disable PM runtime in orderly fashion and power down.
1526 pm_runtime_get_sync(dev);
1527 pm_runtime_put_noidle(dev);
1528 pm_runtime_disable(dev);
1529 gpiod_set_value_cansleep(yas5xx->reset, 1);
1530 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1533 static int yas5xx_runtime_suspend(struct device *dev)
1535 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1536 struct yas5xx *yas5xx = iio_priv(indio_dev);
1538 gpiod_set_value_cansleep(yas5xx->reset, 1);
1539 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1541 return 0;
1544 static int yas5xx_runtime_resume(struct device *dev)
1546 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1547 struct yas5xx *yas5xx = iio_priv(indio_dev);
1548 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
1549 int ret;
1551 ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1552 if (ret) {
1553 dev_err(dev, "cannot enable regulators\n");
1554 return ret;
1558 * The YAS530 datasheet says TVSKW is up to 30 ms, after that 1 ms
1559 * for all voltages to settle. The YAS532 is 10ms then 4ms for the
1560 * I2C to come online. Let's keep it safe and put this at 31ms.
1562 usleep_range(31000, 40000);
1563 gpiod_set_value_cansleep(yas5xx->reset, 0);
1565 ret = ci->power_on(yas5xx);
1566 if (ret) {
1567 dev_err(dev, "cannot power on\n");
1568 goto out_reset;
1571 return 0;
1573 out_reset:
1574 gpiod_set_value_cansleep(yas5xx->reset, 1);
1575 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1577 return ret;
1580 static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops, yas5xx_runtime_suspend,
1581 yas5xx_runtime_resume, NULL);
1583 static const struct i2c_device_id yas5xx_id[] = {
1584 {"yas530", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas530] },
1585 {"yas532", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas532] },
1586 {"yas533", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas533] },
1587 {"yas537", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas537] },
1590 MODULE_DEVICE_TABLE(i2c, yas5xx_id);
1592 static const struct of_device_id yas5xx_of_match[] = {
1593 { .compatible = "yamaha,yas530", &yas5xx_chip_info_tbl[yas530] },
1594 { .compatible = "yamaha,yas532", &yas5xx_chip_info_tbl[yas532] },
1595 { .compatible = "yamaha,yas533", &yas5xx_chip_info_tbl[yas533] },
1596 { .compatible = "yamaha,yas537", &yas5xx_chip_info_tbl[yas537] },
1599 MODULE_DEVICE_TABLE(of, yas5xx_of_match);
1601 static struct i2c_driver yas5xx_driver = {
1602 .driver = {
1603 .name = "yas5xx",
1604 .of_match_table = yas5xx_of_match,
1605 .pm = pm_ptr(&yas5xx_dev_pm_ops),
1607 .probe = yas5xx_probe,
1608 .remove = yas5xx_remove,
1609 .id_table = yas5xx_id,
1611 module_i2c_driver(yas5xx_driver);
1613 MODULE_DESCRIPTION("Yamaha YAS53x 3-axis magnetometer driver");
1614 MODULE_AUTHOR("Linus Walleij");
1615 MODULE_LICENSE("GPL v2");