| 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 <[email protected]> |
| 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/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> |
| 40 |
|
| 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> |
| 45 |
|
| 46 |
#include <linux/unaligned.h> |
| 47 |
|
| 48 |
/* Commonly used registers */ |
| 49 |
#define YAS5XX_DEVICE_ID 0x80 |
| 50 |
#define YAS5XX_MEASURE_DATA 0xB0 |
| 51 |
|
| 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 |
| 63 |
|
| 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 |
| 80 |
|
| 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 |
| 86 |
|
| 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) |
| 93 |
|
| 94 |
/* Bits in the measure data register */ |
| 95 |
#define YAS5XX_MEASURE_DATA_BUSY BIT(7) |
| 96 |
|
| 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) |
| 105 |
|
| 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) |
| 116 |
|
| 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) |
| 132 |
|
| 133 |
/* Turn off device regulators etc after 5 seconds of inactivity */ |
| 134 |
#define YAS5XX_AUTOSUSPEND_DELAY_MS 5000 |
| 135 |
|
| 136 |
enum chip_ids { |
| 137 |
yas530, |
| 138 |
yas532, |
| 139 |
yas533, |
| 140 |
yas537, |
| 141 |
}; |
| 142 |
|
| 143 |
static const int yas530_volatile_reg[] = { |
| 144 |
YAS530_ACTUATE_INIT_COIL, |
| 145 |
YAS530_MEASURE, |
| 146 |
}; |
| 147 |
|
| 148 |
static const int yas537_volatile_reg[] = { |
| 149 |
YAS537_MEASURE, |
| 150 |
}; |
| 151 |
|
| 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; |
| 164 |
}; |
| 165 |
|
| 166 |
struct yas5xx; |
| 167 |
|
| 168 |
/** |
| 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 |
| 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_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); |
| 204 |
}; |
| 205 |
|
| 206 |
/** |
| 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 |
| 221 |
*/ |
| 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; |
| 233 |
/* |
| 234 |
* The scanout is 4 x 32 bits in CPU endianness. |
| 235 |
* Ensure timestamp is naturally aligned |
| 236 |
*/ |
| 237 |
struct { |
| 238 |
s32 channels[4]; |
| 239 |
aligned_s64 ts; |
| 240 |
} scan; |
| 241 |
}; |
| 242 |
|
| 243 |
/* On YAS530 the x, y1 and y2 values are 12 bits */ |
| 244 |
static u16 yas530_extract_axis(u8 *data) |
| 245 |
{ |
| 246 |
u16 val; |
| 247 |
|
| 248 |
/* |
| 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 |
| 252 |
*/ |
| 253 |
val = get_unaligned_be16(&data[0]); |
| 254 |
val = FIELD_GET(GENMASK(14, 3), val); |
| 255 |
return val; |
| 256 |
} |
| 257 |
|
| 258 |
/* On YAS532 the x, y1 and y2 values are 13 bits */ |
| 259 |
static u16 yas532_extract_axis(u8 *data) |
| 260 |
{ |
| 261 |
u16 val; |
| 262 |
|
| 263 |
/* |
| 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 |
| 267 |
*/ |
| 268 |
val = get_unaligned_be16(&data[0]); |
| 269 |
val = FIELD_GET(GENMASK(14, 2), val); |
| 270 |
return val; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 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 |
| 281 |
* |
| 282 |
* Used by YAS530, YAS532 and YAS533. |
| 283 |
*/ |
| 284 |
static int yas530_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2) |
| 285 |
{ |
| 286 |
const struct yas5xx_chip_info *ci = yas5xx->chip_info; |
| 287 |
unsigned int busy; |
| 288 |
u8 data[8]; |
| 289 |
int ret; |
| 290 |
u16 val; |
| 291 |
|
| 292 |
mutex_lock(&yas5xx->lock); |
| 293 |
ret = regmap_write(yas5xx->map, YAS530_MEASURE, YAS5XX_MEASURE_START); |
| 294 |
if (ret < 0) |
| 295 |
goto out_unlock; |
| 296 |
|
| 297 |
/* |
| 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. |
| 301 |
*/ |
| 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; |
| 308 |
} |
| 309 |
|
| 310 |
ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA, |
| 311 |
data, sizeof(data)); |
| 312 |
if (ret) |
| 313 |
goto out_unlock; |
| 314 |
|
| 315 |
mutex_unlock(&yas5xx->lock); |
| 316 |
|
| 317 |
switch (ci->devid) { |
| 318 |
case YAS530_DEVICE_ID: |
| 319 |
/* |
| 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 |
| 324 |
*/ |
| 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: |
| 333 |
/* |
| 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 |
| 338 |
*/ |
| 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; |
| 350 |
} |
| 351 |
|
| 352 |
return ret; |
| 353 |
|
| 354 |
out_unlock: |
| 355 |
mutex_unlock(&yas5xx->lock); |
| 356 |
return ret; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 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 |
| 367 |
*/ |
| 368 |
static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2) |
| 369 |
{ |
| 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; |
| 377 |
|
| 378 |
mutex_lock(&yas5xx->lock); |
| 379 |
|
| 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; |
| 385 |
|
| 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; |
| 393 |
} |
| 394 |
|
| 395 |
ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA, |
| 396 |
data, sizeof(data)); |
| 397 |
if (ret) |
| 398 |
goto out_unlock; |
| 399 |
|
| 400 |
mutex_unlock(&yas5xx->lock); |
| 401 |
|
| 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]); |
| 406 |
|
| 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; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
*x = xy1y2[0]; |
| 421 |
*y1 = xy1y2[1]; |
| 422 |
*y2 = xy1y2[2]; |
| 423 |
|
| 424 |
return 0; |
| 425 |
|
| 426 |
out_unlock: |
| 427 |
mutex_unlock(&yas5xx->lock); |
| 428 |
return ret; |
| 429 |
} |
| 430 |
|
| 431 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 432 |
static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis) |
| 433 |
{ |
| 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, |
| 440 |
}; |
| 441 |
s32 coef; |
| 442 |
|
| 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; |
| 461 |
} |
| 462 |
/* |
| 463 |
* Linearization formula: |
| 464 |
* |
| 465 |
* x' = x - (3721 + 50 * f) + (xoffset - r) * c |
| 466 |
* |
| 467 |
* Where f and r are calibration values, c is a per-device |
| 468 |
* and sometimes per-axis coefficient. |
| 469 |
*/ |
| 470 |
return val - (3721 + 50 * c->f[axis]) + |
| 471 |
(yas5xx->hard_offsets[axis] - c->r[axis]) * coef; |
| 472 |
} |
| 473 |
|
| 474 |
static s32 yas5xx_calc_temperature(struct yas5xx *yas5xx, u16 t) |
| 475 |
{ |
| 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; |
| 481 |
|
| 482 |
t_ref = ci->t_ref; |
| 483 |
min_temp_x10 = ci->min_temp_x10; |
| 484 |
ref_temp_x10 = 200; |
| 485 |
|
| 486 |
to = (min_temp_x10 + ((ref_temp_x10 - min_temp_x10) * t / t_ref)) * 100; |
| 487 |
return to; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 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 |
| 498 |
* |
| 499 |
* Used by YAS530, YAS532 and YAS533. |
| 500 |
*/ |
| 501 |
static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo) |
| 502 |
{ |
| 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; |
| 509 |
|
| 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; |
| 514 |
|
| 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); |
| 519 |
|
| 520 |
/* |
| 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. |
| 524 |
*/ |
| 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; |
| 531 |
} |
| 532 |
|
| 533 |
/* |
| 534 |
* Temperature compensation for x, y1, y2 respectively: |
| 535 |
* |
| 536 |
* Cx * t_comp |
| 537 |
* x' = x - ----------- |
| 538 |
* 100 |
| 539 |
*/ |
| 540 |
sx = sx - (c->Cx * t_comp) / 100; |
| 541 |
sy1 = sy1 - (c->Cy1 * t_comp) / 100; |
| 542 |
sy2 = sy2 - (c->Cy2 * t_comp) / 100; |
| 543 |
|
| 544 |
/* |
| 545 |
* Break y1 and y2 into y and z, y1 and y2 are apparently encoding |
| 546 |
* y and z. |
| 547 |
*/ |
| 548 |
sy = sy1 - sy2; |
| 549 |
sz = -sy1 - sy2; |
| 550 |
|
| 551 |
/* Calculate temperature readout */ |
| 552 |
*to = yas5xx_calc_temperature(yas5xx, t); |
| 553 |
|
| 554 |
/* |
| 555 |
* Calibrate [x,y,z] with some formulas like this: |
| 556 |
* |
| 557 |
* 100 * x + a_2 * y + a_3 * z |
| 558 |
* x' = k * --------------------------- |
| 559 |
* 10 |
| 560 |
* |
| 561 |
* a_4 * x + a_5 * y + a_6 * z |
| 562 |
* y' = k * --------------------------- |
| 563 |
* 10 |
| 564 |
* |
| 565 |
* a_7 * x + a_8 * y + a_9 * z |
| 566 |
* z' = k * --------------------------- |
| 567 |
* 10 |
| 568 |
*/ |
| 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); |
| 572 |
|
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 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 |
| 584 |
*/ |
| 585 |
static int yas537_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo) |
| 586 |
{ |
| 587 |
u16 t, x, y1, y2; |
| 588 |
int ret; |
| 589 |
|
| 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; |
| 594 |
|
| 595 |
/* Calculate temperature readout */ |
| 596 |
*to = yas5xx_calc_temperature(yas5xx, t); |
| 597 |
|
| 598 |
/* |
| 599 |
* Unfortunately, no linearization or temperature compensation formulas |
| 600 |
* are known for YAS537. |
| 601 |
*/ |
| 602 |
|
| 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; |
| 607 |
|
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 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) |
| 615 |
{ |
| 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; |
| 620 |
|
| 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_put_autosuspend(yas5xx->dev); |
| 627 |
if (ret) |
| 628 |
return ret; |
| 629 |
switch (chan->address) { |
| 630 |
case 0: |
| 631 |
*val = t; |
| 632 |
break; |
| 633 |
case 1: |
| 634 |
*val = x; |
| 635 |
break; |
| 636 |
case 2: |
| 637 |
*val = y; |
| 638 |
break; |
| 639 |
case 3: |
| 640 |
*val = z; |
| 641 |
break; |
| 642 |
default: |
| 643 |
dev_err(yas5xx->dev, "unknown channel\n"); |
| 644 |
return -EINVAL; |
| 645 |
} |
| 646 |
return IIO_VAL_INT; |
| 647 |
case IIO_CHAN_INFO_SCALE: |
| 648 |
*val = 1; |
| 649 |
*val2 = ci->scaling_val2; |
| 650 |
return IIO_VAL_FRACTIONAL; |
| 651 |
default: |
| 652 |
/* Unknown request */ |
| 653 |
return -EINVAL; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
static void yas5xx_fill_buffer(struct iio_dev *indio_dev) |
| 658 |
{ |
| 659 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 660 |
const struct yas5xx_chip_info *ci = yas5xx->chip_info; |
| 661 |
s32 t, x, y, z; |
| 662 |
int ret; |
| 663 |
|
| 664 |
pm_runtime_get_sync(yas5xx->dev); |
| 665 |
ret = ci->get_measure(yas5xx, &t, &x, &y, &z); |
| 666 |
pm_runtime_put_autosuspend(yas5xx->dev); |
| 667 |
if (ret) { |
| 668 |
dev_err(yas5xx->dev, "error refilling buffer\n"); |
| 669 |
return; |
| 670 |
} |
| 671 |
yas5xx->scan.channels[0] = t; |
| 672 |
yas5xx->scan.channels[1] = x; |
| 673 |
yas5xx->scan.channels[2] = y; |
| 674 |
yas5xx->scan.channels[3] = z; |
| 675 |
iio_push_to_buffers_with_ts(indio_dev, &yas5xx->scan, sizeof(yas5xx->scan), |
| 676 |
iio_get_time_ns(indio_dev)); |
| 677 |
} |
| 678 |
|
| 679 |
static irqreturn_t yas5xx_handle_trigger(int irq, void *p) |
| 680 |
{ |
| 681 |
const struct iio_poll_func *pf = p; |
| 682 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 683 |
|
| 684 |
yas5xx_fill_buffer(indio_dev); |
| 685 |
iio_trigger_notify_done(indio_dev->trig); |
| 686 |
|
| 687 |
return IRQ_HANDLED; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
static const struct iio_mount_matrix * |
| 692 |
yas5xx_get_mount_matrix(const struct iio_dev *indio_dev, |
| 693 |
const struct iio_chan_spec *chan) |
| 694 |
{ |
| 695 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 696 |
|
| 697 |
return &yas5xx->orientation; |
| 698 |
} |
| 699 |
|
| 700 |
static const struct iio_chan_spec_ext_info yas5xx_ext_info[] = { |
| 701 |
IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, yas5xx_get_mount_matrix), |
| 702 |
{ } |
| 703 |
}; |
| 704 |
|
| 705 |
#define YAS5XX_AXIS_CHANNEL(axis, index) \ |
| 706 |
{ \ |
| 707 |
.type = IIO_MAGN, \ |
| 708 |
.modified = 1, \ |
| 709 |
.channel2 = IIO_MOD_##axis, \ |
| 710 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 711 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 712 |
.ext_info = yas5xx_ext_info, \ |
| 713 |
.address = index, \ |
| 714 |
.scan_index = index, \ |
| 715 |
.scan_type = { \ |
| 716 |
.sign = 's', \ |
| 717 |
.realbits = 32, \ |
| 718 |
.storagebits = 32, \ |
| 719 |
.endianness = IIO_CPU, \ |
| 720 |
}, \ |
| 721 |
} |
| 722 |
|
| 723 |
static const struct iio_chan_spec yas5xx_channels[] = { |
| 724 |
{ |
| 725 |
.type = IIO_TEMP, |
| 726 |
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 727 |
.address = 0, |
| 728 |
.scan_index = 0, |
| 729 |
.scan_type = { |
| 730 |
.sign = 's', |
| 731 |
.realbits = 32, |
| 732 |
.storagebits = 32, |
| 733 |
.endianness = IIO_CPU, |
| 734 |
}, |
| 735 |
}, |
| 736 |
YAS5XX_AXIS_CHANNEL(X, 1), |
| 737 |
YAS5XX_AXIS_CHANNEL(Y, 2), |
| 738 |
YAS5XX_AXIS_CHANNEL(Z, 3), |
| 739 |
IIO_CHAN_SOFT_TIMESTAMP(4), |
| 740 |
}; |
| 741 |
|
| 742 |
static const unsigned long yas5xx_scan_masks[] = { GENMASK(3, 0), 0 }; |
| 743 |
|
| 744 |
static const struct iio_info yas5xx_info = { |
| 745 |
.read_raw = &yas5xx_read_raw, |
| 746 |
}; |
| 747 |
|
| 748 |
static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg) |
| 749 |
{ |
| 750 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 751 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 752 |
const struct yas5xx_chip_info *ci = yas5xx->chip_info; |
| 753 |
int reg_qty; |
| 754 |
int i; |
| 755 |
|
| 756 |
if (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8) |
| 757 |
return true; |
| 758 |
|
| 759 |
/* |
| 760 |
* YAS versions share different registers on the same address, |
| 761 |
* need to differentiate. |
| 762 |
*/ |
| 763 |
reg_qty = ci->volatile_reg_qty; |
| 764 |
for (i = 0; i < reg_qty; i++) { |
| 765 |
if (reg == ci->volatile_reg[i]) |
| 766 |
return true; |
| 767 |
} |
| 768 |
|
| 769 |
return false; |
| 770 |
} |
| 771 |
|
| 772 |
/* TODO: enable regmap cache, using mark dirty and sync at runtime resume */ |
| 773 |
static const struct regmap_config yas5xx_regmap_config = { |
| 774 |
.reg_bits = 8, |
| 775 |
.val_bits = 8, |
| 776 |
.max_register = 0xff, |
| 777 |
.volatile_reg = yas5xx_volatile_reg, |
| 778 |
}; |
| 779 |
|
| 780 |
/** |
| 781 |
* yas530_extract_calibration() - extracts the a2-a9 and k calibration |
| 782 |
* @data: the bitfield to use |
| 783 |
* @c: the calibration to populate |
| 784 |
* |
| 785 |
* Used by YAS530, YAS532 and YAS533. |
| 786 |
*/ |
| 787 |
static void yas530_extract_calibration(u8 *data, struct yas5xx_calibration *c) |
| 788 |
{ |
| 789 |
u64 val = get_unaligned_be64(data); |
| 790 |
|
| 791 |
/* |
| 792 |
* Bitfield layout for the axis calibration data, for factor |
| 793 |
* a2 = 2 etc, k = k, c = clock divider |
| 794 |
* |
| 795 |
* n 7 6 5 4 3 2 1 0 |
| 796 |
* 0 [ 2 2 2 2 2 2 3 3 ] bits 63 .. 56 |
| 797 |
* 1 [ 3 3 4 4 4 4 4 4 ] bits 55 .. 48 |
| 798 |
* 2 [ 5 5 5 5 5 5 6 6 ] bits 47 .. 40 |
| 799 |
* 3 [ 6 6 6 6 7 7 7 7 ] bits 39 .. 32 |
| 800 |
* 4 [ 7 7 7 8 8 8 8 8 ] bits 31 .. 24 |
| 801 |
* 5 [ 8 9 9 9 9 9 9 9 ] bits 23 .. 16 |
| 802 |
* 6 [ 9 k k k k k c c ] bits 15 .. 8 |
| 803 |
* 7 [ c x x x x x x x ] bits 7 .. 0 |
| 804 |
*/ |
| 805 |
c->a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32; |
| 806 |
c->a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8; |
| 807 |
c->a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32; |
| 808 |
c->a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38; |
| 809 |
c->a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32; |
| 810 |
c->a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64; |
| 811 |
c->a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32; |
| 812 |
c->a9 = FIELD_GET(GENMASK_ULL(22, 15), val); |
| 813 |
c->k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10; |
| 814 |
c->dck = FIELD_GET(GENMASK_ULL(9, 7), val); |
| 815 |
} |
| 816 |
|
| 817 |
static int yas530_get_calibration_data(struct yas5xx *yas5xx) |
| 818 |
{ |
| 819 |
struct yas5xx_calibration *c = &yas5xx->calibration; |
| 820 |
u8 data[16]; |
| 821 |
u32 val; |
| 822 |
int ret; |
| 823 |
|
| 824 |
/* Dummy read, first read is ALWAYS wrong */ |
| 825 |
ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data)); |
| 826 |
if (ret) |
| 827 |
return ret; |
| 828 |
|
| 829 |
/* Actual calibration readout */ |
| 830 |
ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data)); |
| 831 |
if (ret) |
| 832 |
return ret; |
| 833 |
dev_dbg(yas5xx->dev, "calibration data: %16ph\n", data); |
| 834 |
|
| 835 |
/* Contribute calibration data to the input pool for kernel entropy */ |
| 836 |
add_device_randomness(data, sizeof(data)); |
| 837 |
|
| 838 |
/* Extract version */ |
| 839 |
yas5xx->version = data[15] & GENMASK(1, 0); |
| 840 |
|
| 841 |
/* Extract the calibration from the bitfield */ |
| 842 |
c->Cx = data[0] * 6 - 768; |
| 843 |
c->Cy1 = data[1] * 6 - 768; |
| 844 |
c->Cy2 = data[2] * 6 - 768; |
| 845 |
yas530_extract_calibration(&data[3], c); |
| 846 |
|
| 847 |
/* |
| 848 |
* Extract linearization: |
| 849 |
* Linearization layout in the 32 bits at byte 11: |
| 850 |
* The r factors are 6 bit values where bit 5 is the sign |
| 851 |
* |
| 852 |
* n 7 6 5 4 3 2 1 0 |
| 853 |
* 0 [ xx xx xx r0 r0 r0 r0 r0 ] bits 31 .. 24 |
| 854 |
* 1 [ r0 f0 f0 r1 r1 r1 r1 r1 ] bits 23 .. 16 |
| 855 |
* 2 [ r1 f1 f1 r2 r2 r2 r2 r2 ] bits 15 .. 8 |
| 856 |
* 3 [ r2 f2 f2 xx xx xx xx xx ] bits 7 .. 0 |
| 857 |
*/ |
| 858 |
val = get_unaligned_be32(&data[11]); |
| 859 |
c->f[0] = FIELD_GET(GENMASK(22, 21), val); |
| 860 |
c->f[1] = FIELD_GET(GENMASK(14, 13), val); |
| 861 |
c->f[2] = FIELD_GET(GENMASK(6, 5), val); |
| 862 |
c->r[0] = sign_extend32(FIELD_GET(GENMASK(28, 23), val), 5); |
| 863 |
c->r[1] = sign_extend32(FIELD_GET(GENMASK(20, 15), val), 5); |
| 864 |
c->r[2] = sign_extend32(FIELD_GET(GENMASK(12, 7), val), 5); |
| 865 |
|
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
static int yas532_get_calibration_data(struct yas5xx *yas5xx) |
| 870 |
{ |
| 871 |
struct yas5xx_calibration *c = &yas5xx->calibration; |
| 872 |
u8 data[14]; |
| 873 |
u32 val; |
| 874 |
int ret; |
| 875 |
|
| 876 |
/* Dummy read, first read is ALWAYS wrong */ |
| 877 |
ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data)); |
| 878 |
if (ret) |
| 879 |
return ret; |
| 880 |
/* Actual calibration readout */ |
| 881 |
ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data)); |
| 882 |
if (ret) |
| 883 |
return ret; |
| 884 |
dev_dbg(yas5xx->dev, "calibration data: %14ph\n", data); |
| 885 |
|
| 886 |
/* Sanity check, is this all zeroes? */ |
| 887 |
if (!memchr_inv(data, 0x00, 13) && !(data[13] & BIT(7))) |
| 888 |
dev_warn(yas5xx->dev, "calibration is blank!\n"); |
| 889 |
|
| 890 |
/* Contribute calibration data to the input pool for kernel entropy */ |
| 891 |
add_device_randomness(data, sizeof(data)); |
| 892 |
|
| 893 |
/* Only one bit of version info reserved here as far as we know */ |
| 894 |
yas5xx->version = data[13] & BIT(0); |
| 895 |
|
| 896 |
/* Extract calibration from the bitfield */ |
| 897 |
c->Cx = data[0] * 10 - 1280; |
| 898 |
c->Cy1 = data[1] * 10 - 1280; |
| 899 |
c->Cy2 = data[2] * 10 - 1280; |
| 900 |
yas530_extract_calibration(&data[3], c); |
| 901 |
|
| 902 |
/* |
| 903 |
* Extract linearization: |
| 904 |
* Linearization layout in the 32 bits at byte 10: |
| 905 |
* The r factors are 6 bit values where bit 5 is the sign |
| 906 |
* |
| 907 |
* n 7 6 5 4 3 2 1 0 |
| 908 |
* 0 [ xx r0 r0 r0 r0 r0 r0 f0 ] bits 31 .. 24 |
| 909 |
* 1 [ f0 r1 r1 r1 r1 r1 r1 f1 ] bits 23 .. 16 |
| 910 |
* 2 [ f1 r2 r2 r2 r2 r2 r2 f2 ] bits 15 .. 8 |
| 911 |
* 3 [ f2 xx xx xx xx xx xx xx ] bits 7 .. 0 |
| 912 |
*/ |
| 913 |
val = get_unaligned_be32(&data[10]); |
| 914 |
c->f[0] = FIELD_GET(GENMASK(24, 23), val); |
| 915 |
c->f[1] = FIELD_GET(GENMASK(16, 15), val); |
| 916 |
c->f[2] = FIELD_GET(GENMASK(8, 7), val); |
| 917 |
c->r[0] = sign_extend32(FIELD_GET(GENMASK(30, 25), val), 5); |
| 918 |
c->r[1] = sign_extend32(FIELD_GET(GENMASK(22, 17), val), 5); |
| 919 |
c->r[2] = sign_extend32(FIELD_GET(GENMASK(14, 7), val), 5); |
| 920 |
|
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
static int yas537_get_calibration_data(struct yas5xx *yas5xx) |
| 925 |
{ |
| 926 |
struct yas5xx_calibration *c = &yas5xx->calibration; |
| 927 |
u8 data[17]; |
| 928 |
u32 val1, val2, val3, val4; |
| 929 |
int i, ret; |
| 930 |
|
| 931 |
/* Writing SRST register */ |
| 932 |
ret = regmap_write(yas5xx->map, YAS537_SRST, BIT(1)); |
| 933 |
if (ret) |
| 934 |
return ret; |
| 935 |
|
| 936 |
/* Calibration readout, YAS537 needs one readout only */ |
| 937 |
ret = regmap_bulk_read(yas5xx->map, YAS537_CAL, data, sizeof(data)); |
| 938 |
if (ret) |
| 939 |
return ret; |
| 940 |
dev_dbg(yas5xx->dev, "calibration data: %17ph\n", data); |
| 941 |
|
| 942 |
/* Sanity check, is this all zeroes? */ |
| 943 |
if (!memchr_inv(data, 0x00, 16) && !FIELD_GET(GENMASK(5, 0), data[16])) |
| 944 |
dev_warn(yas5xx->dev, "calibration is blank!\n"); |
| 945 |
|
| 946 |
/* Contribute calibration data to the input pool for kernel entropy */ |
| 947 |
add_device_randomness(data, sizeof(data)); |
| 948 |
|
| 949 |
/* Extract version information */ |
| 950 |
yas5xx->version = FIELD_GET(GENMASK(7, 6), data[16]); |
| 951 |
|
| 952 |
/* There are two versions of YAS537 behaving differently */ |
| 953 |
switch (yas5xx->version) { |
| 954 |
case YAS537_VERSION_0: |
| 955 |
/* |
| 956 |
* The first version simply writes data back into registers: |
| 957 |
* |
| 958 |
* data[0] YAS537_MTC 0x93 |
| 959 |
* data[1] 0x94 |
| 960 |
* data[2] 0x95 |
| 961 |
* data[3] 0x96 |
| 962 |
* data[4] 0x97 |
| 963 |
* data[5] 0x98 |
| 964 |
* data[6] 0x99 |
| 965 |
* data[7] 0x9a |
| 966 |
* data[8] 0x9b |
| 967 |
* data[9] 0x9c |
| 968 |
* data[10] 0x9d |
| 969 |
* data[11] YAS537_OC 0x9e |
| 970 |
* |
| 971 |
* data[12] YAS537_OFFSET_X 0x84 |
| 972 |
* data[13] YAS537_OFFSET_Y1 0x85 |
| 973 |
* data[14] YAS537_OFFSET_Y2 0x86 |
| 974 |
* |
| 975 |
* data[15] YAS537_HCK 0x88 |
| 976 |
* data[16] YAS537_LCK 0x89 |
| 977 |
*/ |
| 978 |
for (i = 0; i < 12; i++) { |
| 979 |
ret = regmap_write(yas5xx->map, YAS537_MTC + i, |
| 980 |
data[i]); |
| 981 |
if (ret) |
| 982 |
return ret; |
| 983 |
} |
| 984 |
for (i = 0; i < 3; i++) { |
| 985 |
ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i, |
| 986 |
data[i + 12]); |
| 987 |
if (ret) |
| 988 |
return ret; |
| 989 |
yas5xx->hard_offsets[i] = data[i + 12]; |
| 990 |
} |
| 991 |
for (i = 0; i < 2; i++) { |
| 992 |
ret = regmap_write(yas5xx->map, YAS537_HCK + i, |
| 993 |
data[i + 15]); |
| 994 |
if (ret) |
| 995 |
return ret; |
| 996 |
} |
| 997 |
break; |
| 998 |
case YAS537_VERSION_1: |
| 999 |
/* |
| 1000 |
* The second version writes some data into registers but also |
| 1001 |
* extracts calibration coefficients. |
| 1002 |
* |
| 1003 |
* Registers being written: |
| 1004 |
* |
| 1005 |
* data[0] YAS537_MTC 0x93 |
| 1006 |
* data[1] YAS537_MTC+1 0x94 |
| 1007 |
* data[2] YAS537_MTC+2 0x95 |
| 1008 |
* data[3] YAS537_MTC+3 (partially) 0x96 |
| 1009 |
* |
| 1010 |
* data[12] YAS537_OFFSET_X 0x84 |
| 1011 |
* data[13] YAS537_OFFSET_Y1 0x85 |
| 1012 |
* data[14] YAS537_OFFSET_Y2 0x86 |
| 1013 |
* |
| 1014 |
* data[15] YAS537_HCK (partially) 0x88 |
| 1015 |
* YAS537_LCK (partially) 0x89 |
| 1016 |
* data[16] YAS537_OC (partially) 0x9e |
| 1017 |
*/ |
| 1018 |
for (i = 0; i < 3; i++) { |
| 1019 |
ret = regmap_write(yas5xx->map, YAS537_MTC + i, |
| 1020 |
data[i]); |
| 1021 |
if (ret) |
| 1022 |
return ret; |
| 1023 |
} |
| 1024 |
for (i = 0; i < 3; i++) { |
| 1025 |
ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i, |
| 1026 |
data[i + 12]); |
| 1027 |
if (ret) |
| 1028 |
return ret; |
| 1029 |
yas5xx->hard_offsets[i] = data[i + 12]; |
| 1030 |
} |
| 1031 |
/* |
| 1032 |
* Visualization of partially taken data: |
| 1033 |
* |
| 1034 |
* data[3] n 7 6 5 4 3 2 1 0 |
| 1035 |
* YAS537_MTC+3 x x x 1 0 0 0 0 |
| 1036 |
* |
| 1037 |
* data[15] n 7 6 5 4 3 2 1 0 |
| 1038 |
* YAS537_HCK x x x x 0 |
| 1039 |
* |
| 1040 |
* data[15] n 7 6 5 4 3 2 1 0 |
| 1041 |
* YAS537_LCK x x x x 0 |
| 1042 |
* |
| 1043 |
* data[16] n 7 6 5 4 3 2 1 0 |
| 1044 |
* YAS537_OC x x x x x x |
| 1045 |
*/ |
| 1046 |
ret = regmap_write(yas5xx->map, YAS537_MTC + 3, |
| 1047 |
FIELD_PREP(YAS537_MTC3_MASK_PREP, |
| 1048 |
FIELD_GET(YAS537_MTC3_MASK_GET, data[3])) | |
| 1049 |
YAS537_MTC3_ADD_BIT); |
| 1050 |
if (ret) |
| 1051 |
return ret; |
| 1052 |
ret = regmap_write(yas5xx->map, YAS537_HCK, |
| 1053 |
FIELD_PREP(YAS537_HCK_MASK_PREP, |
| 1054 |
FIELD_GET(YAS537_HCK_MASK_GET, data[15]))); |
| 1055 |
if (ret) |
| 1056 |
return ret; |
| 1057 |
ret = regmap_write(yas5xx->map, YAS537_LCK, |
| 1058 |
FIELD_PREP(YAS537_LCK_MASK_PREP, |
| 1059 |
FIELD_GET(YAS537_LCK_MASK_GET, data[15]))); |
| 1060 |
if (ret) |
| 1061 |
return ret; |
| 1062 |
ret = regmap_write(yas5xx->map, YAS537_OC, |
| 1063 |
FIELD_GET(YAS537_OC_MASK_GET, data[16])); |
| 1064 |
if (ret) |
| 1065 |
return ret; |
| 1066 |
/* |
| 1067 |
* For data extraction, build some blocks. Four 32-bit blocks |
| 1068 |
* look appropriate. |
| 1069 |
* |
| 1070 |
* n 7 6 5 4 3 2 1 0 |
| 1071 |
* data[0] 0 [ Cx Cx Cx Cx Cx Cx Cx Cx ] bits 31 .. 24 |
| 1072 |
* data[1] 1 [ Cx C1 C1 C1 C1 C1 C1 C1 ] bits 23 .. 16 |
| 1073 |
* data[2] 2 [ C1 C1 C2 C2 C2 C2 C2 C2 ] bits 15 .. 8 |
| 1074 |
* data[3] 3 [ C2 C2 C2 ] bits 7 .. 0 |
| 1075 |
* |
| 1076 |
* n 7 6 5 4 3 2 1 0 |
| 1077 |
* data[3] 0 [ a2 a2 a2 a2 a2 ] bits 31 .. 24 |
| 1078 |
* data[4] 1 [ a2 a2 a3 a3 a3 a3 a3 a3 ] bits 23 .. 16 |
| 1079 |
* data[5] 2 [ a3 a4 a4 a4 a4 a4 a4 a4 ] bits 15 .. 8 |
| 1080 |
* data[6] 3 [ a4 ] bits 7 .. 0 |
| 1081 |
* |
| 1082 |
* n 7 6 5 4 3 2 1 0 |
| 1083 |
* data[6] 0 [ a5 a5 a5 a5 a5 a5 a5 ] bits 31 .. 24 |
| 1084 |
* data[7] 1 [ a5 a5 a6 a6 a6 a6 a6 a6 ] bits 23 .. 16 |
| 1085 |
* data[8] 2 [ a6 a7 a7 a7 a7 a7 a7 a7 ] bits 15 .. 8 |
| 1086 |
* data[9] 3 [ a7 ] bits 7 .. 0 |
| 1087 |
* |
| 1088 |
* n 7 6 5 4 3 2 1 0 |
| 1089 |
* data[9] 0 [ a8 a8 a8 a8 a8 a8 a8 ] bits 31 .. 24 |
| 1090 |
* data[10] 1 [ a9 a9 a9 a9 a9 a9 a9 a9 ] bits 23 .. 16 |
| 1091 |
* data[11] 2 [ a9 k k k k k k k ] bits 15 .. 8 |
| 1092 |
* data[12] 3 [ ] bits 7 .. 0 |
| 1093 |
*/ |
| 1094 |
val1 = get_unaligned_be32(&data[0]); |
| 1095 |
val2 = get_unaligned_be32(&data[3]); |
| 1096 |
val3 = get_unaligned_be32(&data[6]); |
| 1097 |
val4 = get_unaligned_be32(&data[9]); |
| 1098 |
/* Extract calibration coefficients and modify */ |
| 1099 |
c->Cx = FIELD_GET(GENMASK(31, 23), val1) - 256; |
| 1100 |
c->Cy1 = FIELD_GET(GENMASK(22, 14), val1) - 256; |
| 1101 |
c->Cy2 = FIELD_GET(GENMASK(13, 5), val1) - 256; |
| 1102 |
c->a2 = FIELD_GET(GENMASK(28, 22), val2) - 64; |
| 1103 |
c->a3 = FIELD_GET(GENMASK(21, 15), val2) - 64; |
| 1104 |
c->a4 = FIELD_GET(GENMASK(14, 7), val2) - 128; |
| 1105 |
c->a5 = FIELD_GET(GENMASK(30, 22), val3) - 112; |
| 1106 |
c->a6 = FIELD_GET(GENMASK(21, 15), val3) - 64; |
| 1107 |
c->a7 = FIELD_GET(GENMASK(14, 7), val3) - 128; |
| 1108 |
c->a8 = FIELD_GET(GENMASK(30, 24), val4) - 64; |
| 1109 |
c->a9 = FIELD_GET(GENMASK(23, 15), val4) - 112; |
| 1110 |
c->k = FIELD_GET(GENMASK(14, 8), val4); |
| 1111 |
break; |
| 1112 |
default: |
| 1113 |
dev_err(yas5xx->dev, "unknown version of YAS537\n"); |
| 1114 |
return -EINVAL; |
| 1115 |
} |
| 1116 |
|
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
|
| 1120 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 1121 |
static void yas530_dump_calibration(struct yas5xx *yas5xx) |
| 1122 |
{ |
| 1123 |
struct yas5xx_calibration *c = &yas5xx->calibration; |
| 1124 |
|
| 1125 |
dev_dbg(yas5xx->dev, "f[] = [%d, %d, %d]\n", |
| 1126 |
c->f[0], c->f[1], c->f[2]); |
| 1127 |
dev_dbg(yas5xx->dev, "r[] = [%d, %d, %d]\n", |
| 1128 |
c->r[0], c->r[1], c->r[2]); |
| 1129 |
dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx); |
| 1130 |
dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1); |
| 1131 |
dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2); |
| 1132 |
dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2); |
| 1133 |
dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3); |
| 1134 |
dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4); |
| 1135 |
dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5); |
| 1136 |
dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6); |
| 1137 |
dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7); |
| 1138 |
dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8); |
| 1139 |
dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9); |
| 1140 |
dev_dbg(yas5xx->dev, "k = %d\n", c->k); |
| 1141 |
dev_dbg(yas5xx->dev, "dck = %d\n", c->dck); |
| 1142 |
} |
| 1143 |
|
| 1144 |
static void yas537_dump_calibration(struct yas5xx *yas5xx) |
| 1145 |
{ |
| 1146 |
struct yas5xx_calibration *c = &yas5xx->calibration; |
| 1147 |
|
| 1148 |
if (yas5xx->version == YAS537_VERSION_1) { |
| 1149 |
dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx); |
| 1150 |
dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1); |
| 1151 |
dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2); |
| 1152 |
dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2); |
| 1153 |
dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3); |
| 1154 |
dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4); |
| 1155 |
dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5); |
| 1156 |
dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6); |
| 1157 |
dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7); |
| 1158 |
dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8); |
| 1159 |
dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9); |
| 1160 |
dev_dbg(yas5xx->dev, "k = %d\n", c->k); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 1165 |
static int yas530_set_offsets(struct yas5xx *yas5xx, s8 ox, s8 oy1, s8 oy2) |
| 1166 |
{ |
| 1167 |
int ret; |
| 1168 |
|
| 1169 |
ret = regmap_write(yas5xx->map, YAS530_OFFSET_X, ox); |
| 1170 |
if (ret) |
| 1171 |
return ret; |
| 1172 |
ret = regmap_write(yas5xx->map, YAS530_OFFSET_Y1, oy1); |
| 1173 |
if (ret) |
| 1174 |
return ret; |
| 1175 |
return regmap_write(yas5xx->map, YAS530_OFFSET_Y2, oy2); |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 1179 |
static s8 yas530_adjust_offset(s8 old, int bit, u16 center, u16 measure) |
| 1180 |
{ |
| 1181 |
if (measure > center) |
| 1182 |
return old + BIT(bit); |
| 1183 |
if (measure < center) |
| 1184 |
return old - BIT(bit); |
| 1185 |
return old; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 1189 |
static int yas530_measure_offsets(struct yas5xx *yas5xx) |
| 1190 |
{ |
| 1191 |
const struct yas5xx_chip_info *ci = yas5xx->chip_info; |
| 1192 |
int ret; |
| 1193 |
u16 center; |
| 1194 |
u16 t, x, y1, y2; |
| 1195 |
s8 ox, oy1, oy2; |
| 1196 |
int i; |
| 1197 |
|
| 1198 |
/* Actuate the init coil and measure offsets */ |
| 1199 |
ret = regmap_write(yas5xx->map, YAS530_ACTUATE_INIT_COIL, 0); |
| 1200 |
if (ret) |
| 1201 |
return ret; |
| 1202 |
|
| 1203 |
/* When the initcoil is active this should be around the center */ |
| 1204 |
switch (ci->devid) { |
| 1205 |
case YAS530_DEVICE_ID: |
| 1206 |
center = YAS530_DATA_CENTER; |
| 1207 |
break; |
| 1208 |
case YAS532_DEVICE_ID: |
| 1209 |
center = YAS532_DATA_CENTER; |
| 1210 |
break; |
| 1211 |
default: |
| 1212 |
dev_err(yas5xx->dev, "unknown device type\n"); |
| 1213 |
return -EINVAL; |
| 1214 |
} |
| 1215 |
|
| 1216 |
/* |
| 1217 |
* We set offsets in the interval +-31 by iterating |
| 1218 |
* +-16, +-8, +-4, +-2, +-1 adjusting the offsets each |
| 1219 |
* time, then writing the final offsets into the |
| 1220 |
* registers. |
| 1221 |
* |
| 1222 |
* NOTE: these offsets are NOT in the same unit or magnitude |
| 1223 |
* as the values for [x, y1, y2]. The value is +/-31 |
| 1224 |
* but the effect on the raw values is much larger. |
| 1225 |
* The effect of the offset is to bring the measure |
| 1226 |
* rougly to the center. |
| 1227 |
*/ |
| 1228 |
ox = 0; |
| 1229 |
oy1 = 0; |
| 1230 |
oy2 = 0; |
| 1231 |
|
| 1232 |
for (i = 4; i >= 0; i--) { |
| 1233 |
ret = yas530_set_offsets(yas5xx, ox, oy1, oy2); |
| 1234 |
if (ret) |
| 1235 |
return ret; |
| 1236 |
|
| 1237 |
ret = yas530_measure(yas5xx, &t, &x, &y1, &y2); |
| 1238 |
if (ret) |
| 1239 |
return ret; |
| 1240 |
dev_dbg(yas5xx->dev, "measurement %d: x=%d, y1=%d, y2=%d\n", |
| 1241 |
5-i, x, y1, y2); |
| 1242 |
|
| 1243 |
ox = yas530_adjust_offset(ox, i, center, x); |
| 1244 |
oy1 = yas530_adjust_offset(oy1, i, center, y1); |
| 1245 |
oy2 = yas530_adjust_offset(oy2, i, center, y2); |
| 1246 |
} |
| 1247 |
|
| 1248 |
/* Needed for calibration algorithm */ |
| 1249 |
yas5xx->hard_offsets[0] = ox; |
| 1250 |
yas5xx->hard_offsets[1] = oy1; |
| 1251 |
yas5xx->hard_offsets[2] = oy2; |
| 1252 |
ret = yas530_set_offsets(yas5xx, ox, oy1, oy2); |
| 1253 |
if (ret) |
| 1254 |
return ret; |
| 1255 |
|
| 1256 |
dev_info(yas5xx->dev, "discovered hard offsets: x=%d, y1=%d, y2=%d\n", |
| 1257 |
ox, oy1, oy2); |
| 1258 |
return 0; |
| 1259 |
} |
| 1260 |
|
| 1261 |
/* Used by YAS530, YAS532 and YAS533 */ |
| 1262 |
static int yas530_power_on(struct yas5xx *yas5xx) |
| 1263 |
{ |
| 1264 |
unsigned int val; |
| 1265 |
int ret; |
| 1266 |
|
| 1267 |
/* Zero the test registers */ |
| 1268 |
ret = regmap_write(yas5xx->map, YAS530_TEST1, 0); |
| 1269 |
if (ret) |
| 1270 |
return ret; |
| 1271 |
ret = regmap_write(yas5xx->map, YAS530_TEST2, 0); |
| 1272 |
if (ret) |
| 1273 |
return ret; |
| 1274 |
|
| 1275 |
/* Set up for no interrupts, calibrated clock divider */ |
| 1276 |
val = FIELD_PREP(YAS5XX_CONFIG_CCK_MASK, yas5xx->calibration.dck); |
| 1277 |
ret = regmap_write(yas5xx->map, YAS530_CONFIG, val); |
| 1278 |
if (ret) |
| 1279 |
return ret; |
| 1280 |
|
| 1281 |
/* Measure interval 0 (back-to-back?) */ |
| 1282 |
return regmap_write(yas5xx->map, YAS530_MEASURE_INTERVAL, 0); |
| 1283 |
} |
| 1284 |
|
| 1285 |
static int yas537_power_on(struct yas5xx *yas5xx) |
| 1286 |
{ |
| 1287 |
__be16 buf; |
| 1288 |
int ret; |
| 1289 |
u8 intrvl; |
| 1290 |
|
| 1291 |
/* Writing ADCCAL and TRM registers */ |
| 1292 |
buf = cpu_to_be16(GENMASK(9, 3)); |
| 1293 |
ret = regmap_bulk_write(yas5xx->map, YAS537_ADCCAL, &buf, sizeof(buf)); |
| 1294 |
if (ret) |
| 1295 |
return ret; |
| 1296 |
ret = regmap_write(yas5xx->map, YAS537_TRM, GENMASK(7, 0)); |
| 1297 |
if (ret) |
| 1298 |
return ret; |
| 1299 |
|
| 1300 |
/* The interval value is static in regular operation */ |
| 1301 |
intrvl = (YAS537_DEFAULT_SENSOR_DELAY_MS * MILLI |
| 1302 |
- YAS537_MEASURE_TIME_WORST_US) / 4100; |
| 1303 |
ret = regmap_write(yas5xx->map, YAS537_MEASURE_INTERVAL, intrvl); |
| 1304 |
if (ret) |
| 1305 |
return ret; |
| 1306 |
|
| 1307 |
/* The average value is also static in regular operation */ |
| 1308 |
ret = regmap_write(yas5xx->map, YAS537_AVR, YAS537_MAG_AVERAGE_32_MASK); |
| 1309 |
if (ret) |
| 1310 |
return ret; |
| 1311 |
|
| 1312 |
/* Perform the "rcoil" part but skip the "last_after_rcoil" read */ |
| 1313 |
ret = regmap_write(yas5xx->map, YAS537_CONFIG, BIT(3)); |
| 1314 |
if (ret) |
| 1315 |
return ret; |
| 1316 |
|
| 1317 |
/* Wait until the coil has ramped up */ |
| 1318 |
usleep_range(YAS537_MAG_RCOIL_TIME_US, YAS537_MAG_RCOIL_TIME_US + 100); |
| 1319 |
|
| 1320 |
return 0; |
| 1321 |
} |
| 1322 |
|
| 1323 |
static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = { |
| 1324 |
[yas530] = { |
| 1325 |
.devid = YAS530_DEVICE_ID, |
| 1326 |
.product_name = "YAS530 MS-3E", |
| 1327 |
.version_names = { "A", "B" }, |
| 1328 |
.volatile_reg = yas530_volatile_reg, |
| 1329 |
.volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg), |
| 1330 |
.scaling_val2 = 100000000, /* picotesla to Gauss */ |
| 1331 |
.t_ref = 182, /* counts */ |
| 1332 |
.min_temp_x10 = -620, /* 1/10:s degrees Celsius */ |
| 1333 |
.get_measure = yas530_get_measure, |
| 1334 |
.get_calibration_data = yas530_get_calibration_data, |
| 1335 |
.dump_calibration = yas530_dump_calibration, |
| 1336 |
.measure_offsets = yas530_measure_offsets, |
| 1337 |
.power_on = yas530_power_on, |
| 1338 |
}, |
| 1339 |
[yas532] = { |
| 1340 |
.devid = YAS532_DEVICE_ID, |
| 1341 |
.product_name = "YAS532 MS-3R", |
| 1342 |
.version_names = { "AB", "AC" }, |
| 1343 |
.volatile_reg = yas530_volatile_reg, |
| 1344 |
.volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg), |
| 1345 |
.scaling_val2 = 100000, /* nanotesla to Gauss */ |
| 1346 |
.t_ref = 390, /* counts */ |
| 1347 |
.min_temp_x10 = -500, /* 1/10:s degrees Celsius */ |
| 1348 |
.get_measure = yas530_get_measure, |
| 1349 |
.get_calibration_data = yas532_get_calibration_data, |
| 1350 |
.dump_calibration = yas530_dump_calibration, |
| 1351 |
.measure_offsets = yas530_measure_offsets, |
| 1352 |
.power_on = yas530_power_on, |
| 1353 |
}, |
| 1354 |
[yas533] = { |
| 1355 |
.devid = YAS532_DEVICE_ID, |
| 1356 |
.product_name = "YAS533 MS-3F", |
| 1357 |
.version_names = { "AB", "AC" }, |
| 1358 |
.volatile_reg = yas530_volatile_reg, |
| 1359 |
.volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg), |
| 1360 |
.scaling_val2 = 100000, /* nanotesla to Gauss */ |
| 1361 |
.t_ref = 390, /* counts */ |
| 1362 |
.min_temp_x10 = -500, /* 1/10:s degrees Celsius */ |
| 1363 |
.get_measure = yas530_get_measure, |
| 1364 |
.get_calibration_data = yas532_get_calibration_data, |
| 1365 |
.dump_calibration = yas530_dump_calibration, |
| 1366 |
.measure_offsets = yas530_measure_offsets, |
| 1367 |
.power_on = yas530_power_on, |
| 1368 |
}, |
| 1369 |
[yas537] = { |
| 1370 |
.devid = YAS537_DEVICE_ID, |
| 1371 |
.product_name = "YAS537 MS-3T", |
| 1372 |
.version_names = { "v0", "v1" }, /* version naming unknown */ |
| 1373 |
.volatile_reg = yas537_volatile_reg, |
| 1374 |
.volatile_reg_qty = ARRAY_SIZE(yas537_volatile_reg), |
| 1375 |
.scaling_val2 = 100000, /* nanotesla to Gauss */ |
| 1376 |
.t_ref = 8120, /* counts */ |
| 1377 |
.min_temp_x10 = -3860, /* 1/10:s degrees Celsius */ |
| 1378 |
.get_measure = yas537_get_measure, |
| 1379 |
.get_calibration_data = yas537_get_calibration_data, |
| 1380 |
.dump_calibration = yas537_dump_calibration, |
| 1381 |
/* .measure_offets is not needed for yas537 */ |
| 1382 |
.power_on = yas537_power_on, |
| 1383 |
}, |
| 1384 |
}; |
| 1385 |
|
| 1386 |
static int yas5xx_probe(struct i2c_client *i2c) |
| 1387 |
{ |
| 1388 |
const struct i2c_device_id *id = i2c_client_get_device_id(i2c); |
| 1389 |
struct iio_dev *indio_dev; |
| 1390 |
struct device *dev = &i2c->dev; |
| 1391 |
struct yas5xx *yas5xx; |
| 1392 |
const struct yas5xx_chip_info *ci; |
| 1393 |
int id_check; |
| 1394 |
int ret; |
| 1395 |
|
| 1396 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*yas5xx)); |
| 1397 |
if (!indio_dev) |
| 1398 |
return -ENOMEM; |
| 1399 |
|
| 1400 |
yas5xx = iio_priv(indio_dev); |
| 1401 |
i2c_set_clientdata(i2c, indio_dev); |
| 1402 |
yas5xx->dev = dev; |
| 1403 |
mutex_init(&yas5xx->lock); |
| 1404 |
|
| 1405 |
ret = iio_read_mount_matrix(dev, &yas5xx->orientation); |
| 1406 |
if (ret) |
| 1407 |
return ret; |
| 1408 |
|
| 1409 |
yas5xx->regs[0].supply = "vdd"; |
| 1410 |
yas5xx->regs[1].supply = "iovdd"; |
| 1411 |
ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(yas5xx->regs), |
| 1412 |
yas5xx->regs); |
| 1413 |
if (ret) |
| 1414 |
return dev_err_probe(dev, ret, "cannot get regulators\n"); |
| 1415 |
|
| 1416 |
ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1417 |
if (ret) |
| 1418 |
return dev_err_probe(dev, ret, "cannot enable regulators\n"); |
| 1419 |
|
| 1420 |
/* See comment in runtime resume callback */ |
| 1421 |
usleep_range(31000, 40000); |
| 1422 |
|
| 1423 |
/* This will take the device out of reset if need be */ |
| 1424 |
yas5xx->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); |
| 1425 |
if (IS_ERR(yas5xx->reset)) { |
| 1426 |
ret = dev_err_probe(dev, PTR_ERR(yas5xx->reset), "failed to get reset line\n"); |
| 1427 |
goto reg_off; |
| 1428 |
} |
| 1429 |
|
| 1430 |
yas5xx->map = devm_regmap_init_i2c(i2c, &yas5xx_regmap_config); |
| 1431 |
if (IS_ERR(yas5xx->map)) { |
| 1432 |
ret = dev_err_probe(dev, PTR_ERR(yas5xx->map), "failed to allocate register map\n"); |
| 1433 |
goto assert_reset; |
| 1434 |
} |
| 1435 |
|
| 1436 |
ci = i2c_get_match_data(i2c); |
| 1437 |
yas5xx->chip_info = ci; |
| 1438 |
|
| 1439 |
ret = regmap_read(yas5xx->map, YAS5XX_DEVICE_ID, &id_check); |
| 1440 |
if (ret) |
| 1441 |
goto assert_reset; |
| 1442 |
|
| 1443 |
if (id_check != ci->devid) { |
| 1444 |
ret = dev_err_probe(dev, -ENODEV, |
| 1445 |
"device ID %02x doesn't match %s\n", |
| 1446 |
id_check, id->name); |
| 1447 |
goto assert_reset; |
| 1448 |
} |
| 1449 |
|
| 1450 |
ret = ci->get_calibration_data(yas5xx); |
| 1451 |
if (ret) |
| 1452 |
goto assert_reset; |
| 1453 |
|
| 1454 |
dev_info(dev, "detected %s %s\n", ci->product_name, |
| 1455 |
ci->version_names[yas5xx->version]); |
| 1456 |
|
| 1457 |
ci->dump_calibration(yas5xx); |
| 1458 |
|
| 1459 |
ret = ci->power_on(yas5xx); |
| 1460 |
if (ret) |
| 1461 |
goto assert_reset; |
| 1462 |
|
| 1463 |
if (ci->measure_offsets) { |
| 1464 |
ret = ci->measure_offsets(yas5xx); |
| 1465 |
if (ret) |
| 1466 |
goto assert_reset; |
| 1467 |
} |
| 1468 |
|
| 1469 |
indio_dev->info = &yas5xx_info; |
| 1470 |
indio_dev->available_scan_masks = yas5xx_scan_masks; |
| 1471 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1472 |
indio_dev->name = id->name; |
| 1473 |
indio_dev->channels = yas5xx_channels; |
| 1474 |
indio_dev->num_channels = ARRAY_SIZE(yas5xx_channels); |
| 1475 |
|
| 1476 |
ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 1477 |
yas5xx_handle_trigger, |
| 1478 |
NULL); |
| 1479 |
if (ret) { |
| 1480 |
dev_err_probe(dev, ret, "triggered buffer setup failed\n"); |
| 1481 |
goto assert_reset; |
| 1482 |
} |
| 1483 |
|
| 1484 |
ret = iio_device_register(indio_dev); |
| 1485 |
if (ret) { |
| 1486 |
dev_err_probe(dev, ret, "device register failed\n"); |
| 1487 |
goto cleanup_buffer; |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* Take runtime PM online */ |
| 1491 |
pm_runtime_get_noresume(dev); |
| 1492 |
pm_runtime_set_active(dev); |
| 1493 |
pm_runtime_enable(dev); |
| 1494 |
|
| 1495 |
pm_runtime_set_autosuspend_delay(dev, YAS5XX_AUTOSUSPEND_DELAY_MS); |
| 1496 |
pm_runtime_use_autosuspend(dev); |
| 1497 |
pm_runtime_put(dev); |
| 1498 |
|
| 1499 |
return 0; |
| 1500 |
|
| 1501 |
cleanup_buffer: |
| 1502 |
iio_triggered_buffer_cleanup(indio_dev); |
| 1503 |
assert_reset: |
| 1504 |
gpiod_set_value_cansleep(yas5xx->reset, 1); |
| 1505 |
reg_off: |
| 1506 |
regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1507 |
|
| 1508 |
return ret; |
| 1509 |
} |
| 1510 |
|
| 1511 |
static void yas5xx_remove(struct i2c_client *i2c) |
| 1512 |
{ |
| 1513 |
struct iio_dev *indio_dev = i2c_get_clientdata(i2c); |
| 1514 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 1515 |
struct device *dev = &i2c->dev; |
| 1516 |
|
| 1517 |
iio_device_unregister(indio_dev); |
| 1518 |
iio_triggered_buffer_cleanup(indio_dev); |
| 1519 |
/* |
| 1520 |
* Now we can't get any more reads from the device, which would |
| 1521 |
* also call pm_runtime* functions and race with our disable |
| 1522 |
* code. Disable PM runtime in orderly fashion and power down. |
| 1523 |
*/ |
| 1524 |
pm_runtime_get_sync(dev); |
| 1525 |
pm_runtime_put_noidle(dev); |
| 1526 |
pm_runtime_disable(dev); |
| 1527 |
gpiod_set_value_cansleep(yas5xx->reset, 1); |
| 1528 |
regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1529 |
} |
| 1530 |
|
| 1531 |
static int yas5xx_runtime_suspend(struct device *dev) |
| 1532 |
{ |
| 1533 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 1534 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 1535 |
|
| 1536 |
gpiod_set_value_cansleep(yas5xx->reset, 1); |
| 1537 |
regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1538 |
|
| 1539 |
return 0; |
| 1540 |
} |
| 1541 |
|
| 1542 |
static int yas5xx_runtime_resume(struct device *dev) |
| 1543 |
{ |
| 1544 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 1545 |
struct yas5xx *yas5xx = iio_priv(indio_dev); |
| 1546 |
const struct yas5xx_chip_info *ci = yas5xx->chip_info; |
| 1547 |
int ret; |
| 1548 |
|
| 1549 |
ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1550 |
if (ret) { |
| 1551 |
dev_err(dev, "cannot enable regulators\n"); |
| 1552 |
return ret; |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* |
| 1556 |
* The YAS530 datasheet says TVSKW is up to 30 ms, after that 1 ms |
| 1557 |
* for all voltages to settle. The YAS532 is 10ms then 4ms for the |
| 1558 |
* I2C to come online. Let's keep it safe and put this at 31ms. |
| 1559 |
*/ |
| 1560 |
usleep_range(31000, 40000); |
| 1561 |
gpiod_set_value_cansleep(yas5xx->reset, 0); |
| 1562 |
|
| 1563 |
ret = ci->power_on(yas5xx); |
| 1564 |
if (ret) { |
| 1565 |
dev_err(dev, "cannot power on\n"); |
| 1566 |
goto out_reset; |
| 1567 |
} |
| 1568 |
|
| 1569 |
return 0; |
| 1570 |
|
| 1571 |
out_reset: |
| 1572 |
gpiod_set_value_cansleep(yas5xx->reset, 1); |
| 1573 |
regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs); |
| 1574 |
|
| 1575 |
return ret; |
| 1576 |
} |
| 1577 |
|
| 1578 |
static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops, yas5xx_runtime_suspend, |
| 1579 |
yas5xx_runtime_resume, NULL); |
| 1580 |
|
| 1581 |
static const struct i2c_device_id yas5xx_id[] = { |
| 1582 |
{"yas530", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas530] }, |
| 1583 |
{"yas532", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas532] }, |
| 1584 |
{"yas533", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas533] }, |
| 1585 |
{"yas537", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas537] }, |
| 1586 |
{ } |
| 1587 |
}; |
| 1588 |
MODULE_DEVICE_TABLE(i2c, yas5xx_id); |
| 1589 |
|
| 1590 |
static const struct of_device_id yas5xx_of_match[] = { |
| 1591 |
{ .compatible = "yamaha,yas530", &yas5xx_chip_info_tbl[yas530] }, |
| 1592 |
{ .compatible = "yamaha,yas532", &yas5xx_chip_info_tbl[yas532] }, |
| 1593 |
{ .compatible = "yamaha,yas533", &yas5xx_chip_info_tbl[yas533] }, |
| 1594 |
{ .compatible = "yamaha,yas537", &yas5xx_chip_info_tbl[yas537] }, |
| 1595 |
{ } |
| 1596 |
}; |
| 1597 |
MODULE_DEVICE_TABLE(of, yas5xx_of_match); |
| 1598 |
|
| 1599 |
static struct i2c_driver yas5xx_driver = { |
| 1600 |
.driver = { |
| 1601 |
.name = "yas5xx", |
| 1602 |
.of_match_table = yas5xx_of_match, |
| 1603 |
.pm = pm_ptr(&yas5xx_dev_pm_ops), |
| 1604 |
}, |
| 1605 |
.probe = yas5xx_probe, |
| 1606 |
.remove = yas5xx_remove, |
| 1607 |
.id_table = yas5xx_id, |
| 1608 |
}; |
| 1609 |
module_i2c_driver(yas5xx_driver); |
| 1610 |
|
| 1611 |
MODULE_DESCRIPTION("Yamaha YAS53x 3-axis magnetometer driver"); |
| 1612 |
MODULE_AUTHOR("Linus Walleij"); |
| 1613 |
MODULE_LICENSE("GPL v2"); |