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:
6 *
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)
15 *
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
19 *
20 * Code functions for YAS537 based on Yamaha Android kernel driver.
21 * Copyright (c) 2014 Yamaha Corporation
22 *
23 * Author: Linus Walleij <linus.walleij@linaro.org>
24 */
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/mutex.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/property.h>
35 #include <linux/regmap.h>
36 #include <linux/regulator/consumer.h>
37 #include <linux/random.h>
38 #include <linux/units.h>
39
40 #include <linux/iio/buffer.h>
41 #include <linux/iio/iio.h>
42 #include <linux/iio/trigger_consumer.h>
43 #include <linux/iio/triggered_buffer.h>
44
45 #include <linux/unaligned.h>
46
47 /* Commonly used registers */
48 #define YAS5XX_DEVICE_ID 0x80
49 #define YAS5XX_MEASURE_DATA 0xB0
50
51 /* These registers are used by YAS530, YAS532 and YAS533 */
52 #define YAS530_ACTUATE_INIT_COIL 0x81
53 #define YAS530_MEASURE 0x82
54 #define YAS530_CONFIG 0x83
55 #define YAS530_MEASURE_INTERVAL 0x84
56 #define YAS530_OFFSET_X 0x85 /* [-31 .. 31] */
57 #define YAS530_OFFSET_Y1 0x86 /* [-31 .. 31] */
58 #define YAS530_OFFSET_Y2 0x87 /* [-31 .. 31] */
59 #define YAS530_TEST1 0x88
60 #define YAS530_TEST2 0x89
61 #define YAS530_CAL 0x90
62
63 /* Registers used by YAS537 */
64 #define YAS537_MEASURE 0x81 /* Originally YAS537_REG_CMDR */
65 #define YAS537_CONFIG 0x82 /* Originally YAS537_REG_CONFR */
66 #define YAS537_MEASURE_INTERVAL 0x83 /* Originally YAS537_REG_INTRVLR */
67 #define YAS537_OFFSET_X 0x84 /* Originally YAS537_REG_OXR */
68 #define YAS537_OFFSET_Y1 0x85 /* Originally YAS537_REG_OY1R */
69 #define YAS537_OFFSET_Y2 0x86 /* Originally YAS537_REG_OY2R */
70 #define YAS537_AVR 0x87
71 #define YAS537_HCK 0x88
72 #define YAS537_LCK 0x89
73 #define YAS537_SRST 0x90
74 #define YAS537_ADCCAL 0x91
75 #define YAS537_MTC 0x93
76 #define YAS537_OC 0x9E
77 #define YAS537_TRM 0x9F
78 #define YAS537_CAL 0xC0
79
80 /* Bits in the YAS5xx config register */
81 #define YAS5XX_CONFIG_INTON BIT(0) /* Interrupt on? */
82 #define YAS5XX_CONFIG_INTHACT BIT(1) /* Interrupt active high? */
83 #define YAS5XX_CONFIG_CCK_MASK GENMASK(4, 2)
84 #define YAS5XX_CONFIG_CCK_SHIFT 2
85
86 /* Bits in the measure command register */
87 #define YAS5XX_MEASURE_START BIT(0)
88 #define YAS5XX_MEASURE_LDTC BIT(1)
89 #define YAS5XX_MEASURE_FORS BIT(2)
90 #define YAS5XX_MEASURE_DLYMES BIT(4)
91 #define YAS5XX_MEASURE_CONT BIT(5)
92
93 /* Bits in the measure data register */
94 #define YAS5XX_MEASURE_DATA_BUSY BIT(7)
95
96 #define YAS530_DEVICE_ID 0x01 /* YAS530 (MS-3E) */
97 #define YAS530_VERSION_A 0 /* YAS530 (MS-3E A) */
98 #define YAS530_VERSION_B 1 /* YAS530B (MS-3E B) */
99 #define YAS530_VERSION_A_COEF 380
100 #define YAS530_VERSION_B_COEF 550
101 #define YAS530_DATA_BITS 12
102 #define YAS530_DATA_CENTER BIT(YAS530_DATA_BITS - 1)
103 #define YAS530_DATA_OVERFLOW (BIT(YAS530_DATA_BITS) - 1)
104
105 #define YAS532_DEVICE_ID 0x02 /* YAS532/YAS533 (MS-3R/F) */
106 #define YAS532_VERSION_AB 0 /* YAS532/533 AB (MS-3R/F AB) */
107 #define YAS532_VERSION_AC 1 /* YAS532/533 AC (MS-3R/F AC) */
108 #define YAS532_VERSION_AB_COEF 1800
109 #define YAS532_VERSION_AC_COEF_X 850
110 #define YAS532_VERSION_AC_COEF_Y1 750
111 #define YAS532_VERSION_AC_COEF_Y2 750
112 #define YAS532_DATA_BITS 13
113 #define YAS532_DATA_CENTER BIT(YAS532_DATA_BITS - 1)
114 #define YAS532_DATA_OVERFLOW (BIT(YAS532_DATA_BITS) - 1)
115
116 #define YAS537_DEVICE_ID 0x07 /* YAS537 (MS-3T) */
117 #define YAS537_VERSION_0 0 /* Version naming unknown */
118 #define YAS537_VERSION_1 1 /* Version naming unknown */
119 #define YAS537_MAG_AVERAGE_32_MASK GENMASK(6, 4)
120 #define YAS537_MEASURE_TIME_WORST_US 1500
121 #define YAS537_DEFAULT_SENSOR_DELAY_MS 50
122 #define YAS537_MAG_RCOIL_TIME_US 65
123 #define YAS537_MTC3_MASK_PREP GENMASK(7, 0)
124 #define YAS537_MTC3_MASK_GET GENMASK(7, 5)
125 #define YAS537_MTC3_ADD_BIT BIT(4)
126 #define YAS537_HCK_MASK_PREP GENMASK(4, 0)
127 #define YAS537_HCK_MASK_GET GENMASK(7, 4)
128 #define YAS537_LCK_MASK_PREP GENMASK(4, 0)
129 #define YAS537_LCK_MASK_GET GENMASK(3, 0)
130 #define YAS537_OC_MASK_GET GENMASK(5, 0)
131
132 /* Turn off device regulators etc after 5 seconds of inactivity */
133 #define YAS5XX_AUTOSUSPEND_DELAY_MS 5000
134
135 enum chip_ids {
136 yas530,
137 yas532,
138 yas533,
139 yas537,
140 };
141
142 static const int yas530_volatile_reg[] = {
143 YAS530_ACTUATE_INIT_COIL,
144 YAS530_MEASURE,
145 };
146
147 static const int yas537_volatile_reg[] = {
148 YAS537_MEASURE,
149 };
150
151 struct yas5xx_calibration {
152 /* Linearization calibration x, y1, y2 */
153 s32 r[3];
154 u32 f[3];
155 /* Temperature compensation calibration */
156 s16 Cx, Cy1, Cy2;
157 /* Misc calibration coefficients */
158 s8 a2, a3, a4, a6, a7, a8;
159 s16 a5, a9;
160 u8 k;
161 /* clock divider */
162 u8 dck;
163 };
164
165 struct yas5xx;
166
167 /**
168 * struct yas5xx_chip_info - device-specific data and function pointers
169 * @devid: device ID number
170 * @product_label: product label used in Linux
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
183 *
184 * The "t_ref" value for YAS532/533 is known from the Android driver.
185 * For YAS530 and YAS537 it was approximately measured.
186 *
187 * The temperatures "min_temp_x10" are derived from the temperature resolutions
188 * given in the data sheets.
189 */
190 struct yas5xx_chip_info {
191 unsigned int devid;
192 const char *product_label;
193 const char *product_name;
194 const char *version_names[2];
195 const int *volatile_reg;
196 int volatile_reg_qty;
197 u32 scaling_val2;
198 u16 t_ref;
199 s16 min_temp_x10;
200 int (*get_measure)(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo);
201 int (*get_calibration_data)(struct yas5xx *yas5xx);
202 void (*dump_calibration)(struct yas5xx *yas5xx);
203 int (*measure_offsets)(struct yas5xx *yas5xx);
204 int (*power_on)(struct yas5xx *yas5xx);
205 };
206
207 /**
208 * struct yas5xx - state container for the YAS5xx driver
209 * @dev: parent device pointer
210 * @chip_info: device-specific data and function pointers
211 * @version: device version
212 * @calibration: calibration settings from the OTP storage
213 * @hard_offsets: offsets for each axis measured with initcoil actuated
214 * @orientation: mounting matrix, flipped axis etc
215 * @map: regmap to access the YAX5xx registers over I2C
216 * @regs: the vdd and vddio power regulators
217 * @reset: optional GPIO line used for handling RESET
218 * @lock: locks the magnetometer for exclusive use during a measurement (which
219 * involves several register transactions so the regmap lock is not enough)
220 * so that measurements get serialized in a first-come-first serve manner
221 * @scan: naturally aligned measurements
222 */
223 struct yas5xx {
224 struct device *dev;
225 const struct yas5xx_chip_info *chip_info;
226 unsigned int version;
227 struct yas5xx_calibration calibration;
228 s8 hard_offsets[3];
229 struct iio_mount_matrix orientation;
230 struct regmap *map;
231 struct regulator_bulk_data regs[2];
232 struct gpio_desc *reset;
233 struct mutex lock;
234 /*
235 * The scanout is 4 x 32 bits in CPU endianness.
236 * Ensure timestamp is naturally aligned
237 */
238 struct {
239 s32 channels[4];
240 aligned_s64 ts;
241 } scan;
242 };
243
244 /* On YAS530 the x, y1 and y2 values are 12 bits */
yas530_extract_axis(u8 * data)245 static u16 yas530_extract_axis(u8 *data)
246 {
247 u16 val;
248
249 /*
250 * These are the bits used in a 16bit word:
251 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
252 * x x x x x x x x x x x x
253 */
254 val = get_unaligned_be16(&data[0]);
255 val = FIELD_GET(GENMASK(14, 3), val);
256 return val;
257 }
258
259 /* On YAS532 the x, y1 and y2 values are 13 bits */
yas532_extract_axis(u8 * data)260 static u16 yas532_extract_axis(u8 *data)
261 {
262 u16 val;
263
264 /*
265 * These are the bits used in a 16bit word:
266 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
267 * x x x x x x x x x x x x x
268 */
269 val = get_unaligned_be16(&data[0]);
270 val = FIELD_GET(GENMASK(14, 2), val);
271 return val;
272 }
273
274 /**
275 * yas530_measure() - Make a measure from the hardware
276 * @yas5xx: The device state
277 * @t: the raw temperature measurement
278 * @x: the raw x axis measurement
279 * @y1: the y1 axis measurement
280 * @y2: the y2 axis measurement
281 * @return: 0 on success or error code
282 *
283 * Used by YAS530, YAS532 and YAS533.
284 */
yas530_measure(struct yas5xx * yas5xx,u16 * t,u16 * x,u16 * y1,u16 * y2)285 static int yas530_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2)
286 {
287 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
288 unsigned int busy;
289 u8 data[8];
290 int ret;
291 u16 val;
292
293 mutex_lock(&yas5xx->lock);
294 ret = regmap_write(yas5xx->map, YAS530_MEASURE, YAS5XX_MEASURE_START);
295 if (ret < 0)
296 goto out_unlock;
297
298 /*
299 * Typical time to measure 1500 us, max 2000 us so wait min 500 us
300 * and at most 20000 us (one magnitude more than the datsheet max)
301 * before timeout.
302 */
303 ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA, busy,
304 !(busy & YAS5XX_MEASURE_DATA_BUSY),
305 500, 20000);
306 if (ret) {
307 dev_err(yas5xx->dev, "timeout waiting for measurement\n");
308 goto out_unlock;
309 }
310
311 ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
312 data, sizeof(data));
313 if (ret)
314 goto out_unlock;
315
316 mutex_unlock(&yas5xx->lock);
317
318 switch (ci->devid) {
319 case YAS530_DEVICE_ID:
320 /*
321 * The t value is 9 bits in big endian format
322 * These are the bits used in a 16bit word:
323 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
324 * x x x x x x x x x
325 */
326 val = get_unaligned_be16(&data[0]);
327 val = FIELD_GET(GENMASK(14, 6), val);
328 *t = val;
329 *x = yas530_extract_axis(&data[2]);
330 *y1 = yas530_extract_axis(&data[4]);
331 *y2 = yas530_extract_axis(&data[6]);
332 break;
333 case YAS532_DEVICE_ID:
334 /*
335 * The t value is 10 bits in big endian format
336 * These are the bits used in a 16bit word:
337 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
338 * x x x x x x x x x x
339 */
340 val = get_unaligned_be16(&data[0]);
341 val = FIELD_GET(GENMASK(14, 5), val);
342 *t = val;
343 *x = yas532_extract_axis(&data[2]);
344 *y1 = yas532_extract_axis(&data[4]);
345 *y2 = yas532_extract_axis(&data[6]);
346 break;
347 default:
348 dev_err(yas5xx->dev, "unknown data format\n");
349 ret = -EINVAL;
350 break;
351 }
352
353 return ret;
354
355 out_unlock:
356 mutex_unlock(&yas5xx->lock);
357 return ret;
358 }
359
360 /**
361 * yas537_measure() - Make a measure from the hardware
362 * @yas5xx: The device state
363 * @t: the raw temperature measurement
364 * @x: the raw x axis measurement
365 * @y1: the y1 axis measurement
366 * @y2: the y2 axis measurement
367 * @return: 0 on success or error code
368 */
yas537_measure(struct yas5xx * yas5xx,u16 * t,u16 * x,u16 * y1,u16 * y2)369 static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2)
370 {
371 struct yas5xx_calibration *c = &yas5xx->calibration;
372 unsigned int busy;
373 u8 data[8];
374 u16 xy1y2[3];
375 s32 h[3], s[3];
376 int half_range = BIT(13);
377 int i, ret;
378
379 mutex_lock(&yas5xx->lock);
380
381 /* Contrary to YAS530/532, also a "cont" bit is set, meaning unknown */
382 ret = regmap_write(yas5xx->map, YAS537_MEASURE, YAS5XX_MEASURE_START |
383 YAS5XX_MEASURE_CONT);
384 if (ret < 0)
385 goto out_unlock;
386
387 /* Use same timeout like YAS530/532 but the bit is in data row 2 */
388 ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA + 2, busy,
389 !(busy & YAS5XX_MEASURE_DATA_BUSY),
390 500, 20000);
391 if (ret) {
392 dev_err(yas5xx->dev, "timeout waiting for measurement\n");
393 goto out_unlock;
394 }
395
396 ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
397 data, sizeof(data));
398 if (ret)
399 goto out_unlock;
400
401 mutex_unlock(&yas5xx->lock);
402
403 *t = get_unaligned_be16(&data[0]);
404 xy1y2[0] = FIELD_GET(GENMASK(13, 0), get_unaligned_be16(&data[2]));
405 xy1y2[1] = get_unaligned_be16(&data[4]);
406 xy1y2[2] = get_unaligned_be16(&data[6]);
407
408 /* The second version of YAS537 needs to include calibration coefficients */
409 if (yas5xx->version == YAS537_VERSION_1) {
410 for (i = 0; i < 3; i++)
411 s[i] = xy1y2[i] - half_range;
412 h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / half_range;
413 h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / half_range;
414 h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / half_range;
415 for (i = 0; i < 3; i++) {
416 h[i] = clamp(h[i], -half_range, half_range - 1);
417 xy1y2[i] = h[i] + half_range;
418 }
419 }
420
421 *x = xy1y2[0];
422 *y1 = xy1y2[1];
423 *y2 = xy1y2[2];
424
425 return 0;
426
427 out_unlock:
428 mutex_unlock(&yas5xx->lock);
429 return ret;
430 }
431
432 /* Used by YAS530, YAS532 and YAS533 */
yas530_linearize(struct yas5xx * yas5xx,u16 val,int axis)433 static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis)
434 {
435 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
436 struct yas5xx_calibration *c = &yas5xx->calibration;
437 static const s32 yas532ac_coef[] = {
438 YAS532_VERSION_AC_COEF_X,
439 YAS532_VERSION_AC_COEF_Y1,
440 YAS532_VERSION_AC_COEF_Y2,
441 };
442 s32 coef;
443
444 /* Select coefficients */
445 switch (ci->devid) {
446 case YAS530_DEVICE_ID:
447 if (yas5xx->version == YAS530_VERSION_A)
448 coef = YAS530_VERSION_A_COEF;
449 else
450 coef = YAS530_VERSION_B_COEF;
451 break;
452 case YAS532_DEVICE_ID:
453 if (yas5xx->version == YAS532_VERSION_AB)
454 coef = YAS532_VERSION_AB_COEF;
455 else
456 /* Elaborate coefficients */
457 coef = yas532ac_coef[axis];
458 break;
459 default:
460 dev_err(yas5xx->dev, "unknown device type\n");
461 return val;
462 }
463 /*
464 * Linearization formula:
465 *
466 * x' = x - (3721 + 50 * f) + (xoffset - r) * c
467 *
468 * Where f and r are calibration values, c is a per-device
469 * and sometimes per-axis coefficient.
470 */
471 return val - (3721 + 50 * c->f[axis]) +
472 (yas5xx->hard_offsets[axis] - c->r[axis]) * coef;
473 }
474
yas5xx_calc_temperature(struct yas5xx * yas5xx,u16 t)475 static s32 yas5xx_calc_temperature(struct yas5xx *yas5xx, u16 t)
476 {
477 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
478 s32 to;
479 u16 t_ref;
480 s16 min_temp_x10;
481 int ref_temp_x10;
482
483 t_ref = ci->t_ref;
484 min_temp_x10 = ci->min_temp_x10;
485 ref_temp_x10 = 200;
486
487 to = (min_temp_x10 + ((ref_temp_x10 - min_temp_x10) * t / t_ref)) * 100;
488 return to;
489 }
490
491 /**
492 * yas530_get_measure() - Measure a sample of all axis and process
493 * @yas5xx: The device state
494 * @to: Temperature out
495 * @xo: X axis out
496 * @yo: Y axis out
497 * @zo: Z axis out
498 * @return: 0 on success or error code
499 *
500 * Used by YAS530, YAS532 and YAS533.
501 */
yas530_get_measure(struct yas5xx * yas5xx,s32 * to,s32 * xo,s32 * yo,s32 * zo)502 static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
503 {
504 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
505 struct yas5xx_calibration *c = &yas5xx->calibration;
506 u16 t_ref, t_comp, t, x, y1, y2;
507 /* These are signed x, signed y1 etc */
508 s32 sx, sy1, sy2, sy, sz;
509 int ret;
510
511 /* We first get raw data that needs to be translated to [x,y,z] */
512 ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
513 if (ret)
514 return ret;
515
516 /* Do some linearization if available */
517 sx = yas530_linearize(yas5xx, x, 0);
518 sy1 = yas530_linearize(yas5xx, y1, 1);
519 sy2 = yas530_linearize(yas5xx, y2, 2);
520
521 /*
522 * Set the temperature for compensation (unit: counts):
523 * YAS532/YAS533 version AC uses the temperature deviation as a
524 * multiplier. YAS530 and YAS532 version AB use solely the t value.
525 */
526 t_ref = ci->t_ref;
527 if (ci->devid == YAS532_DEVICE_ID &&
528 yas5xx->version == YAS532_VERSION_AC) {
529 t_comp = t - t_ref;
530 } else {
531 t_comp = t;
532 }
533
534 /*
535 * Temperature compensation for x, y1, y2 respectively:
536 *
537 * Cx * t_comp
538 * x' = x - -----------
539 * 100
540 */
541 sx = sx - (c->Cx * t_comp) / 100;
542 sy1 = sy1 - (c->Cy1 * t_comp) / 100;
543 sy2 = sy2 - (c->Cy2 * t_comp) / 100;
544
545 /*
546 * Break y1 and y2 into y and z, y1 and y2 are apparently encoding
547 * y and z.
548 */
549 sy = sy1 - sy2;
550 sz = -sy1 - sy2;
551
552 /* Calculate temperature readout */
553 *to = yas5xx_calc_temperature(yas5xx, t);
554
555 /*
556 * Calibrate [x,y,z] with some formulas like this:
557 *
558 * 100 * x + a_2 * y + a_3 * z
559 * x' = k * ---------------------------
560 * 10
561 *
562 * a_4 * x + a_5 * y + a_6 * z
563 * y' = k * ---------------------------
564 * 10
565 *
566 * a_7 * x + a_8 * y + a_9 * z
567 * z' = k * ---------------------------
568 * 10
569 */
570 *xo = c->k * ((100 * sx + c->a2 * sy + c->a3 * sz) / 10);
571 *yo = c->k * ((c->a4 * sx + c->a5 * sy + c->a6 * sz) / 10);
572 *zo = c->k * ((c->a7 * sx + c->a8 * sy + c->a9 * sz) / 10);
573
574 return 0;
575 }
576
577 /**
578 * yas537_get_measure() - Measure a sample of all axis and process
579 * @yas5xx: The device state
580 * @to: Temperature out
581 * @xo: X axis out
582 * @yo: Y axis out
583 * @zo: Z axis out
584 * @return: 0 on success or error code
585 */
yas537_get_measure(struct yas5xx * yas5xx,s32 * to,s32 * xo,s32 * yo,s32 * zo)586 static int yas537_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
587 {
588 u16 t, x, y1, y2;
589 int ret;
590
591 /* We first get raw data that needs to be translated to [x,y,z] */
592 ret = yas537_measure(yas5xx, &t, &x, &y1, &y2);
593 if (ret)
594 return ret;
595
596 /* Calculate temperature readout */
597 *to = yas5xx_calc_temperature(yas5xx, t);
598
599 /*
600 * Unfortunately, no linearization or temperature compensation formulas
601 * are known for YAS537.
602 */
603
604 /* Calculate x, y, z from x, y1, y2 */
605 *xo = (x - BIT(13)) * 300;
606 *yo = (y1 - y2) * 1732 / 10;
607 *zo = (-y1 - y2 + BIT(14)) * 300;
608
609 return 0;
610 }
611
yas5xx_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)612 static int yas5xx_read_raw(struct iio_dev *indio_dev,
613 struct iio_chan_spec const *chan,
614 int *val, int *val2,
615 long mask)
616 {
617 struct yas5xx *yas5xx = iio_priv(indio_dev);
618 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
619 s32 t, x, y, z;
620 int ret;
621
622 switch (mask) {
623 case IIO_CHAN_INFO_PROCESSED:
624 case IIO_CHAN_INFO_RAW:
625 pm_runtime_get_sync(yas5xx->dev);
626 ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
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;
646 }
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;
655 }
656 }
657
yas5xx_fill_buffer(struct iio_dev * indio_dev)658 static void yas5xx_fill_buffer(struct iio_dev *indio_dev)
659 {
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;
664
665 pm_runtime_get_sync(yas5xx->dev);
666 ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
667 pm_runtime_put_autosuspend(yas5xx->dev);
668 if (ret) {
669 dev_err(yas5xx->dev, "error refilling buffer\n");
670 return;
671 }
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_ts(indio_dev, &yas5xx->scan, sizeof(yas5xx->scan),
677 iio_get_time_ns(indio_dev));
678 }
679
yas5xx_handle_trigger(int irq,void * p)680 static irqreturn_t yas5xx_handle_trigger(int irq, void *p)
681 {
682 const struct iio_poll_func *pf = p;
683 struct iio_dev *indio_dev = pf->indio_dev;
684
685 yas5xx_fill_buffer(indio_dev);
686 iio_trigger_notify_done(indio_dev->trig);
687
688 return IRQ_HANDLED;
689 }
690
691
692 static const struct iio_mount_matrix *
yas5xx_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)693 yas5xx_get_mount_matrix(const struct iio_dev *indio_dev,
694 const struct iio_chan_spec *chan)
695 {
696 struct yas5xx *yas5xx = iio_priv(indio_dev);
697
698 return &yas5xx->orientation;
699 }
700
701 static const struct iio_chan_spec_ext_info yas5xx_ext_info[] = {
702 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, yas5xx_get_mount_matrix),
703 { }
704 };
705
706 #define YAS5XX_AXIS_CHANNEL(axis, index) \
707 { \
708 .type = IIO_MAGN, \
709 .modified = 1, \
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, \
714 .address = index, \
715 .scan_index = index, \
716 .scan_type = { \
717 .sign = 's', \
718 .realbits = 32, \
719 .storagebits = 32, \
720 .endianness = IIO_CPU, \
721 }, \
722 }
723
724 static const struct iio_chan_spec yas5xx_channels[] = {
725 {
726 .type = IIO_TEMP,
727 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
728 .address = 0,
729 .scan_index = 0,
730 .scan_type = {
731 .sign = 's',
732 .realbits = 32,
733 .storagebits = 32,
734 .endianness = IIO_CPU,
735 },
736 },
737 YAS5XX_AXIS_CHANNEL(X, 1),
738 YAS5XX_AXIS_CHANNEL(Y, 2),
739 YAS5XX_AXIS_CHANNEL(Z, 3),
740 IIO_CHAN_SOFT_TIMESTAMP(4),
741 };
742
743 static const unsigned long yas5xx_scan_masks[] = { GENMASK(3, 0), 0 };
744
745 static const struct iio_info yas5xx_info = {
746 .read_raw = &yas5xx_read_raw,
747 };
748
yas5xx_volatile_reg(struct device * dev,unsigned int reg)749 static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg)
750 {
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;
754 int reg_qty;
755 int i;
756
757 if (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8)
758 return true;
759
760 /*
761 * YAS versions share different registers on the same address,
762 * need to differentiate.
763 */
764 reg_qty = ci->volatile_reg_qty;
765 for (i = 0; i < reg_qty; i++) {
766 if (reg == ci->volatile_reg[i])
767 return true;
768 }
769
770 return false;
771 }
772
773 /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
774 static const struct regmap_config yas5xx_regmap_config = {
775 .reg_bits = 8,
776 .val_bits = 8,
777 .max_register = 0xff,
778 .volatile_reg = yas5xx_volatile_reg,
779 };
780
781 /**
782 * yas530_extract_calibration() - extracts the a2-a9 and k calibration
783 * @data: the bitfield to use
784 * @c: the calibration to populate
785 *
786 * Used by YAS530, YAS532 and YAS533.
787 */
yas530_extract_calibration(u8 * data,struct yas5xx_calibration * c)788 static void yas530_extract_calibration(u8 *data, struct yas5xx_calibration *c)
789 {
790 u64 val = get_unaligned_be64(data);
791
792 /*
793 * Bitfield layout for the axis calibration data, for factor
794 * a2 = 2 etc, k = k, c = clock divider
795 *
796 * n 7 6 5 4 3 2 1 0
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
805 */
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);
816 }
817
yas530_get_calibration_data(struct yas5xx * yas5xx)818 static int yas530_get_calibration_data(struct yas5xx *yas5xx)
819 {
820 struct yas5xx_calibration *c = &yas5xx->calibration;
821 u8 data[16];
822 u32 val;
823 int ret;
824
825 /* Dummy read, first read is ALWAYS wrong */
826 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
827 if (ret)
828 return ret;
829
830 /* Actual calibration readout */
831 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
832 if (ret)
833 return ret;
834 dev_dbg(yas5xx->dev, "calibration data: %16ph\n", data);
835
836 /* Contribute calibration data to the input pool for kernel entropy */
837 add_device_randomness(data, sizeof(data));
838
839 /* Extract version */
840 yas5xx->version = data[15] & GENMASK(1, 0);
841
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);
847
848 /*
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
852 *
853 * n 7 6 5 4 3 2 1 0
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
858 */
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] = FIELD_GET_SIGNED(GENMASK(28, 23), val);
864 c->r[1] = FIELD_GET_SIGNED(GENMASK(20, 15), val);
865 c->r[2] = FIELD_GET_SIGNED(GENMASK(12, 7), val);
866
867 return 0;
868 }
869
yas532_get_calibration_data(struct yas5xx * yas5xx)870 static int yas532_get_calibration_data(struct yas5xx *yas5xx)
871 {
872 struct yas5xx_calibration *c = &yas5xx->calibration;
873 u8 data[14];
874 u32 val;
875 int ret;
876
877 /* Dummy read, first read is ALWAYS wrong */
878 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
879 if (ret)
880 return ret;
881 /* Actual calibration readout */
882 ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
883 if (ret)
884 return ret;
885 dev_dbg(yas5xx->dev, "calibration data: %14ph\n", data);
886
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");
890
891 /* Contribute calibration data to the input pool for kernel entropy */
892 add_device_randomness(data, sizeof(data));
893
894 /* Only one bit of version info reserved here as far as we know */
895 yas5xx->version = data[13] & BIT(0);
896
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);
902
903 /*
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
907 *
908 * n 7 6 5 4 3 2 1 0
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
913 */
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] = FIELD_GET_SIGNED(GENMASK(30, 25), val);
919 c->r[1] = FIELD_GET_SIGNED(GENMASK(22, 17), val);
920 c->r[2] = FIELD_GET_SIGNED(GENMASK(14, 7), val);
921
922 return 0;
923 }
924
yas537_get_calibration_data(struct yas5xx * yas5xx)925 static int yas537_get_calibration_data(struct yas5xx *yas5xx)
926 {
927 struct yas5xx_calibration *c = &yas5xx->calibration;
928 u8 data[17];
929 u32 val1, val2, val3, val4;
930 int i, ret;
931
932 /* Writing SRST register */
933 ret = regmap_write(yas5xx->map, YAS537_SRST, BIT(1));
934 if (ret)
935 return ret;
936
937 /* Calibration readout, YAS537 needs one readout only */
938 ret = regmap_bulk_read(yas5xx->map, YAS537_CAL, data, sizeof(data));
939 if (ret)
940 return ret;
941 dev_dbg(yas5xx->dev, "calibration data: %17ph\n", data);
942
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");
946
947 /* Contribute calibration data to the input pool for kernel entropy */
948 add_device_randomness(data, sizeof(data));
949
950 /* Extract version information */
951 yas5xx->version = FIELD_GET(GENMASK(7, 6), data[16]);
952
953 /* There are two versions of YAS537 behaving differently */
954 switch (yas5xx->version) {
955 case YAS537_VERSION_0:
956 /*
957 * The first version simply writes data back into registers:
958 *
959 * data[0] YAS537_MTC 0x93
960 * data[1] 0x94
961 * data[2] 0x95
962 * data[3] 0x96
963 * data[4] 0x97
964 * data[5] 0x98
965 * data[6] 0x99
966 * data[7] 0x9a
967 * data[8] 0x9b
968 * data[9] 0x9c
969 * data[10] 0x9d
970 * data[11] YAS537_OC 0x9e
971 *
972 * data[12] YAS537_OFFSET_X 0x84
973 * data[13] YAS537_OFFSET_Y1 0x85
974 * data[14] YAS537_OFFSET_Y2 0x86
975 *
976 * data[15] YAS537_HCK 0x88
977 * data[16] YAS537_LCK 0x89
978 */
979 for (i = 0; i < 12; i++) {
980 ret = regmap_write(yas5xx->map, YAS537_MTC + i,
981 data[i]);
982 if (ret)
983 return ret;
984 }
985 for (i = 0; i < 3; i++) {
986 ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
987 data[i + 12]);
988 if (ret)
989 return ret;
990 yas5xx->hard_offsets[i] = data[i + 12];
991 }
992 for (i = 0; i < 2; i++) {
993 ret = regmap_write(yas5xx->map, YAS537_HCK + i,
994 data[i + 15]);
995 if (ret)
996 return ret;
997 }
998 break;
999 case YAS537_VERSION_1:
1000 /*
1001 * The second version writes some data into registers but also
1002 * extracts calibration coefficients.
1003 *
1004 * Registers being written:
1005 *
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
1010 *
1011 * data[12] YAS537_OFFSET_X 0x84
1012 * data[13] YAS537_OFFSET_Y1 0x85
1013 * data[14] YAS537_OFFSET_Y2 0x86
1014 *
1015 * data[15] YAS537_HCK (partially) 0x88
1016 * YAS537_LCK (partially) 0x89
1017 * data[16] YAS537_OC (partially) 0x9e
1018 */
1019 for (i = 0; i < 3; i++) {
1020 ret = regmap_write(yas5xx->map, YAS537_MTC + i,
1021 data[i]);
1022 if (ret)
1023 return ret;
1024 }
1025 for (i = 0; i < 3; i++) {
1026 ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
1027 data[i + 12]);
1028 if (ret)
1029 return ret;
1030 yas5xx->hard_offsets[i] = data[i + 12];
1031 }
1032 /*
1033 * Visualization of partially taken data:
1034 *
1035 * data[3] n 7 6 5 4 3 2 1 0
1036 * YAS537_MTC+3 x x x 1 0 0 0 0
1037 *
1038 * data[15] n 7 6 5 4 3 2 1 0
1039 * YAS537_HCK x x x x 0
1040 *
1041 * data[15] n 7 6 5 4 3 2 1 0
1042 * YAS537_LCK x x x x 0
1043 *
1044 * data[16] n 7 6 5 4 3 2 1 0
1045 * YAS537_OC x x x x x x
1046 */
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);
1051 if (ret)
1052 return ret;
1053 ret = regmap_write(yas5xx->map, YAS537_HCK,
1054 FIELD_PREP(YAS537_HCK_MASK_PREP,
1055 FIELD_GET(YAS537_HCK_MASK_GET, data[15])));
1056 if (ret)
1057 return ret;
1058 ret = regmap_write(yas5xx->map, YAS537_LCK,
1059 FIELD_PREP(YAS537_LCK_MASK_PREP,
1060 FIELD_GET(YAS537_LCK_MASK_GET, data[15])));
1061 if (ret)
1062 return ret;
1063 ret = regmap_write(yas5xx->map, YAS537_OC,
1064 FIELD_GET(YAS537_OC_MASK_GET, data[16]));
1065 if (ret)
1066 return ret;
1067 /*
1068 * For data extraction, build some blocks. Four 32-bit blocks
1069 * look appropriate.
1070 *
1071 * n 7 6 5 4 3 2 1 0
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
1076 *
1077 * n 7 6 5 4 3 2 1 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
1082 *
1083 * n 7 6 5 4 3 2 1 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
1088 *
1089 * n 7 6 5 4 3 2 1 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
1094 */
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);
1112 break;
1113 default:
1114 dev_err(yas5xx->dev, "unknown version of YAS537\n");
1115 return -EINVAL;
1116 }
1117
1118 return 0;
1119 }
1120
1121 /* Used by YAS530, YAS532 and YAS533 */
yas530_dump_calibration(struct yas5xx * yas5xx)1122 static void yas530_dump_calibration(struct yas5xx *yas5xx)
1123 {
1124 struct yas5xx_calibration *c = &yas5xx->calibration;
1125
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);
1143 }
1144
yas537_dump_calibration(struct yas5xx * yas5xx)1145 static void yas537_dump_calibration(struct yas5xx *yas5xx)
1146 {
1147 struct yas5xx_calibration *c = &yas5xx->calibration;
1148
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);
1162 }
1163 }
1164
1165 /* Used by YAS530, YAS532 and YAS533 */
yas530_set_offsets(struct yas5xx * yas5xx,s8 ox,s8 oy1,s8 oy2)1166 static int yas530_set_offsets(struct yas5xx *yas5xx, s8 ox, s8 oy1, s8 oy2)
1167 {
1168 int ret;
1169
1170 ret = regmap_write(yas5xx->map, YAS530_OFFSET_X, ox);
1171 if (ret)
1172 return ret;
1173 ret = regmap_write(yas5xx->map, YAS530_OFFSET_Y1, oy1);
1174 if (ret)
1175 return ret;
1176 return regmap_write(yas5xx->map, YAS530_OFFSET_Y2, oy2);
1177 }
1178
1179 /* Used by YAS530, YAS532 and YAS533 */
yas530_adjust_offset(s8 old,int bit,u16 center,u16 measure)1180 static s8 yas530_adjust_offset(s8 old, int bit, u16 center, u16 measure)
1181 {
1182 if (measure > center)
1183 return old + BIT(bit);
1184 if (measure < center)
1185 return old - BIT(bit);
1186 return old;
1187 }
1188
1189 /* Used by YAS530, YAS532 and YAS533 */
yas530_measure_offsets(struct yas5xx * yas5xx)1190 static int yas530_measure_offsets(struct yas5xx *yas5xx)
1191 {
1192 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
1193 int ret;
1194 u16 center;
1195 u16 t, x, y1, y2;
1196 s8 ox, oy1, oy2;
1197 int i;
1198
1199 /* Actuate the init coil and measure offsets */
1200 ret = regmap_write(yas5xx->map, YAS530_ACTUATE_INIT_COIL, 0);
1201 if (ret)
1202 return ret;
1203
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;
1208 break;
1209 case YAS532_DEVICE_ID:
1210 center = YAS532_DATA_CENTER;
1211 break;
1212 default:
1213 dev_err(yas5xx->dev, "unknown device type\n");
1214 return -EINVAL;
1215 }
1216
1217 /*
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
1221 * registers.
1222 *
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 * roughly to the center.
1228 */
1229 ox = 0;
1230 oy1 = 0;
1231 oy2 = 0;
1232
1233 for (i = 4; i >= 0; i--) {
1234 ret = yas530_set_offsets(yas5xx, ox, oy1, oy2);
1235 if (ret)
1236 return ret;
1237
1238 ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
1239 if (ret)
1240 return ret;
1241 dev_dbg(yas5xx->dev, "measurement %d: x=%d, y1=%d, y2=%d\n",
1242 5-i, x, y1, y2);
1243
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);
1247 }
1248
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);
1254 if (ret)
1255 return ret;
1256
1257 dev_info(yas5xx->dev, "discovered hard offsets: x=%d, y1=%d, y2=%d\n",
1258 ox, oy1, oy2);
1259 return 0;
1260 }
1261
1262 /* Used by YAS530, YAS532 and YAS533 */
yas530_power_on(struct yas5xx * yas5xx)1263 static int yas530_power_on(struct yas5xx *yas5xx)
1264 {
1265 unsigned int val;
1266 int ret;
1267
1268 /* Zero the test registers */
1269 ret = regmap_write(yas5xx->map, YAS530_TEST1, 0);
1270 if (ret)
1271 return ret;
1272 ret = regmap_write(yas5xx->map, YAS530_TEST2, 0);
1273 if (ret)
1274 return ret;
1275
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);
1279 if (ret)
1280 return ret;
1281
1282 /* Measure interval 0 (back-to-back?) */
1283 return regmap_write(yas5xx->map, YAS530_MEASURE_INTERVAL, 0);
1284 }
1285
yas537_power_on(struct yas5xx * yas5xx)1286 static int yas537_power_on(struct yas5xx *yas5xx)
1287 {
1288 __be16 buf;
1289 int ret;
1290 u8 intrvl;
1291
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));
1295 if (ret)
1296 return ret;
1297 ret = regmap_write(yas5xx->map, YAS537_TRM, GENMASK(7, 0));
1298 if (ret)
1299 return ret;
1300
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);
1305 if (ret)
1306 return ret;
1307
1308 /* The average value is also static in regular operation */
1309 ret = regmap_write(yas5xx->map, YAS537_AVR, YAS537_MAG_AVERAGE_32_MASK);
1310 if (ret)
1311 return ret;
1312
1313 /* Perform the "rcoil" part but skip the "last_after_rcoil" read */
1314 ret = regmap_write(yas5xx->map, YAS537_CONFIG, BIT(3));
1315 if (ret)
1316 return ret;
1317
1318 /* Wait until the coil has ramped up */
1319 fsleep(YAS537_MAG_RCOIL_TIME_US);
1320
1321 return 0;
1322 }
1323
1324 static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
1325 [yas530] = {
1326 .devid = YAS530_DEVICE_ID,
1327 .product_label = "yas530",
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,
1340 },
1341 [yas532] = {
1342 .devid = YAS532_DEVICE_ID,
1343 .product_label = "yas532",
1344 .product_name = "YAS532 MS-3R",
1345 .version_names = { "AB", "AC" },
1346 .volatile_reg = yas530_volatile_reg,
1347 .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
1348 .scaling_val2 = 100000, /* nanotesla to Gauss */
1349 .t_ref = 390, /* counts */
1350 .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
1351 .get_measure = yas530_get_measure,
1352 .get_calibration_data = yas532_get_calibration_data,
1353 .dump_calibration = yas530_dump_calibration,
1354 .measure_offsets = yas530_measure_offsets,
1355 .power_on = yas530_power_on,
1356 },
1357 [yas533] = {
1358 .devid = YAS532_DEVICE_ID,
1359 .product_label = "yas533",
1360 .product_name = "YAS533 MS-3F",
1361 .version_names = { "AB", "AC" },
1362 .volatile_reg = yas530_volatile_reg,
1363 .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
1364 .scaling_val2 = 100000, /* nanotesla to Gauss */
1365 .t_ref = 390, /* counts */
1366 .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
1367 .get_measure = yas530_get_measure,
1368 .get_calibration_data = yas532_get_calibration_data,
1369 .dump_calibration = yas530_dump_calibration,
1370 .measure_offsets = yas530_measure_offsets,
1371 .power_on = yas530_power_on,
1372 },
1373 [yas537] = {
1374 .devid = YAS537_DEVICE_ID,
1375 .product_label = "yas537",
1376 .product_name = "YAS537 MS-3T",
1377 .version_names = { "v0", "v1" }, /* version naming unknown */
1378 .volatile_reg = yas537_volatile_reg,
1379 .volatile_reg_qty = ARRAY_SIZE(yas537_volatile_reg),
1380 .scaling_val2 = 100000, /* nanotesla to Gauss */
1381 .t_ref = 8120, /* counts */
1382 .min_temp_x10 = -3860, /* 1/10:s degrees Celsius */
1383 .get_measure = yas537_get_measure,
1384 .get_calibration_data = yas537_get_calibration_data,
1385 .dump_calibration = yas537_dump_calibration,
1386 /* .measure_offets is not needed for yas537 */
1387 .power_on = yas537_power_on,
1388 },
1389 };
1390
yas5xx_probe(struct i2c_client * i2c)1391 static int yas5xx_probe(struct i2c_client *i2c)
1392 {
1393 struct iio_dev *indio_dev;
1394 struct device *dev = &i2c->dev;
1395 struct yas5xx *yas5xx;
1396 const struct yas5xx_chip_info *ci;
1397 int id_check;
1398 int ret;
1399
1400 indio_dev = devm_iio_device_alloc(dev, sizeof(*yas5xx));
1401 if (!indio_dev)
1402 return -ENOMEM;
1403
1404 yas5xx = iio_priv(indio_dev);
1405 i2c_set_clientdata(i2c, indio_dev);
1406 yas5xx->dev = dev;
1407
1408 ret = devm_mutex_init(dev, &yas5xx->lock);
1409 if (ret)
1410 return ret;
1411
1412 ret = iio_read_mount_matrix(dev, &yas5xx->orientation);
1413 if (ret)
1414 return ret;
1415
1416 yas5xx->regs[0].supply = "vdd";
1417 yas5xx->regs[1].supply = "iovdd";
1418 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(yas5xx->regs),
1419 yas5xx->regs);
1420 if (ret)
1421 return dev_err_probe(dev, ret, "cannot get regulators\n");
1422
1423 ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1424 if (ret)
1425 return dev_err_probe(dev, ret, "cannot enable regulators\n");
1426
1427 /* See comment in runtime resume callback */
1428 fsleep(31 * USEC_PER_MSEC);
1429
1430 /* This will take the device out of reset if need be */
1431 yas5xx->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1432 if (IS_ERR(yas5xx->reset)) {
1433 ret = dev_err_probe(dev, PTR_ERR(yas5xx->reset), "failed to get reset line\n");
1434 goto reg_off;
1435 }
1436
1437 yas5xx->map = devm_regmap_init_i2c(i2c, &yas5xx_regmap_config);
1438 if (IS_ERR(yas5xx->map)) {
1439 ret = dev_err_probe(dev, PTR_ERR(yas5xx->map), "failed to allocate register map\n");
1440 goto assert_reset;
1441 }
1442
1443 ci = i2c_get_match_data(i2c);
1444 yas5xx->chip_info = ci;
1445
1446 ret = regmap_read(yas5xx->map, YAS5XX_DEVICE_ID, &id_check);
1447 if (ret)
1448 goto assert_reset;
1449
1450 if (id_check != ci->devid) {
1451 ret = dev_err_probe(dev, -ENODEV,
1452 "device ID %02x doesn't match %s\n",
1453 id_check, ci->product_label);
1454 goto assert_reset;
1455 }
1456
1457 ret = ci->get_calibration_data(yas5xx);
1458 if (ret)
1459 goto assert_reset;
1460
1461 dev_info(dev, "detected %s %s\n", ci->product_name,
1462 ci->version_names[yas5xx->version]);
1463
1464 ci->dump_calibration(yas5xx);
1465
1466 ret = ci->power_on(yas5xx);
1467 if (ret)
1468 goto assert_reset;
1469
1470 if (ci->measure_offsets) {
1471 ret = ci->measure_offsets(yas5xx);
1472 if (ret)
1473 goto assert_reset;
1474 }
1475
1476 indio_dev->info = &yas5xx_info;
1477 indio_dev->available_scan_masks = yas5xx_scan_masks;
1478 indio_dev->modes = INDIO_DIRECT_MODE;
1479 indio_dev->name = ci->product_label;
1480 indio_dev->channels = yas5xx_channels;
1481 indio_dev->num_channels = ARRAY_SIZE(yas5xx_channels);
1482
1483 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1484 yas5xx_handle_trigger,
1485 NULL);
1486 if (ret) {
1487 dev_err_probe(dev, ret, "triggered buffer setup failed\n");
1488 goto assert_reset;
1489 }
1490
1491 ret = iio_device_register(indio_dev);
1492 if (ret) {
1493 dev_err_probe(dev, ret, "device register failed\n");
1494 goto cleanup_buffer;
1495 }
1496
1497 /* Take runtime PM online */
1498 pm_runtime_get_noresume(dev);
1499 pm_runtime_set_active(dev);
1500 pm_runtime_enable(dev);
1501
1502 pm_runtime_set_autosuspend_delay(dev, YAS5XX_AUTOSUSPEND_DELAY_MS);
1503 pm_runtime_use_autosuspend(dev);
1504 pm_runtime_put(dev);
1505
1506 return 0;
1507
1508 cleanup_buffer:
1509 iio_triggered_buffer_cleanup(indio_dev);
1510 assert_reset:
1511 gpiod_set_value_cansleep(yas5xx->reset, 1);
1512 reg_off:
1513 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1514
1515 return ret;
1516 }
1517
yas5xx_remove(struct i2c_client * i2c)1518 static void yas5xx_remove(struct i2c_client *i2c)
1519 {
1520 struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
1521 struct yas5xx *yas5xx = iio_priv(indio_dev);
1522 struct device *dev = &i2c->dev;
1523
1524 iio_device_unregister(indio_dev);
1525 iio_triggered_buffer_cleanup(indio_dev);
1526 /*
1527 * Now we can't get any more reads from the device, which would
1528 * also call pm_runtime* functions and race with our disable
1529 * code. Disable PM runtime in orderly fashion and power down.
1530 */
1531 pm_runtime_get_sync(dev);
1532 pm_runtime_put_noidle(dev);
1533 pm_runtime_disable(dev);
1534 gpiod_set_value_cansleep(yas5xx->reset, 1);
1535 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1536 }
1537
yas5xx_runtime_suspend(struct device * dev)1538 static int yas5xx_runtime_suspend(struct device *dev)
1539 {
1540 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1541 struct yas5xx *yas5xx = iio_priv(indio_dev);
1542
1543 gpiod_set_value_cansleep(yas5xx->reset, 1);
1544 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1545
1546 return 0;
1547 }
1548
yas5xx_runtime_resume(struct device * dev)1549 static int yas5xx_runtime_resume(struct device *dev)
1550 {
1551 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1552 struct yas5xx *yas5xx = iio_priv(indio_dev);
1553 const struct yas5xx_chip_info *ci = yas5xx->chip_info;
1554 int ret;
1555
1556 ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1557 if (ret) {
1558 dev_err(dev, "cannot enable regulators\n");
1559 return ret;
1560 }
1561
1562 /*
1563 * The YAS530 datasheet says TVSKW is up to 30 ms, after that 1 ms
1564 * for all voltages to settle. The YAS532 is 10ms then 4ms for the
1565 * I2C to come online. Let's keep it safe and put this at 31ms.
1566 */
1567 fsleep(31 * USEC_PER_MSEC);
1568 gpiod_set_value_cansleep(yas5xx->reset, 0);
1569
1570 ret = ci->power_on(yas5xx);
1571 if (ret) {
1572 dev_err(dev, "cannot power on\n");
1573 goto out_reset;
1574 }
1575
1576 return 0;
1577
1578 out_reset:
1579 gpiod_set_value_cansleep(yas5xx->reset, 1);
1580 regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
1581
1582 return ret;
1583 }
1584
1585 static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops, yas5xx_runtime_suspend,
1586 yas5xx_runtime_resume, NULL);
1587
1588 static const struct i2c_device_id yas5xx_id[] = {
1589 { .name = "yas530", .driver_data = (kernel_ulong_t)&yas5xx_chip_info_tbl[yas530] },
1590 { .name = "yas532", .driver_data = (kernel_ulong_t)&yas5xx_chip_info_tbl[yas532] },
1591 { .name = "yas533", .driver_data = (kernel_ulong_t)&yas5xx_chip_info_tbl[yas533] },
1592 { .name = "yas537", .driver_data = (kernel_ulong_t)&yas5xx_chip_info_tbl[yas537] },
1593 { }
1594 };
1595 MODULE_DEVICE_TABLE(i2c, yas5xx_id);
1596
1597 static const struct of_device_id yas5xx_of_match[] = {
1598 { .compatible = "yamaha,yas530", &yas5xx_chip_info_tbl[yas530] },
1599 { .compatible = "yamaha,yas532", &yas5xx_chip_info_tbl[yas532] },
1600 { .compatible = "yamaha,yas533", &yas5xx_chip_info_tbl[yas533] },
1601 { .compatible = "yamaha,yas537", &yas5xx_chip_info_tbl[yas537] },
1602 { }
1603 };
1604 MODULE_DEVICE_TABLE(of, yas5xx_of_match);
1605
1606 static struct i2c_driver yas5xx_driver = {
1607 .driver = {
1608 .name = "yas5xx",
1609 .of_match_table = yas5xx_of_match,
1610 .pm = pm_ptr(&yas5xx_dev_pm_ops),
1611 },
1612 .probe = yas5xx_probe,
1613 .remove = yas5xx_remove,
1614 .id_table = yas5xx_id,
1615 };
1616 module_i2c_driver(yas5xx_driver);
1617
1618 MODULE_DESCRIPTION("Yamaha YAS53x 3-axis magnetometer driver");
1619 MODULE_AUTHOR("Linus Walleij");
1620 MODULE_LICENSE("GPL v2");
1621